• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
R Patel
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ronak Sp", please check your private messages regarding an important administrative matter.
 
R Patel
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic