我创建了Datasource并尝试使用以下代码获取连接对象,
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
java.sql.Connection vendorConn = null;
try {
ctx = new InitialContext(ht);
javax.sql.DataSource ds
= (javax.sql.DataSource) ctx.lookup("jdbc/myDataSource");
conn = ds.getConnection();
} catch (SQLException e) {
LOGGER.error(e.getMessage());
}
下面我提到了连接对象和可调用对象,
weblogic.jdbc.rmi.SerialConnection_weblogic_jdbc_rmi_internal_ConnectionImpl_weblogic_jdbc_wrapper_JTAConnection_weblogic_jdbc_wrapper_XAConnection_oracle_jdbc_driver_LogicalConnection_12130_WLStub@d4
cstmt = (weblogic.jdbc.rmi.SerialCallableStatement_weblogic_jdbc_rmi_internal_CallableStatementStub_weblogic_jdbc_rmi_internal_CallableStatementImpl_weblogic_jdbc_wrapper_CallableStatement_oracle_jdbc_driver_OracleCallableStatementWrapper_12130_WLStub) weblogic.jdbc.rmi.SerialCallableStatement_weblogic_jdbc_rmi_internal_CallableStatementStub_weblogic_jdbc_rmi_internal_CallableStatementImpl_weblogic_jdbc_wrapper_CallableStatement_oracle_jdbc_driver_OracleCallableStatementWrapper_12130_WLStub@145
当我使用可调用
调用存储过程时,我收到以下异常java.sql.SQLException:weblogic.rmi.extensions.RemoteRuntimeException:
Unexpected Exception
at weblogic.jdbc.rmi.SerialStatement.close(SerialStatement.java:126)
at weblogic.jdbc.rmi.SerialStatement.close(SerialStatement.java:110)
at weblogic.ejb.container.internal.MDListener.execute(MDListener.java:451)
at weblogic.ejb.container.internal.MDListener.transactionalOnMessage(MDListener.java:375)
at weblogic.ejb.container.internal.MDListener.onMessage(MDListener.java:310)
at weblogic.jms.client.JMSSession.onMessage(JMSSession.java:4855)
at weblogic.jms.client.JMSSession.execute(JMSSession.java:4529)
at weblogic.jms.client.JMSSession.executeMessage(JMSSession.java:3976)
at weblogic.jms.client.JMSSession.access$000(JMSSession.java:120)
at weblogic.jms.client.JMSSession$UseForRunnable.run(JMSSession.java:5375)
at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:548)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)
java.sql.SQLException: prepareStatement, Exception = Unexpected Exception
at weblogic.jdbc.rmi.RMIWrapperImpl.invocationExceptionHandler(RMIWrapperImpl.java:102)
at weblogic.jdbc.rmi.RMIStubWrapperImpl.invocationExceptionHandler(RMIStubWrapperImpl.java:34)
at weblogic.jdbc.rmi.SerialConnection.prepareStatement(SerialConnection.java:236)
at weblogic.ejb.container.internal.MDListener.execute(MDListener.java:451)
请建议这是使用数据源连接的正确方法,其中我的所有sql语句都在工作但是没有调用过程而且我需要将串行连接强制转换为sql.Connection。
CallableStatement cst = null;
try {
cst = conn
.prepareCall("{call myProc(?,?,?,?,?,?,?,?)}");
final String typeTableName = "studentdetails";
cst.setInt(1, student.getEmpid());
cst.setInt(2, student.getOrgid());
cst.setInt(3, student.getYearid());
cst.setString(4, student.getClassType());
cst.setInt(5, student.getStudentid());
cst.registerOutParameter(6, Types.ARRAY, typeTableName);
cst.registerOutParameter(7, java.sql.Types.VARCHAR);
cst.registerOutParameter(8, java.sql.Types.VARCHAR);
long startTime=System.currentTimeMillis();
cst.execute();
String dat=cst.getString(7);
//Array arr = cst.getArray(6);
long endTime=System.currentTimeMillis();
if (null != cst.getObject(6)) {
data = (Object[]) ((Array) cst.getObject(6)).getArray();
}
如果我使用datasource,我将cst.getObject(6)作为null,但是如果使用普通的jdbc连接,它可以通过提供对象来正常工作。
答案 0 :(得分:0)
如果您使用的是Weblogic,则必须将以下元素添加到 weblogic.xml
<resource-description>
<res-ref-name>datasource_ref</res-ref-name>
<jndi-name>jdbc/myDataSource</jndi-name>
</resource-description>
然后,您必须通过添加以下元素从部署描述符 web.xml 引用它
<resource-ref>
<res-ref-name>datasource_ref</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
然后,您可以从Java代码中引用Data Src,如下所示,
Context cntxt= (Context)new InitialContext().lookup("java:comp/env");
DataSource ds= (DataSource)cntxt.lookup("datasource_ref");
或者使用资源注入,如下所示,
@ApplicationScoped
public class DBHandler{
@Resource(name="datasource_ref")
private javax.sql.DataSource myDB;
public Connection getConnection(){
return myDB.getConnection();
}
}
您可以使用CDI随时随地使用
DBHandler handler = CDI.current().select(DBHandler.class).get();
或通过现场注射
@Inject
javax.enterprise.inject.Instance<DBHandler> instance;
....
void persist(){
DBHandler handler=instance.get();
Connection con= handler.getConnection();
}