Spring transaction management outside of application server (No container)

Posted on 2012-09-05
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
    LVL 35

    Accepted Solution

    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=""
         xsi:schemaLocation="   ">
      <!-- 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

    Thank you for your response.
    LVL 35

    Expert Comment

    Not a problem!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    zeroMAx challenge 20 66
    countClumps  challenge 10 70
    nested class vs inner class 5 36
    Unable to start eclipse ? 17 49
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    This video teaches viewers about errors in exception handling.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now