Oracle Connection Pool Class

前端 未结 3 1589
轻奢々
轻奢々 2021-01-07 00:07

I want to setup a connection pool for a Oracle DB in a Helper class.

public class DbConnection {

// Data source for the pooled connection
private static Ora         


        
相关标签:
3条回答
  • 2021-01-07 00:32

    You should not use a ConnectionPoolDataSource directly. It is intended for use by a connection pool in an application server. It does not provide connection pooling itself. See also https://stackoverflow.com/a/12651163/466862

    In other words: You need to use an actual connection pool, like DBCP, c3p0 or BoneCP, or the UCP (Universal Connection Pool).

    0 讨论(0)
  • 2021-01-07 00:44

    You need you to use OracleDataSource (not OracleConnectionPoolDataSource) and set setConnectionCachingEnabled(true).

         private  static OracleDataSource ods = null;
         ...
         static {
            System.out.println("OracleDataSource Initialization");
            try {
                ods = new OracleDataSource();
                ods.setConnectionCachingEnabled(true);
                ods.setConnectionCacheName("mycache");
                ods.setURL("jdbc:oracle:thin:@//server.local:1521/prod");
                ods.setUser("scott");
                ods.setPassword("tiger");
                Properties cacheProps = new Properties();
                cacheProps.setProperty("MinLimit", "1");
                cacheProps.setProperty("MaxLimit", "4");
                cacheProps.setProperty("InitialLimit", "1");
                cacheProps.setProperty("ConnectionWaitTimeout", "5");
                cacheProps.setProperty("ValidateConnection", "true");
                ods.setConnectionCacheProperties(cacheProps);
    
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
    ...
        public static Connection getConnection()
           throws SQLException {
          return ods.getConnection();
        }
    

    Complete example here.

    0 讨论(0)
  • 2021-01-07 00:53

    oracle.jdbc.pool.OracleDataSource.setConnectionCachingEnabled documentation state the following:

    Deprecated. Use Oracle Universal Connection Pool instead.

    You can download Oracle Database UCP and create your DataSource as follows:

    import oracle.jdbc.pool.OracleDataSource;
    import oracle.ucp.jdbc.PoolDataSource;
    import oracle.ucp.jdbc.PoolDataSourceFactory;
    
    PoolDataSource poolDataSource = PoolDataSourceFactory.getPoolDataSource();
    poolDataSource.setConnectionFactoryClassName(OracleDataSource.class.getName());
    poolDataSource.setURL("jdbc:oracle:thin:@localhost:1521:XE");
    poolDataSource.setUser("SYSTEM");
    poolDataSource.setPassword("****");
    
    0 讨论(0)
提交回复
热议问题