File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "spring @Transactional" Watch "spring @Transactional" New topic

spring @Transactional

pete reisinger
Ranch Hand

Joined: Dec 30, 2009
Posts: 46
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?

pete reisinger
Ranch Hand

Joined: Dec 30, 2009
Posts: 46
ok, finally I found an article that clears things up a bit:
I agree. Here's the link:
subject: spring @Transactional
jQuery in Action, 3rd edition