1

我在 Ubuntu 12.04.1 上运行 Glassfish 3.1.2。

我有一个这样定义的数据库连接池:

jdbc-connection-pool validation-table-name="sysibm.sysdummy1" datasource-classname="com.ibm.db2.jcc.DB2DataSource" pooling="false" res-type="javax.sql.DataSource" wrap-jdbc-objects="false" name="eERPnextPool" is-connection-validation-required="true">
      <property name="portNumber" value="12345"></property>
      <property name="databaseName" value="MYDB"></property>
      <property name="driverType" value="4"></property>
      <property name="serverName" value="192.168.0.2"></property>
      <property name="user" value="MYDB"></property>
      <property name="password" value="MYDB"></property>
      <property name="enableNamedParameterMarkers" value="1"></property>
    </jdbc-connection-pool>
    <jdbc-resource pool-name="eERPnextPool" description="" jndi-name="jdbc/eERPnextDataSource"></jdbc-resource>

当我的 EJB 中有这样的代码时:

InitialContext initialContext = null;
Connection conn = null;
Connection conn1 = null;
try {
    initialContext = new InitialContext();
    DataSource workDs = (DataSource) initialContext.lookup("jdbc/eERPnextDataSource");
    conn = workDs.getConnection();
    conn1 = workDs.getConnection();

    System.out.println("conn = " + conn + " = " + conn.unwrap(DB2Connection.class));
    System.out.println("conn1 = " + conn1 + " = " + conn1.unwrap(DB2Connection.class));
} catch (Exception e) {
    Log.error("ERROR", e);
} finally {
    conn.close();
    conn1.close();
    initialContext.close();
}

我得到:

conn  = com.sun.gjc.spi.jdbc40.ConnectionHolder40@2df6b722 = com.ibm.db2.jcc.t4.b@207130c1
conn1 = com.sun.gjc.spi.jdbc40.ConnectionHolder40@2ebfbc98 = com.ibm.db2.jcc.t4.b@207130c1

所以两个不同的连接持有者背后有相同的物理连接。这是为什么?这是正常的吗?

4

1 回答 1

0

将 @TransactionManagement(TransactionManagementType.BEAN) 添加到类定义中:

@LocalBean                                                                                                                                                                
@TransactionManagement(TransactionManagementType.BEAN)                                                                                                                    
public class HelloBean {  
.....
}

有所作为。现在,如果我调用 workDs.getConnection() 两次,我将获得两个连接。但是现在我必须自己调用回滚/提交,这对我来说更方便。

于 2012-12-31T16:18:02.913 回答