File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes jdbcTemplate, Transaction Rollback issue with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "jdbcTemplate, Transaction Rollback issue with "defaultAutoCommit"   (Env - Tomcat, Spring, Sybase)" Watch "jdbcTemplate, Transaction Rollback issue with "defaultAutoCommit"   (Env - Tomcat, Spring, Sybase)" New topic
Author

jdbcTemplate, Transaction Rollback issue with "defaultAutoCommit" (Env - Tomcat, Spring, Sybase)

R Patel
Greenhorn

Joined: May 24, 2010
Posts: 13
Hi,

Thanks in advance for your inputs/suggestions.

Since last two days i am facing the issue with Spring transaction rollback. i am using Tomcat 5.5.28 and Spring. Basically i want to handle transaction with spring, in Tomcat environment. I am using Sybase ASE 12.5 as my back-end database.

I have defined database connection under Tomcat_Home/conf/context.xml.

Everything seems working fine, but I am facing the issue on property 'defaultAutoCommit' under Tomcat_Home/conf/context.xml

Please find below scenarios.

1. if defaultAutoCommit="false" ---> then on successfull execution also its not saving data into backend table.

2. if defaultAutoCommit="true" ---> then on RunTimeException also its not doing RollBack. and updating back-end table.

Anyone please guide me how should i handle the situation,

Please find below code.

Tomcat_Home/conf/context.xml



Java class - CustUtilities.java



applicationContext-jdbc2.xml



JSP/Servlet class from where i call the testSpring() method.



Please provide me your suggestions.

Thanks,
Ronak.
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
one problem I can see is your test method is doing update, and your transaction advice declared read-only=true as opposed to false. By the way when you are using spring, you don't need to use the old-fashion way of declaring datasoure with jndi inside context.xml, you can simple declare you dabasource as a spring bean, and regarding testing you can use junit or testNg, instead of testing your code with jsp or servlet.


Do you know why this cup is useful? Because it is empty.
R Patel
Greenhorn

Joined: May 24, 2010
Posts: 13
Hi Hongli,

Thanks for your time and suggestion.

I tried by removing read-only condition, but still i am facing the same issue. And I have a situation where i cant create dataSource under application code or applicationContext-jdbc2.xml. I have to use context.xml, so I am doing jndi lookup.

Onething i am guessing is if i put defaultAutoCommit="false" then i need to commit the transaction through my code, or Spring can do transaction commit after successful execution of method?

is there any property like after successful execution of method "commit" to database else do "rollback".

Please guide me.

Thank You.
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
I don't know why you code is not working, can you show me your transaction logs. The aop tag you defined in the xml file can be completely replaced by transactional annotation, you should inject an instance of JdbcTemplate to your application code instead of looking it up mannually, With spring 2.5 you can autowire an instance of jdbtemplate in your client code, further more your CustUtilities class looks like a Dao to me, why not annotate you bean with @repository so that spring can translate all your sql execeptions. By the way what situation prevents you from defining your datasource as a spring managed bean?
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3102
    
  40

"Ronak Sp", please check your private messages regarding an important administrative matter.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
R Patel
Greenhorn

Joined: May 24, 2010
Posts: 13
Hi Hongli,

Thanks for your time and suggestions.

As per your suggestion I have modified, and I found that Transaction is not getting initilize/starting. As in my scenarion i may need to use the DataSource credentials from Context.xml or some property file. But for time being as per your suggestion i have defined in applicationContext-jdbc2.xml

And it seems that Transaction is not getting start. Please find below the code and logs as well.

applicationContext-jdbc2.xml



CustUtilities.java



CustService.java



CustDAO.java



call from jsp/servlet



output logs:



I think somewhere i am missing configuration, and transaction is not getting initilize, in the bove scenario its updating the back-end table successfully, while actully it should roll-back the changes.

Thanks,
Ronak.
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
You don't need transaction annotations all over the places, you should keep annotation only for your service class and don't forget to annotation your service class with @Service. As you can see you dependency injected custDAO in CustService. Why not inject a custService instance the same way inside your CustUtilities, there is no need to manually look up custservice there, this is not DI.

P.S add log4j.logger.org.springframework.transaction=DEBUG to your log4j properties file you should see transaction log
R Patel
Greenhorn

Joined: May 24, 2010
Posts: 13
Hi Hongli,

I got the solution, I found that transaction is getting intialize/starting under CustService and CustDAO classes only, as these two classes are managed by Spring. So its doing Rollback for RuntimeException under these two classes. So its doing Rollback as per my expectation. , ( and @Transactional is required to any one of these two classes as per one's requirement not required to put in CustDAO if @Transactional is there on CustService class or any of method under this class )

But it will not do Rollback under CustUtilities class as it is not managed by Spring. And @Transactional is useless for this class and any method under this class.

One question regarding your suggestion. Can you please show me way?

As per my knowledge we need to instiate spring container by following way,

So if i will inject custService under CustUtilities then i need to create <bean name="custUtil"> under applicationContext-jdbc2.xml for CustUtilities class first as mentioned below. (correct me if i am wrong)



and after that i need to get that custUtil bean like following way instead of creating new CustUtilities(), correct me if i am worng.


Or is there any way to instanitate Spring container without using above code?

Thanks again for your help.

Regards,
Ronak.
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
I saw your CustUtilities is using servlet context so I assume you are doing a web app instead of standalone app. what web framework are you using, since most web framework have a spring integration point, and as long as you controller is managed by Spring and if you make CustUtilities as a property of your controller, you wont' need a manual look up for services.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jdbcTemplate, Transaction Rollback issue with "defaultAutoCommit" (Env - Tomcat, Spring, Sybase)