Hibernate's internal connection pool fails to clean up thread, prevents JVM exit

Description

When using the Hibernate internal connection pool in a simple Java console app, the JVM hangs and waits forever for a thread. The thread is the connection pool thread spawned in DriverManagerConnectionProviderImpl. This thread should be cleaned up by DriverManagerConnectionProviderImpl.stop(), but this method is never called in AbstractServiceRegistryImpl.destroy() (during SessionFactoryImpl.close()), or elsewhere. The only way to get the app to exit normally (yes, other than using a proper connection pool), is to do some ugly forcing like this in HibernateUtil:

public static void stopConnectionProvider() {
final SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
ConnectionProvider connectionProvider = sessionFactoryImplementor.getConnectionProvider();
if (Stoppable.class.isInstance(connectionProvider)) {
((Stoppable) connectionProvider).stop();
}
}

This bug started happening after upgrading to Hibernate 4.3.0-SNAPSHOT from 4.2.0.CR2, so it must have been introduced by a recent change.

Related stackoverflow question:
http://stackoverflow.com/questions/20976322/close-sessionfactory-in-hibernate-4-3

Environment

HIbernate 4.3.0-SNAPSHOT, Postgres 9.3

Activity

Show:
Brett Meyer
March 19, 2014, 9:46 PM

ORM does not currently consider the chain of ServiceRegistries during shutdown, causing this issue. Currently, closing the SessionFactory shuts down the SessionFactoryServiceRegistry only. The ConnectionProvider is closed only when the StandardServiceRegistry (outlives the SessionFactory) itself is destroyed.

Steve Ebersole
March 19, 2014, 10:00 PM
Edited

To be clear... its not that the chain is not considered. The problem all along has been an open question in the design of the registries. Namely can registries be reused as the parent of multiple child repositories. For example, take the BootstrapServiceRegistry.. given this:

Closing the BootstrapServiceRegistry when we close one of the "other" registries is bad.

So the first question is a design question, and whether it makes sense to allow registry reuse as parent in multiple registries. Personally, I think that does make sense. So given that, the question moves to a practical question of how to know when all references to a registry are done with it, which comes down to reference counting or somesuch solution.

Steve Ebersole
March 19, 2014, 10:07 PM

See

Steve Ebersole
March 20, 2014, 2:06 PM

Actually this is a duplicate of

Duplicate

Assignee

Brett Meyer

Reporter

Shane St Savage

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure