tomcat icon indicating copy to clipboard operation
tomcat copied to clipboard

Fix problem with concurrent access to different ConnectionPool instances

Open pb00067 opened this issue 1 year ago • 6 comments

The Tomcat-JDBC-Connection-Pool is designed to handle more pool (access to different databases) instances. However there's a single static instance for cleaning the pool(s) named poolCleanTimer which apparently has to handle the cleaning for all used pools.

When accessing (opening/closing) different connection pools out from different threads we often run into the exception/stacktrace reported below. We use Hibernate version 6.4 and Tomcat version 9.0.90. This pull request fixes this by using a common lock instance for both methods registerCleaner and unregisterCleaner.

org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Could not create a TomcatJDBC pool at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:276) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:238) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:215) at org.hibernate.boot.model.relational.Database.<init>(Database.java:45) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getDatabase(InFlightMetadataCollectorImpl.java:223) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:191) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:170) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1432) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1503) at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:55) at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:80) at com.wuerth.phoenix.bcserver.base.DatabaseServer.initServer(DatabaseServer.java:325) at com.wuerth.phoenix.bcserver.base.DatabaseServer.<init>(DatabaseServer.java:92) at com.wuerth.phoenix.bcserver.base.ComponentServer.init(ComponentServer.java:140) at com.wuerth.phoenix.bcserver.base.PeerContextFactory.<init>(PeerContextFactory.java:21) at com.wuerth.phoenix.bcutil.ContextFactory.instantiate(ContextFactory.java:115) at com.wuerth.phoenix.bcutil.ContextFactory.init(ContextFactory.java:141) at com.wuerth.phoenix.cis.database.DatabaseController.<init>(DatabaseController.java:11) at com.wuerth.phoenix.cis.database.DatabaseController.getDatabaseController(DatabaseController.java:17) at com.wuerth.phoenix.cis.test.CTSTransationManager$CTSContext.<init>(Unknown Source) at com.wuerth.phoenix.cis.test.CTSTransationManager.createContext(Unknown Source) at com.wuerth.phoenix.cis.test.core.database.CTCDBAdapter.beforeEach(Unknown Source) at com.wuerth.phoenix.cis.test.core.CTCJunit5.beforeEach(Unknown Source) at com.wuerth.phoenix.cis.test.CISTestBase.setUp(Unknown Source) at com.wuerth.phoenix.cis.esg.input.gri.EsgInputGriModelTest.setUp(Unknown Source) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74) Caused by: org.hibernate.HibernateException: Could not create a TomcatJDBC pool at org.hibernate.connection.TomcatJDBCConnectionProvider.configure(TomcatJDBCConnectionProvider.java:191) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:136) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:247) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:215) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:395) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJdbcMetadata(JdbcEnvironmentInitiator.java:262) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:107) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:130) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ... 32 more Caused by: java.lang.IllegalStateException: Timer already cancelled. at java.base/java.util.Timer.sched(Timer.java:409) at java.base/java.util.Timer.schedule(Timer.java:260) at org.apache.tomcat.jdbc.pool.ConnectionPool.registerCleaner(ConnectionPool.java:1537) at org.apache.tomcat.jdbc.pool.ConnectionPool.access$400(ConnectionPool.java:57) at org.apache.tomcat.jdbc.pool.ConnectionPool$PoolCleaner.start(ConnectionPool.java:1618) at org.apache.tomcat.jdbc.pool.ConnectionPool.initializePoolCleaner(ConnectionPool.java:556) at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:465) at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:155) at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) at org.hibernate.connection.TomcatJDBCConnectionProvider.configure(TomcatJDBCConnectionProvider.java:157) ... 41 more

P.S.: Since a user might use a different pool-configuration for each database, it is questionable if we should keep just one common poolCleanTimer. I think from architecture point of view it would be better to have one poolCleanTimer per each single pool instance.

pb00067 avatar Jul 01 '24 10:07 pb00067