Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Persistence context lost after rollback
Persistence context lost after rollback [message #389735] Thu, 25 June 2009 09:09 Go to next message
Ga'bor Pe'ntek is currently offline Ga'bor Pe'ntekFriend
Messages: 16
Registered: July 2009
Junior Member
Hi All!

I have the following code:
try {
entityManager1.getTransaction().begin();
for (OItem i : itemList) {
entityManager1.persist(i);
}
for (OItem i : toBeDeletedList) {
entityManager1.remove(i);
}
entityManager1.getTransaction().commit();
} catch (javax.persistence.RollbackException ex) {
tetelList.addAll(toBeDeletedList);
} finally {
} finally {
toBeDeletedList = new LinkedList<OItem>();
}


There are two entities (Offer and OItem) in relationship with each other.
The above code is part of the Tetel management service. When the user
deletes a Tetel object the object is removed from the Item list and added
to the toBeDeleted list. If the user hits the save button the above code
runs.

The problem is, if any OItem in the toBeDeleted list is related to an
Offer allready, a persistence exception is thrown. I assume, that in this
case the transaction is rolled back, and the original state is restored,
however after the exception all OItems in the itemList become detached.

Is this the normal behaviour of eclipselink or I am missing something?
I am using eclipselink 1.0.1

Greets,
Gábor
Re: Persistence context lost after rollback [message #389741 is a reply to message #389735] Thu, 25 June 2009 14:00 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

This is the required behavior for JPA, on a rollback, or any failure,
everything becomes detached. You need to get a new persistence context /
EntityManager, and re-read/merge your objects to attempt another commit.

The native EclipseLink UnitOfWork API, did allow resuming the UnitOfWork
on failure in many cases, but this functionality has not been exposed yet
to the JPA API (feel free to log an enhancement for this).

---
James
http://www.nabble.com/EclipseLink---Users-f26658.html


James : Wiki : Book : Blog : Twitter
Re: Persistence context lost after rollback [message #389787 is a reply to message #389741] Mon, 06 July 2009 08:39 Go to previous messageGo to next message
Ga'bor Pe'ntek is currently offline Ga'bor Pe'ntekFriend
Messages: 16
Registered: July 2009
Junior Member
Thanks for the answer! Where should I post my feature request?

Greets,
Gabor
Re: Persistence context lost after rollback [message #389788 is a reply to message #389787] Mon, 06 July 2009 13:47 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

See,

http://wiki.eclipse.org/EclipseLink/Bugs


James : Wiki : Book : Blog : Twitter
Re: Persistence context lost after rollback [message #389789 is a reply to message #389787] Mon, 06 July 2009 13:56 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
I've written an extention on the entitymanager that registers any persist, merge or delete action and holds those until the commit is executed. Then it re-applies these actions.

This because I ran into the same situation as you did, so now it can survive a SQLException. Simply change the entiies so it won't fail and commit again.

Tom
Re: Persistence context lost after rollback [message #389790 is a reply to message #389789] Mon, 06 July 2009 15:09 Go to previous messageGo to next message
Ga'bor Pe'ntek is currently offline Ga'bor Pe'ntekFriend
Messages: 16
Registered: July 2009
Junior Member
tbee wrote:

> I've written an extention on the entitymanager that registers any persist,
merge or delete action and holds those until the commit is executed. Then it
re-applies these actions.

> This because I ran into the same situation as you did, so now it can survive
a SQLException. Simply change the entiies so it won't fail and commit again.

I don't think I clearly understand you. The first part is ok, keep my own
history of the changes, but what do you mean by "re-applies"? Do you mean
applying the operations right before the commit? I don't see how would it
help restoring the context.

Could you explain this a bit detailed?

Thanks,
Ga'bor
Re: Persistence context lost after rollback [message #389792 is a reply to message #389790] Mon, 06 July 2009 15:52 Go to previous message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
> Could you explain this a bit detailed?

The extended EM keeps a list of every merge, removed or persisted entity. Then upon commit it simply loops over the actions and re-executes them. So it will actually do a "em.persist(e)". Eclipselink will make in memory copies and try to persist those. If that fails, you get an exception and then usually the administration of the entities is out of sync; e.g. some of the entities may already have a new versionnr assigned. Now, if you make changes to these entities and then attempt to persist them again, that will fail. However, the original entities, registered by the extended EM, have not been changed. Therefor you can simply make the change and call commit again. Which the will do the "em.persist(e)" again and an new attempt is made.

I've been using this in production for almost a year now. Works flawlessly.

Tom
Previous Topic:@manytomany and "Null primary key encountered in unit of work clone"
Next Topic:MOXy mapping question
Goto Forum:
  


Current Time: Wed Dec 17 21:30:02 GMT 2014

Powered by FUDForum. Page generated in 0.02393 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software