"Illegal attempt to associate a collection with two open sessions" during test execution

Description

*Describe the bug*
I am currently upgrading a project from hibernate 5.0.10 (wildfly 10), to hibernate 5.4.17 (quarkus).
The application itself works without any problems.
Test execution fails however on the new hibernate version.

I reduced the problem down to some strange session handling.
Inside "AbstractPersistentCollection.setCurrentSession", the sessions are not equal. The collection was created with the entitymanager (and therefore session) I use in my test, but the transaction commit happens with another session.

Further testing showed, that this regression was introduced somewhere between hibernate 5.1.17 and 5.2.0.

I attached an minimal reproducer project, which has hibernate 5.2.0 as dependency.
You can relativly easily switch it to 5.1.17, where the tests execute correctly.

*Expected behavior*
No problems with test execution after the upgrade to hibernate 5.2.0.

*Actual behavior*
When I run my tests, I get the following exception. The test run is failed (red) in this case.

*To Reproduce*
Steps to reproduce the behavior:
1. Download the attached reproducer project
2. Run "mvn clean test"
3. Tests failed.
4. In the pom.xml, change the hibernate-orm.version to 5.1.17.Final, and add hibernate-entitymanager
5. Run "mvn clean test" again
6. No test error

*Environment (please complete the following information):*

  • Output of `uname -a` or `ver`:
    MSYS_NT-10.0 NANB7NLNVP2 2.10.0(0.325/5/3) 2018-06-13 23:34 x86_64 Msys

  • Build tool (ie. output of `mvnw --version` or `gradlew --version`):
    Maven home: C:\eclipse\tools\apache-maven\bin\..
    Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_181\jre
    Default locale: de_DE, platform encoding: Cp1252
    OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Environment

Hibernate 5.2.0,
h2,
Windows 10,
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\eclipse\tools\apache-maven\bin\..
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_181\jre
Default locale: de_DE, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Activity

Show:
Martin Panzer
June 4, 2020, 8:18 PM

I tested a bit further, and figured out that the problem got introduced somewhere between 5.1.17 and 5.2.0.

I updated the reproducer accordingly.

Martin Panzer
July 20, 2020, 12:21 PM

Any updates on this topic?

Andrea Boriero
September 23, 2020, 12:18 PM

Hi , the big change from 5.1 to 5.2 is that Sessionextends EntityManager.

It seems that the spied EntityManager produced by Mokito is not the same instance created by the EntityManagerFactorybut internally the spied one has references to the one created by Hibernate, this messes up the internal checks causing the error you are facing.

A workaround that seems working for your test is the following change

 

Sanne Grinovero
September 24, 2020, 12:02 PM

If it’s a problem with mocking we should close this issue.

Assignee

Unassigned

Reporter

Martin Panzer

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.1.17

Affects versions

Priority

Major
Configure