gpt4 book ai didi

java - hibernate SessionFactory.openSession() 是否等待池中的数据库连接可用

转载 作者:行者123 更新时间:2023-12-04 21:02:24 27 4
gpt4 key购买 nike

是否 hibernate SessionFactory.openSession()等待池中的数据库连接可用?

我认为它确实如此,但我有这个异常(exception)的客户

org.hibernate.exception.GenericJDBCException: Could not open connection
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:235)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:162)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1471)
at com.jthink.songlayer.hibernate.HibernateUtil.beginTransaction(HibernateUtil.java:192)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongMatcher.call(MusicBrainzSongMatcher.java:83)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongMatcher.call(MusicBrainzSongMatcher.java:35)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.jthink.songkong.analyse.analyser.MainAnalyserService$EnsureIncreaseCountIfRunOnCallingThread.rejectedExecution(MainAnalyserService.java:100)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at com.jthink.songkong.analyse.analyser.MainAnalyserService.submit(MainAnalyserService.java:121)
at com.jthink.songkong.analyse.analyser.MusicBrainzMetadataMatcher.processMetadataFailedToMatch(MusicBrainzMetadataMatcher.java:107)
at com.jthink.songkong.analyse.analyser.MusicBrainzMetadataMatcher.call(MusicBrainzMetadataMatcher.java:381)
at com.jthink.songkong.analyse.analyser.MusicBrainzMetadataMatcher.call(MusicBrainzMetadataMatcher.java:34)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:689)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
at org.hibernate.c3p0.internal.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:90)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
... 20 more
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1418)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:606)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:526)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:755)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:682)

让我觉得不是,还是等了一段时间就放弃了,我在用 hibernate 4.3.11 C3p0 H2 1.5

我的 hibernate 配置是
    config.setProperty("hibernate.c3p0.min_size","1");
config.setProperty("hibernate.c3p0.max_size","50");
config.setProperty("hibernate.c3p0.max_statements","3000");
config.setProperty("hibernate.c3p0.timeout","2000");
config.setProperty("hibernate.c3p0.maxStatementsPerConnection","50");
config.setProperty("hibernate.c3p0.idle_test_period","3000");
config.setProperty("hibernate.c3p0.acquireRetryAttempts","10");

最佳答案

hibernate session 工厂 openSession() 方法总是打开一个新 session 。完成所有数据库操作后,您应该关闭此 session 对象。
session 对象不是线程安全的,你应该在多线程环境中为每个请求打开一个新的 session

基于此引用
https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/SessionFactory.html#openSession()

Session openSession() throws HibernateException Open a Session. JDBC connection(s will be obtained from the configured ConnectionProvider as needed to perform requested work.

Returns: The created session. Throws: HibernateException - Indicates a peroblem opening the session; pretty rare here.



您也可以检查此链接以解决问题:

A ResourcePool could not acquire a resource from its primary factory or source

com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source

关于java - hibernate SessionFactory.openSession() 是否等待池中的数据库连接可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56899387/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com