Skip to content

HibernateJpaDialect should warn about connection release mode other than ON_CLOSE (when upgrading from 4.0.x) [SPR-13269] #17860

Closed

Description

@spring-projects-issues

Oliver Becker opened SPR-13269 and commented

We have a Spring MVC web application with Hibernate 4.3.10 as JPA provider and HikariCP 2.3.9 as our data source.

After updating Spring to the latest 4.1 version we noticed connection leaks (reported by Hikari). These leaking connections always belong to readonly transactions, more specific the outermost transaction must be readonly (annotated as @Transactional(readonly=true))

After some debugging the relevant code seems to be in org.springframework.orm.jpa.vendor.HibernateJpaDialect, inner class SessionTransactionData, method resetSessionState.

The point is that this method is called from cleanupTransaction, i.e. after the transaction has been committed and the corresponding connection has been released already. resetSessionState then gets again a connection (the same instance), but this time it is never released. This happens only if resetConnection is true, which in turn happens for readonly transactions. So if this transaction is the outermost transaction then nobody closes the connection obtained in resetSessionState.

I cannot really believe that there is such a serious bug present for already 7 minor releases - so what am I doing wrong? Where is the reset connection supposed the be released?


Affects: 4.1.7

Issue Links:

Referenced from: commits a1107af

0 votes, 5 watchers

Activity

  1. spring-projects-issues commented on Jul 23, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    This is an effect triggered by the changes behind #13599 / #16559. Do you by any chance specify a connection release mode other than ON_CLOSE? In this case you would indeed get a reset executed on the current Connection handle returned by the Hibernate Session, with no explicit close call.

    See the javadoc of HibernateJpaDialect.setPrepareConnection for some details. You should be able to switch that flag off to restore the previous behavior (not propagating the read-only status to the Connection to begin with}}, or to configure connection release mode ON_CLOSE to make it work properly.

    I suppose we should try to identify such scenarios and not try to set/reset the read-only status on the Connection correspondingly, based on the connection release mode in the current Session handle, analogous to what we do in HibernateTransactionManager for native Hibernate access code.

    Juergen

  2. spring-projects-issues commented on Jul 24, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Becker commented

    Yes, first we had an explicit AFTER_TRANSACTION configuration, then we tried AUTO, which doesn't solve the problem either.
    Thanks for explaining the possible work-arounds.

  3. spring-projects-issues commented on Jul 24, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    So just double-checking, does it work with ON_CLOSE for you?

    Note that Hibernate's JdbcTransactionFactory defaults to ON_CLOSE since Hibernate 4.1. This is why we took that as an assumption in HibernateJpaDialect.

    Juergen

  4. spring-projects-issues commented on Jul 24, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Becker commented

    Hmm, Hikari keeps reporting "Apparent connection leak detected", even for ON_CLOSE.

  5. spring-projects-issues commented on Jul 25, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Could you double-check the effect with ON_CLOSE there? I'm surprised that there would be any kind of connection leak in that case...

    Juergen

  6. spring-projects-issues commented on Jul 30, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Becker commented

    Sorry for not responding earlier. It turned out that these leaking connections in the case of ON_CLOSE were indeed a bug in our application (hidden by using AFTER_TRANSACTION). We have fixed it and now everything works correct with ON_CLOSE (resp. AUTO) and the latest Spring release.

    Nevertheless, with AFTER_TRANSACTION the above-mentioned problems still exist (but they're not a blocker for us anymore).

    Oliver

  7. spring-projects-issues commented on Aug 27, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    HibernateJpaDialect logs a warning now if the freshly retrieved Connection handle to reset is not identical to the Connection that it originally prepared at the beginning of the transaction. This will appear for connection release modes other than ON_CLOSE but also for Hibernate <4.2 where call-specific Connection wrappers were returned; this is alright since we do not support prepared connections on Hibernate <4.2 anyway.

    Juergen

  8. added
    in: data
    type: enhancement
    on Jan 11, 2019
  9. added this to the 4.2.1 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: datatype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions