I’m trying to make 2 different DB calls with the same transaction entity. I know that I can make both interrogations between begin() and commit() but I’m trying this for educational purposes only.
EntityTransaction transaction = em.getTransaction();
EventService eventService = new EventService();
transaction.begin();
Event currentEvent = eventService.read(eventId);
transaction.commit();
if (currentEvent != null){
CommentService commentService = new CommentService();
transaction.begin();
commentList = commentService.getList(1, id, 50);
transaction.commit();
}
This piece of code throws :
Exception Description: Transaction is currently active
which is normal knowing that I’m attempting a begin() to an already opened transaction.
Is it correct to exclude the second transaction.begin() and just use commit() whenever I have to work with the DB?
LE:
I’m using EclipseLink and RESOURCE_LOCAL
This happens because the
transacton-typeis set toRESOURCE_LOCAL.In this cases you must create some
SingleTonclasses which should handle theEntityManagerand theEntityTransaction.