title: Spring Transaction - automatic rollback of previous db updates when one db update failes
body: <p>I am writing a simple application (Spring + Hibernate + PostgreSql db). I am just trying to  construct a sample object and persist in db.</p>
  
  <p>I run a simple java class main method where i have loaded the applicationContext and have got reference to the service class as below</p>
  
  <pre><code>TestService srv = (TestService)factory.getBean("testService");  
  </code></pre>
  
  <p>Application Context - context :</p>
  
  <pre><code>&lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactoryVsm" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="testService" class="com.test.service.TestServiceImpl"&gt;
      &lt;property name="testDao" ref="testDao"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="testDao" class="com.test.dao.TestDaoImpl&gt;
      &lt;property name="sessionFactory" ref="sessionFactoryVsm"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <hr>
  
  <p>In TestService I have injected TestDao. In test service method I have constructed to employee objects
  emp1 and emp2 and calling dao twice to update. </p>
  
  <p>TestDaoImpl code:</p>
  
  <pre><code>public void saveOrUpdate(BaseDomainModel baseObject) {
  
      Session session = null;
      try {
          session = getHibernateTemplate().getSessionFactory().openSession();
          session.saveOrUpdate(baseObject);
          session.flush();
      } catch (Exception e) {
          logger.error("Generic DAO:saveOrUpdate::" + e);
          e.printStackTrace();
      } finally {
          if (session != null) {
              session.close();
          }
      }
  
  }
  </code></pre>
  
  <p>When emp2 update fails emp1 should also fail. How do I do that. Please advice</p>
  
  <p>Thanks in advance</p>
  
  <p>Updated : </p>
  
  <p>Thanks Nanda. I tried Declarative transaction. But it is not working. emp1 gets persisted and not rolled back eveb second dao call fails. I have added transaction advice to the method.<br>
    
         
          
          
          
      <br>
      
          
          
      </p>
  
  <p>to test if the transaction advice is applied or not i changed the propagation to "NOT_SUPPORTED". but still emp1 gets persisted. the expectation is we should have got Transaction Not Supported type of exception. please advice .</p>
  
  <p>UPDATED </p>
  
  <p>@seanizer - Thanks for the update. I have even tried adding   </p>
  
  <pre><code>@Transactional(propagation=Propagation.NOT_SUPPORTED)
  public void saveEmp(Employee emp)
  </code></pre>
  
  <p>to that service method. But it didn't work. Moreover iterating the <code>collection&lt;BaseDomainModel&gt;</code> hold good only if i need to call one dao. If in case i have to call two  different dao to persist obj1 and obj2- this may not help. Just to check if the transaction is getting applied I get 
  <code>@Transactional(propagation=Propagation.NOT_SUPPORTED)</code>. 
  But still obj1 got persisted. 
  I just doubt if the xml configuration/ annotation given is correct. please check</p>
  
  <pre><code>&lt;bean id="txManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactoryVsm" /&gt;
      &lt;/bean&gt;
  
  &lt;tx:advice id="txAdvice" transaction-manager="txManager"&gt;
      &lt;tx:attributes&gt;
           &lt;tx:method name="saveEmp" propagation="REQUIRED" rollback-for="Exception"/&gt;
          &lt;tx:method name="*"/&gt;
      &lt;/tx:attributes&gt;
   &lt;/tx:advice&gt;   
   &lt;aop:config&gt;
      &lt;aop:pointcut id="testServiceOperation" expression="execution(*com.test.service.*(..))"/&gt;
      &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="testServiceOperation"/&gt;
    &lt;/aop:config&gt; 
  </code></pre>
  
  <p>I am using org.springframework.orm.hibernate3.HibernateTransactionManager for the transactionManager. Is this correct ?</p>
  
  <hr>
  
  <p>Updated </p>
  
  <p>I have created my exception class myRuntimeExp extending from RuntimeException and throwing the same from Dao method to the service method. but still the rollback is not happening. I just doubt if I have correctly given the configurations in the applnContext.xml. Can someone help me how to check if the transaction advice / annotation is being applied to the method or not? is there any way of running it in a debugging mode and check </p>
  
  <p>Issue :</p>
  
  <p>I was using </p>
  
  <pre><code>session = getHibernateTemplate().getSessionFactory().openSession();
  </code></pre>
  
  <p>But it should be current session and it is working fine.</p>
  
  <pre><code>session = getHibernateTemplate().getSessionFactory().getCurrentSession();
  </code></pre>