0

我的 JAVA 应用程序使用多线程一次处理多个请求。因此,不同的请求同时使用不同的线程进行处理。

我正在使用以下 hibernate.properties 使用 hibernate 和 C3P0 访问我的 Oracle 数据库:

hibernate.bytecode.use_reflection_optimizer=false
hibernate.connection.driver_class=oracle.jdbc.OracleDriver
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.search.autoregister_listeners=false

hibernate.connection.url=${jdbc.url}
hibernate.default_schema=${jdbc.schema}
hibernate.connection.username=${jdbc.username}
hibernate.connection.password=${jdbc.password}

hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=10
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50

和 c3p0.properties:

c3p0.preferredTestQuery=SELECT 1 from dual
c3p0.testConnectionOnCheckin=true
c3p0.idleConnectionTestPeriod=10
c3p0.driverClass=oracle.jdbc.driver.OracleDriver

(我还使用 testConnectionOnCheckout 而不是 testConnectionOnCheckin 对其进行了测试)。

我的 java 代码执行以下操作:

    Session session = sessionFactory.openSession();
    try{                
        session.beginTransaction();

        Log.debug(localizator + "Start");
        processCounters(id, user, session);
        Log.debug(localizator + "Stop");
        session.getTransaction().commit();

    } finally{
        session.close();
    }

当我运行它时,它为每个线程打印“开始”,但在一个持久化到数据库中被“锁定”,并且没有打印“停止”。

如果我在数据库中查看打开的会话,则有 10 个打开的会话(在 c3p0 中配置的最大数量),但它们都是空闲的。有没有办法让 c3p0 释放一些空闲连接,所以至少一个线程结束了它的进程(除了增加最大连接数)?

4

1 回答 1

0

将连接池更改为 BoneCP 已解决该问题。我使用了以下配置(我认为可以对其进行调整以进一步提高过程的速度):

bonecp.idleMaxAge=240
bonecp.idleConnectionTestPeriod=60
bonecp.partitionCount=3
bonecp.acquireIncrement=1
bonecp.maxConnectionsPerPartition=5
bonecp.minConnectionsPerPartition=2
bonecp.statementsCacheSize=50
bonecp.releaseHelperThreads=3

现在它不会挂起,更好的是,它提高了查询的性能。

于 2013-04-29T09:14:37.127 回答