我在 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
所以两个不同的连接持有者背后有相同的物理连接。这是为什么?这是正常的吗?