• 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

AutoCommit Issue while using JDBCTemplate

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are using JDBCTemplate in our code. Below is the sample code. Issue is that we want to make the autocommit as off/false but it is autocommitting each and every individual statement. Please let me know how to make autocommit as false using JDBCTemplate in Spring JDBC. Also, can we configure this in the XML?



Edit- Shortened lines in code block to prevent horizontal scrolling
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah don't do that Show me your datasource configuration in your XML.
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DriverManagerDataSource is very basic and useful only for testing use BasicDataSource from Apache. You can set the defulaultAutoCommit like shown below.




How do you have transactions set up in your Spring config?
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,

Below is the snippet from my XML file for transaction configuration:


 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok well your making it harder than it needs to be, have you looked at the annotation based approach to delclaritive transactions?

Take a look at the Spring reference doc here

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations

You will want an interface for every service and repository (this is good practice anyway). This is because by default Spring uses an interface based JDK dynamic proxy (no proxy no transaction) This is interface restriction is also applicable to the XML based declarative transactions you are using currently.

Use the @Transactional on the concrete class method but only on methods exposed by the interface. Also be sure to access the class by the interface. I would read that section of the docs I linked and give it a try. It is much simpler.

A little sampling your transaction configuration in your xml becomes this:




That's it your done. Now on any method that is transactional in one of your beans you simply add the annotation.

So for your example you have this bean



I am going to change it because it looks like you are not using interfaces. I am going to wire up the implementation instead.



Now your your class might look something like this




You also probably have another bean we will call it PatientService when you declare your dao in there make sure to reference it by the interface like this


You may also want to consider moving your @Transactionals to the Service class (in my example put it on the method in PatientServiceImpl). This allows you to group multiple db operations together within a single transaction that will rollback together on exceptions.


 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill for your inputs.

But the customer wants us to implement AOP based transactions. So, it would be great if you can point out what are we missing in the AOP based transacational approach. I have verified the code with 'Spring Recipes' book as well and didn't find any difference in the example provided in the book and my approach.
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be clear, they both are AOP, so if their only requirement is to use AOP based transactions you can use the approach I specified. One is done in XML while the other uses annotations. With the annotation based approach Spring takes care of all the wiring since it can apply advice to an annotation (@Transactional). The XML based approach is much more verbose since you don't have an annotation to apply advice to, you must specify the pointcuts yourself.

You need to set up the interfaces like I mentioned before. Regardless of which approach you use Spring uses an interface based JDK dynamic proxy. This requires you to code to interfaces. I would start with making that correction in your code.

 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill for your time and inputs. I will try updating code as per your suggestions to include the interfaces.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Bill, for explanation about calls of @Transactional methods
via interface only!
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bill,



The above code works for me. If i sysout it give me false. Also in case of exception, the transaction is rolled back.
But there is one thing I fail to understand that in case i remove and try to achieve the same using it does not work. If i sysout , it give me true and in case of exception in 2nd transaction, the 1st transaction is not rolled back. Why?

The entire code is as follows:


 
reply
    Bookmark Topic Watch Topic
  • New Topic