简体   繁体   中英

Oracle DB Connection Pooling in Multi Threaded Java Program

I have a java process which is multi threaded using ExecutorService (15 threads). Each thread calls store procedure to insert data to table, my connection to be pooled across 15 threads so that I could see multiple commits on the table at the same time, but i only see one connection established for one active thread even through 15 threads are ready and waiting.

Driver: oracle.jdbc.driver.OracleDriver

Following are the connection details I have in my properties file url, username, password

Class.forName(DB_DRIVER); 

DataSource oracleDataSource = new DriverManagerDataSource(DB_CONNECTION, DB_USER,DB_PASSWORD); 

ObjectPool objectPool = new GenericObjectPool(); 

DataSourceConnectionFactory datasourceConnectionFactory = new DataSourceConnectionFactory(oracleDataSource); 

PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(datasourceConnectionFactory, objectPool, null, null, false, true); 
objectPool.setFactory(poolableConnectionFactory); 

PoolingDataSource datasource = new PoolingDataSource(objectPool)

Oracle has Universal Connection Pool (ucp.jar) which is easy to use and is from oracle. All you need is to include ucp.jar in the classpath along with ojdbc6.jar or ojdbc7.jar.

Refer to the UCP Reference Guide: http://docs.oracle.com/database/121/JJUCP/toc.htm

PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
pds.setMinPoolSize(10);
pds.setMaxPoolSize(50);
Connection conn=pds.getConnection(); 

You need a connection pool.

Either the object that manages the executor pool will check out a connection, give it to the ExecutorService, and close it when the task completes OR the ExecutorService will manage it. Make sure that you pay careful attention to connection management and SQL resource cleanup or you'll quickly have problems under high request volume.

Usually it's Java EE app servers that manage connection pools for you, but it sounds like you aren't using one. If that's true, perhaps Apache Database Connection Pool will suit your purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM