permaculture playing cards*
The moose likes Spring and the fly likes transaction rollback Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "transaction rollback" Watch "transaction rollback" New topic
Author

transaction rollback

kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54
Hi all,

i have method




Now what i want is even if updateMinSalary() succeds and i am purposefully throwing NullPointer exception from updateMaxSalary() and expecting data should not be updated in the database and whole updateSalary method should be rolled back, but the updateMinSalary() method inserting the data.how to prevent it.
Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
Have you configured annotation and aop autoproxy tags in xml file ? I am suspecting annotations are not been called at all.

Thanks


Manish S.
kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54
Hi i used only <tx:annotation-driven/> tag only
Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
You need to define the transaction manager for your annotation tags. Something like this.Thanks,
kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54
hi manish it is my mistake to not to provide complete details in begining post.

as mentioned in your reply i defined datasource , transaction manager and <tx:annotation-driven /> tag and jdbc template to update Database from begining . Inspite of this the problem occured.

You please use the same configuration and code (within this use jdbc template for database updation) i gave to reproduce the problem.
Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
Have you set transaction manager in I would appreciate if you turned on logging for spring jars and attached the spring source to it and then you can see where does it getting failed.

Thanks
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

First of all you should never throw a null pointer exception. Create your own ApplicationService runtime exception if you need to, but throwing null pointer is bad code smell

Secondly you don't need the 'Rollback for ' as @Transactional will roll back when it encounters any runtime exception by default. On top of that from what you are explaining I don't think you need the propagation tags either spring will wrap the inner transactions into the outer ones and they should roll back together so I would get rid of that to.

How are you configuring your datasource is autocommit set to false?

Thanks,


[How To Ask Questions][Read before you PM me]
kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54
Hi manish,
By default spring looks for transaction manager with name transactionManager . so i have not explicitly added attribute transaction-manager to <tx:annotation-driven /> . I tried adding but no change in result.

Hi Bill,
"How are you configuring your datasource is autocommit set to false? " . No ,is it required to set!
Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
Hi

I too expected the transaction manager should automatically get picked up by spring but I too have similar issues. I need to attached transaction manager to tx:annonation tag to make it work. I was using spring 3.1.1 jars.
I guess, first thing you need to confirm whether transaction manager is getting invoked or not and then perhaps you can check what is causing failure in rollback.

As Bill point it out correctly, if you are writing your database connection code then you need to set autocommit to false in your code before you do any transaction. You can refer to this URL for more information.

Thanks
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

Kish,

It might be helpful if you could post the relevant pieces of your spring configuration file.
kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54
Hi manish,
I am not using connection object anywhere since the main purpose of jdbcTemplate object itself is to avoid the boilerplate code like opening connection ,closing connection etc.
i have seen the URL you have given where connection object is used. I guess there must be a way to solve it without calling connection object

Hi Bill,
the code is in my personal PC.so i cannot post it now.


Thanks
Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 183

Not related but this post was really helpful. None of my transaction were getting rolled back even though I had used the @Transactional attribute. It seems I had missed the following two attributes in my configuration file

And I had to add the namespace in



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: transaction rollback