aspose file tools*
The moose likes Spring and the fly likes how does rollbackFor work ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Reply locked New topic
Author

how does rollbackFor work ?

Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 201
Hey guys,

I was going through the spring docs and came to know about this rollbackFor thing which's used to define the class for which the transaction must be rolled back in case exception occurs of that type. So tried the following-


But it does not work. It basically inserts the record and throws the runtime exception and does not rollback.

Am I right in assuming that that's the way it is supposed to work?


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Can you show your application context file ?


[My Blog]
All roads lead to JavaRanch
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 201
Thanks Christophe for the reply, here is the config file

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

By default transactions @Transactional will automatically rollback when any RuntimeException is thrown. So rollbackFor and noRollbackFor are used to customize specifically what exceptions you want to rollback for. So say you have 15 different RuntimeExceptions that might be thrown and you want only two of them to actually rollback your transaction, then you use the rollbackFor property to list just those two. If you want 12 to rollback and 3 not to rollback, then you use the noRollbackFor property. Basically they are the opposite of each other and you choose the one that will cause you the least amount of typing.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Girish Vasmatkar
Ranch Hand

Joined: Apr 24, 2008
Posts: 201
Thanks Mark for the nice explanation.

But here, in my case, why is it not roll backing the transaction?

Do I need to specify any subclass of RuntimeException for it to work ?

I see the records being inserted in the database even if the rollbackFor is defined here.

Best
pete reisinger
Ranch Hand

Joined: Dec 30, 2009
Posts: 46
as far as I know, your db needs to support transactions, so I can see that you are using mysql, is it innodb or myisam engine? only innodb supports transactions (I think)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Pete.

This is a thread from last August. Please try not to resurrect old threads.

Thanks

Mark
 
Don't get me started about those stupid light bulbs.
 
subject: how does rollbackFor work ?