简体   繁体   English

多线程Java程序中的Oracle DB连接池

[英]Oracle DB Connection Pooling in Multi Threaded Java Program

I have a java process which is multi threaded using ExecutorService (15 threads). 我有一个使用ExecutorService (15个线程)多线程的Java进程。 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. 每个线程都调用存储过程以将数据插入表中,我的连接被池化到15个线程中,这样我就可以同时看到表上的多个提交,但是我只看到为一个活动线程建立了一个连接,即使有15个线程也是如此。准备好并等待。

Driver: oracle.jdbc.driver.OracleDriver 驱动程序: oracle.jdbc.driver.OracleDriver

Following are the connection details I have in my properties file url, username, password 以下是我的属性文件中的连接详细信息url,用户名,密码

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. Oracle具有易于使用且来自oracle的通用连接池(ucp.jar)。 All you need is to include ucp.jar in the classpath along with ojdbc6.jar or ojdbc7.jar. 您只需要在类路径中包括ucp.jar以及ojdbc6.jar或ojdbc7.jar。

Refer to the UCP Reference Guide: http://docs.oracle.com/database/121/JJUCP/toc.htm 请参阅《 UCP参考指南》: 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. 管理执行程序池的对象将签出连接,将其提供给ExecutorService,然后在任务完成时将其关闭,否则ExecutorService将对其进行管理。 Make sure that you pay careful attention to connection management and SQL resource cleanup or you'll quickly have problems under high request volume. 确保特别注意连接管理和SQL资源清理,否则在高请求量下很快就会遇到问题。

Usually it's Java EE app servers that manage connection pools for you, but it sounds like you aren't using one. 通常是Java EE应用服务器为您管理连接池,但听起来您好像并没有使用其中一个。 If that's true, perhaps Apache Database Connection Pool will suit your purpose. 如果是这样,那么Apache Database Connection Pool可能适合您的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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