Spring transaction management outside of application server (No container)

Posted on 2012-09-05
Medium Priority
Last Modified: 2012-09-05
Hi, I am beginner to spring transaction management. I am uaing SimpleJdbcTemplate to perform my jdbc operations. I am having requirement to rollback some of the updates when there is some error. Can you please provide me example for this ?

Thanks in advance
Question by:kcmuthyala2
  • 2
LVL 36

Accepted Solution

mccarl earned 2000 total points
ID: 38370726
Spring's Transaction Management does not rely on having to run within an application server. You can use a local DataSourceTransactionManager to provide transaction management on a standard JDBC datasource.

Then you have the option to use whichever style of Spring Transaction Management that you want, declaritive (both in XML and via annotations) or programmatic. I am generally a fan of the annotation based config and so that is what I have used mostly. A small example of the relevent parts follows.

// the service class that we want to make transactional
public class DefaultFooService implements FooService {
  private SimpleJdbcTemplate jdbcTemplate;
  void setDataSource(DataSource dataSource) {
    jdbcTemplate = new SimpleJdbcTemplate(dataSource);

  Foo getFoo(String fooName) {
    return jdbcTemplate.queryForObject("SELECT * FROM TABLE WHERE name = ?", new RowMapper<Foo>() {//... Implementation here}, fooName);

  Foo getFoo(String fooName, String barName);

  // This method (and the other 3 aswell) will be run within transactions due to @Transactional above, and the tx will rollback if any RuntimeException is thrown either ourselves or from the Spring framework
  void insertFoo(Foo foo) {
    // Do some jdbcTemplate things here
    if (someErrorConditionExists) {
      throw new RuntimeException("Some error occurred");
    // Do some more jdbcTemplate things here

  void updateFoo(Foo foo);

Open in new window

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  <!-- configure a traditional JDBC datasource here -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

  <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="txManager"/>

  <!-- a PlatformTransactionManager is still required -->
  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  <!-- this is the service object that we want to make transactional -->
  <bean id="fooService" class="x.y.service.DefaultFooService">
    <property name="dataSource" ref="dataSource"/>

  <!-- other <bean/> definitions here -->


Open in new window

(Note: I have used jdbcTemplate directly in the Service class above just to keep things simple, but you want to have a DAO layer with the low level JDBC calls in it, and the service make use of the DAO. Either way, the idea is the same)

The thing to note about the above is that each method within the service class, which is annotated with @Transactional, will run under a transaction, and any RuntimeException thrown whilst in one of those methods will make the transaction rollback. The @Transactional can be placed on the method level too if you need finer control of transactional management. See the reference doc that I linked to above, for some tips on using this annotation strategy though, as there can be some traps.

Author Closing Comment

ID: 38370771
Thank you for your response.
LVL 36

Expert Comment

ID: 38370777
Not a problem!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
A solution for Fortify Path Manipulation.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question