Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: HibernateJpaVendorAdapter's "prepareConnection" may interfere with Hibernate 5.1+ in a JTA environment [SPR-14957] #19524

Closed
spring-issuemaster opened this issue Nov 27, 2016 · 4 comments

Comments

@spring-issuemaster
Copy link
Collaborator

commented Nov 27, 2016

Stéphane Nicoll opened SPR-14957 and commented

See spring-projects/spring-boot#7455 (a sample is available).

The reporter experiences a difference of behaviour when OpenEntityManagerInViewFilter is enabled on a Spring MVC application using Weblogic and Hibernate 5.1 or 5.2. The same code works fine with 5.0

I am wondering if that's a regression in our Hibernate support or in Hibernate itself.


Affects: 4.3.4

Issue Links:

  • #19116 Default connection release mode inconsistent with Hibernate 5.1.1
  • #20710 Make JpaVendorAdapters JTA-aware (in particular for Hibernate 5.1/5.2)

Referenced from: commits 2f80b84, 04af30d

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Nov 27, 2016

Alexandru-Constantin Bledea commented

I also opened an issue on the hibernate issue tracker

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Nov 28, 2016

Alexandru-Constantin Bledea commented

This is the commit that breaks the previous behavior

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Nov 28, 2016

Alexandru-Constantin Bledea commented

It looks like the issue is setting the default modes for spring 5.1.x and spring 5.2.x

For 5.1.x, using these settings (just to override the automatic override in HibernateJpaVendorAdapter), this issue doesn't manifest itself in jta anymore

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="default"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.connection.release_mode" value="auto"/>
            </map>
        </property>
    </bean>

For 5.2.x, using these settings (just to override the automatic override in HibernateJpaVendorAdapter), this issue doesn't manifest itself in jta anymore

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="default"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.connection.handling_mode" value="DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT"/>
            </map>
        </property>
    </bean>

In 5.0.x the default mode for JdbcResourceLocalTransaction was indeed ON_CLOSE, however for JtaTransaction it was AFTER_STATEMENT. It looks like this wasn't taken into account when the 'revert' to the default mode was added.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Nov 28, 2016

Juergen Hoeller commented

This is essentially a known issue: HibernateJpaVendorAdapter.setPrepareConnection mentions the enforced DELAYED_ACQUISITION_AND_HOLD mode on Hibernate 5.2 already. Admittedly, this should more explicitly mention JTA setups and WebLogic in particular... and have a dedicated note in the reference documentation as well.

The underlying technical problem is that we can't influence Hibernate's connection release mode depending on the transaction setup: simply because JPA doesn't allow us to reliably introspect the transaction setup in general. As a consequence, HibernateJpaVendorAdapter leans towards optimizing our common case: Spring-driven resource transactions with full control over the lifecycle of the underlying connection, which requires ON_CLOSE connection mode semantics. Hibernate has been changing the default a few times over the years, and our general policy is to enforce ON_CLOSE by default whenever we need to.

The easiest way out would be to set HibernateJpaVendorAdapter's "prepareConnection" flag to false, or to not use HibernateJpaVendorAdapter in a WebLogic JTA setup to begin with. Of course you may also simply override the corresponding Hibernate property, as you suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.