I have a problem with database connections not being 'released' by javers.
We are using Hibernate 5.0.6, Hikari as our connection pool and the MSSQL Server as our database. Javers is configured as follows (snippet):
JaversBuilder.javers().
registerJaversRepository(SqlRepositoryBuilder.sqlRepository().
withConnectionProvider(() -> ((SessionFactoryImpl) sessionFactory).getServiceRegistry().
getService(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider.class).getConnection()).
withDialect(DialectName.MSSQL).
build()).
build();
Obtaining connections works fine this way. The connection pool opens database connections if no more are available. However, the connections obtained by javers are 'inUse' forever.
61366 [Hikari Housekeeping Timer (pool HikariPool-0)] DEBUG HikariPool - Before cleanup pool stats HikariPool-0 (total=100, inUse=100, avail=0, waiting=1)
61366 [Hikari Housekeeping Timer (pool HikariPool-0)] DEBUG HikariPool - After cleanup pool stats HikariPool-0 (total=100, inUse=100, avail=0, waiting=1)
61366 [HikariCP connection filler (pool HikariPool-0)] DEBUG HikariPool - After fill pool stats HikariPool-0 (total=100, inUse=100, avail=0, waiting=1)
Do I have to manually close the connection? If I try this (just close every connection i gave to Javers after one second), the connection pool is cleared. However, this approach is not an option.
Is there something that i miss? If i have to manually close the connection, is it at least possible to receive a notice from Javers that it doesn't need the connection anymore?
You shouldn't close connections, in fact you shouldn't also open new connections for JaVers.
The main idea is that JaVers reuses connections and transactions opened and closed by Application.
Example scenario:
If you are using JPA API, you can use
JpaHibernateConnectionProvider
bundled with JaVers:If you are using bare Hibernate, you can write similar code, which extracts connection from current Hibernate session (bounded to current thread).
Ok here is my solution. Collecting all connections obtained by Javers and closing them (close() does not close but releases them to the connection pool!) after Javers work is done:
A class to collect the connections:
Wrapping the SqlRepository to call cleanup after Javers work is done:
And finally, putting it all together: