2017-06-14 17 views
0

我已經創建數據源並嘗試使用下面的代碼獲取連接對象,不能夠使用可調用語句從連接池從WLS 12.1.3

 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[email protected]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[email protected]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語句進行工作,但過程是沒有得到所謂的,也是我需要的是類型轉換SerialConnection到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(); 
       } 

如果我使用數據源,我越來越cst.getObject(6)爲空,但如果使用正常的JDBC連接是通過提供對象工作的罰款。

回答

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> 

然後你可以引用你的Data Src從y使用資源注入喜歡我們的Java代碼類似如下,

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(); 
} 
+0

此連接是否支持Store過程調用? – user3428736

+0

是的。我們在這裏做的只是挖掘來自應用程序服務器的連接池的連接。只要應用程序服務器的JDBC實現支持存儲過程調用,任何從它挖掘出的連接都應支持該功能。 – TMtech