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
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
Joined: Sep 23, 2011
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.
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
Joined: Sep 23, 2011
Thanks Bill for your time and inputs. I will try updating code as per your suggestions to include the interfaces.
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)
subject: AutoCommit Issue while using JDBCTemplate