title: Easy transactions using Spring JDBC?
body: <p>I am working on a Java app that uses Spring <a href="https://en.wikipedia.org/wiki/Inversion_of_control" rel="nofollow noreferrer">IoC</a> and JDBC Template classes. I have a DAO class that has 4 methods : m1() to m4(). m1 performs multiple inserts and updates on table t1, m2 on table t2, m3 on t3, etc.</p>
  
  <p>The DAO methods are used as follows:</p>
  
  <pre><code>while(true)
  {
    //process &amp; generate data
  
    dao.m1(data1);
    dao.m2(data2);
    dao.m3(data3);
    dao.m4(data4);
  
    //sleep
  }
  </code></pre>
  
  <p>I want the db operations under the 4 consecutive method calls to be atomic, either all the 4 tables are updated successfully or none are. So, if there is an error while performing operations in m3(), i want to rollback all the changes(updates &amp; inserts) performed in m2 &amp; m1.</p>
  
  <p>So does spring let you do it the following way ? </p>
  
  <pre><code>while (true)
  {
    //process &amp; generate data
  
    transaction = TransactionManager.createNewTransaction();
  
    transaction.start()
  
    try
    {
      dao.m1(data1);
      dao.m2(data2);
      dao.m3(data3);
      dao.m4(data4);
    }
    catch(DbUpdateException e)
    {
      transaction.rollBack();
    }
  
    transaction.end();
  
    // sleep
  </code></pre>
  
  <p>}</p>
  
  <p>or are there better ways to do it?</p>