3

I am using the Hibernate Session obtained from em.unwrap(..) method. I couldn't seem to find any info on whether I have to release it after usage. The transaction is managed by the bean and the entity manager is injected using @Inject.

    boolean transactionSuccess = false;
    try {
        utx.begin();
        final Session session = em.unwrap(Session.class);
        transactionSuccess = true;
    } finally {
        commitOrRollback(transactionSuccess);
    }

I presume that the entity manager instance is managed by the container. Since the Session is more or less the underlying implementation do I have to release it? It is AutoCloseable after all.

the only relevant info i found was this (but it seems vanilla JPA): After using the unwrap method on entitymanager to get the native hibernate session do I have to close both?

jl.
  • 752
  • 6
  • 19

1 Answers1

0

No, you requested an underlying object, the responsibility to close that object lies with the EntityManager.

Session to be AutoCloseable is reasonable when you work with unwrapped Hibernate.

aschoerk
  • 3,333
  • 2
  • 15
  • 29