vladmihalcea / hypersistence-optimizer

Hypersistence Optimizer allows you to get the most out of JPA and Hibernate. By scanning your application configuration and mappings, Hypersistence Optimizer can tell you what changes you need to do to speed up your data access layer.
https://vladmihalcea.com/hypersistence-optimizer/
Apache License 2.0
306 stars 43 forks source link

Hypersistence EventListener Session unwrapping fails in JTA environment #225

Closed rramostdev closed 6 months ago

rramostdev commented 6 months ago

Hi, We have been using the 2.7.1 version with our project and is great, but the thing is that we decided to update to 2.8.1 and we have started to notice some errors related to the sessions:

java.lang.IllegalStateException: Session/EntityManager is closed

mainly related to the implementation of the event listeners in the Hibernate56SessionFactory class, in some casses when doing the unwrap to the session of the event, it will fail as it's already closed. To isolate the error, we tested with the hypersistence-optimizer-spring-boot-example module of the repo and it also results in error, the steps to reproduce the error are the following:

  1. Update the spring-boot.version to at least 2.6.0 (we use 2.7.1)
  2. Add the narayana dependency
    <dependency>
        <groupId>me.snowdrop</groupId>
        <artifactId>narayana-spring-boot-starter</artifactId>
        <version>2.6.7</version>
    </dependency>
  3. Add and execute the following test case:
    @Test
    public void test() {
     forumService.newPost("TestPost", Arrays.asList("hibernate", "jpa"));
    }

    The test will fail with will a Session/EntityManager is closed IllegalStateException. We've also tested the hypersistence-optimizer-spring-boot3-example with the 3.0.0 version of narayana and it also fails. Any suggestions?

vladmihalcea commented 6 months ago

@rramostdev Thanks for the detailed report. I could replicate the issue and I will provide a fix for it.

vladmihalcea commented 6 months ago

Fixed.

vladmihalcea commented 6 months ago

@rramostdev The 2.8.2 version has been released and provides a fix for this issue. Let me know if it works fine now for you.

rramostdev commented 6 months ago

Thanks so much for such a quick fix, we're currently testing it and will let you know our results when we finish.

rramostdev commented 6 months ago

So the tests were good, it seems that everything works smoothly, again thanks so much for such a quick response.

vladmihalcea commented 6 months ago

I'm glad I could help.