aspose file tools*
The moose likes Spring and the fly likes AutoCommit Issue while using JDBCTemplate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "AutoCommit Issue while using JDBCTemplate" Watch "AutoCommit Issue while using JDBCTemplate" New topic
Author

AutoCommit Issue while using JDBCTemplate

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
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
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Yeah don't do that Show me your datasource configuration in your XML.


[How To Ask Questions][Read before you PM me]
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

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

Joined: Sep 23, 2011
Posts: 140
Hi Bill,

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


Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

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

Joined: Sep 23, 2011
Posts: 140
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

Joined: Mar 07, 2010
Posts: 1680
    
    7

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

Joined: Sep 23, 2011
Posts: 140
Thanks Bill for your time and inputs. I will try updating code as per your suggestions to include the interfaces.
Andrei Karii
Greenhorn

Joined: Sep 15, 2013
Posts: 1
Thank you very much, Bill, for explanation about calls of @Transactional methods
via interface only!
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 358
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:



SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AutoCommit Issue while using JDBCTemplate