JDBC3.0规范规定了如下的类和接口来支持数据库连接池的实现。
- javax.sql.ConnectionEvent
- javax.sql.ConnectionPoolDataSource
- javax.sql.PooledConnection
- javax.sql.ConnectionEventListener
规范上说PooledConnection 对象表示到数据源的物理连接. 那么执行两次PooledConnection.getConnection操作获得的逻辑连接背后的物理连接是一样的.确实如此,代码验证:
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
-
- import oracle.jdbc.pool.OracleConnectionPoolDataSource;
- import oracle.jdbc.pool.OraclePooledConnection;
-
- public class pooledConnection {
- static OraclePooledConnection opc;
-
- public static void main(String args[]) throws SQLException,
- InterruptedException {
- OracleConnectionPoolDataSource ps = null;
- ps = new OracleConnectionPoolDataSource();
-
- ps.setDatabaseName("orcl");
- ps.setServerName("localhost");
- ps.setPortNumber(1521);
- ps.setDriverType("thin");
- ps.setUser("guojje");
- ps.setPassword("guojje");
-
- opc = (OraclePooledConnection) ps.getPooledConnection();
-
- Connection c1 = opc.getConnection();
-
- c1.setAutoCommit(false);
-
- PreparedStatement p1 = c1
- .prepareStatement("insert into test values(1)");
- p1.execute();
-
-
- Connection c2 = opc.getConnection();
-
- System.out.println(c2.equals(c1));
-
- c2.setAutoCommit(false);
- PreparedStatement p2 = c2
- .prepareStatement("insert into test values(2)");
- p2.execute();
- c2.commit();
-
- Thread.currentThread().sleep(1000000);
-
- }
- }
通过查看数据库存表可以,两次操作都成功. 说明c1与c2底层物理连接是一样的.
本文转自 anranran 51CTO博客,原文链接:http://blog.51cto.com/guojuanjun/844555