Does hibernate SessionFactory.openSession() wait for a database connection to be available from pool

Tags: , , , ,



Does hibernate SessionFactory.openSession() wait for a database connection to be available from pool ?

I assumed it did, but I have customer with this 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)

making me think it isn’t, or is it waiting for a certain time and then giving up, I am using Hibernate 4.3.11 with C3p0 and H2 1.5

My Hibernate config is

    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");

Answer

Hibernate SessionFactory openSession() method always opens a new session. you should close this session object once you are done with all the database operations. Session objects are not thread safe,you should open a new session for each request in multi-threaded environment

base on this refrence 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.

also you can check this links to resolve problem:

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



Source: stackoverflow