Hibernate Shared Sessions

Since I migrated to Hibernate 6.x+ I started having problems with shared sessions in unit sessions

I have a problem where I have the following structure:

I have a test that first creates a record (banana).

After that, I have a concurrency test that triggers 10 threads in 15 iterations that try to delete the record (banana), but I am receiving the message:
Caused by: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [banana#123]

This behavior did not occur in version 5.x of hibernate, it only started in version 6.x

Below is an example of how I set up my test

@SpringBootTest
@RunWith(SpringRunner.class)
public class BananaConcurrentDeletedTest {

    private static final int THREADS = 10;
    private static final int ITERATIONS = 15;
    private static final String BANANA_ID = "123";

   @Autowired
    private BananaConcurrentDeletedFixture fixture;

    @Test
    public void ensureConcurrentBananaIsCorrect() throws Exception {

        createBanana();

        Set<String> bananaIds = new HashSet<>();
        ids.add(BANANA_ID);

        Target target = new Target(ids);

        for (int i=0; i<THREADS; i++) {
            Thread thread = new Thread(target);
            thread.start();
        }

        target.endLatch.await();
    }
    
   private void createBanana() throws Exception {
        this.fixture.createBanana(BANANA_ID);
    }

private void deleteBanana() throws Exception {
        this.fixture.createBanana(BANANA_ID);
    }
}

My Target implemtation:

   private class Target implements Runnable {
        private final CountDownLatch startLatch = new CountDownLatch(THREADS);
        private final CountDownLatch endLatch = new CountDownLatch(THREADS);

        private final Set<String> ids;

        public Target(Set<String> ids) {
            this.ids = ids;
        }

        @Override
        public void run() {
            try {
                startLatch.countDown();
                startLatch.await();

                for (int i=0; i<ITERATIONS; i++) {
                   deleteBanana(this.ids);
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            } finally {
                endLatch.countDown();
            }
        }
    }

I don’t know what you think you’re doing, but a Hibernate Session is not thread safe. Also, you’re not sharing the full code, so it’s hard to understand what is going on exactly.

What I’m trying to do within my test method is:
1 - create a record and insert it into the database
2 - create 10 threads that will simultaneously try to delete the created record

Both are used by different service classes.
When running the test in Hibernate 5.x, the scenarios pass successfully
When running the test in Hibernate 6.x, the deletion scenarios fail to perform the flush, because there are 2 objects with the same ID (object created in item 1) in the session.

When I run the test with the object already persisted in the database, I have no problems with concurrency of deletions.

Apparently, the process that creates the object and the one that removes it are related in the same Session, however, I have nothing explicitly informed that they should run in the same Transaction/Session.

If you’re not sharing Session with multiple threads, then you should be able to reproduce whatever problem you’re facing also with a simple code, no threads needed. If you can pull that off, please share that.