One of the issues with open-in-view is that connections are not released for a long time, which can lead to connection pool starvation.

Sometimes, controller method even makes no queries for seconds and doesn't really need to keep the connection for itself all that time. For example that is often the case, when application makes REST call after making database query.

This pull request gives user setting, which tells transaction manager, that it should release JDBC connection after transaction is finished (successfully or not). Connection release only happens if the setting is turned on and if open-in-view is enabled. If open-in-view is disabled, then entity manager is closed after transaction is finished and connection is released anyway.

What this pull request changes in existing behaviour

  1. JpaDialect#releaseJdbcConnection is now only called if entity manager is pre-bound. This happens if open-in-view is turned on.
  2. JpaDialect#cleanupTransaction is now called before JpaDialect#releaseJdbcConnection , because if connection is released, transaction manager can't reset isolation level and read-only flag. I think the only reason nobody noticed that before is that nobody tried to override JpaDialect#releaseJdbcConnection for HibernateJpaDialect

What this pull request adds

  1. releaseConnectionAfterTransaction setting for HibernateJpaDialect and which user can configure through HibernateJpaVendorAdapter (like showSql and generateDdl).
  2. HibernateJpaDialect#releaseJdbcConnection method now really releases JDBC connection if releaseConnectionAfterTransaction setting is enabled.

P. S. I think those changes are ok, because the behaviour is copied from HibernateTransactionManager. Hibernate 6 is officially only supported through JpaTransactionManager, so it would be nice to port this HibernateTransactionManager feature.