Hello,

I have the use case where I need a transaction context within the REST
layer and after in the OSGi service layer as well. Currently achieve this
by another mechanism in the REST, and via JpaTemplate.tx() in the OSGi
service.

This currently breaks when i try to persist/merge something within the
service call, Hibernate says
2021-07-28T11:07:16.143+0200 ERROR
org.hibernate.proxy.AbstractLazyInitializer
http-listener-worker-thread-2181 (14000)
[org.hibernate.orm.core:5.4.32.Final] 000 HHH000485: Illegally attempted to
associate a proxy for entity [...] with two open sessions.
I saw JpaTemplate joins an existing transaction when the TransactionType is
Required, but never reuses an existing Hibernate Session (which in
hibernate 5 at least also implements the EntityManager interface).

>From EmSupplierImpl:
EntityManager em = emf.createEntityManager(); in Hibernate this results in
SessionFactoryImpl.openSession() which always creates a new session.

Is it possible to force EmSupplierImpl to call
SessionFactory.getCurrentSession() instead of openSession(), or override
some method to do this manually ? Also would this be correct at all as it
seems this behavior was forced by design by Aries JPA ?

I thought about adding JpaTemplate.tx() also in the REST layer. Then this
would probably work. The thing I don't like too much about that approach is
that in REST we only need a transaction, but make no DB calls at all, so
wouldn't this be an overhead then ?

What would you recommend the best way to solve the above exception
(AbstractLazyInitializer) would be in my setup ?

Thank you,
Regards,
Vassil

Reply via email to