aspose file tools*
The moose likes Spring and the fly likes spring @Transactional Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "spring @Transactional" Watch "spring @Transactional" New topic
Author

spring @Transactional

pete reisinger
Ranch Hand

Joined: Dec 30, 2009
Posts: 46
Hi,
I'm having difficulties to understand how @Transactional works.
This is how I understand it, please let me know if it is correct.

so method annotated by @Transactional annotation with value propagation.REQUIRES_NEW, REQUIRED etc. tells underlying db that the query(ies) shoud be transactional, so if there's any error then it should roll back changes made (if any)
readOnly=true marks that transaction is read only

the problem is if I call two transactional methods - getById(..) and delete(...), both of them are transactional (first is propagation.REQUIRED, readOnly=true, second propagation.REQUIRES_NEW, readOnly=false
then I get exception that i have two opened sessions.
If second is propagation.REQUIRED then I get exception saying that the object will be resaved. I suppose that it is because both use the same transaction (because it is in one request) and if first one marks it as read only then it cannot be overriden by second one, so it throws exception and is rolled back, therefore resaved.

so it is not really about methods annotated by @Transactional but also in which order they are called. because it applies on the whole request (I'm talking about mvc web application)

the way my service methods are now is: getters - propagation.NOT_SUPPORTED and save, delete propagation.REQUIRES_NEW
is it correct?

thanks
pete reisinger
Ranch Hand

Joined: Dec 30, 2009
Posts: 46
ok, finally I found an article that clears things up a bit:
http://www.ibm.com/developerworks/java/library/j-ts1.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: spring @Transactional