title: How to rollback Spring Transaction when an Exception is thrown
body: <p>I am using spring 3.0.5 and hibernate 3.6. In my project there is a scenario where i have to rollback transaction of any exception in thrown or error occurs.  This the sample code, Everything works fine  except transaction is not getting rolled back when I throw an Exception but if any exception is thrown such as <strong>mysql.IntegrityConstraintException</strong>   then transaction gets rolled back, why this is not happening in my case?  </p>
  
  <p><strong>applicationContext.xml</strong></p>
  
  <pre><code>    &lt;bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="location" value="classpath:database.properties"/&gt;
      &lt;/bean&gt;
        &lt;bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
              &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
              &lt;property name="url" value="${jdbc.url}" /&gt;
              &lt;property name="username" value="${jdbc.username}" /&gt;
              &lt;property name="password" value="${jdbc.password}" /&gt;
  
          &lt;/bean&gt;
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
   &lt;property name="dataSource" ref="myDataSource" /&gt;
      &lt;property name="packagesToScan" value="com.alw.imps"/&gt;
      &lt;property name="configLocation"&gt;    
          &lt;value&gt;
              classpath:hibernate.cfg.xml
          &lt;/value&gt;
       &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="stateDao" class="com.alw.imps.dao.StateDaoImpl"&gt;
       &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
  
          &lt;bean id="stateService" class="com.alw.imps.services.StateService"&gt;
         &lt;property name="stateDao" ref="stateDao"&gt;&lt;/property&gt;
         &lt;property name="cityDao" ref="cityDao"&gt;&lt;/property&gt;
         &lt;property name="customerDao" ref="customerDao"&gt;&lt;/property&gt;
         &lt;/bean&gt;  
  
          &lt;bean id="customerDao" class="com.alw.imps.dao.CustomerDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt;
         &lt;/bean&gt; 
  
              &lt;bean id="cityDao" class="com.alw.imps.dao.CityDaoImpl"&gt;
                &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt;
              &lt;/bean&gt;  
  
  
  
  
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"  /&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;        
  
  &lt;tx:advice id = "txAdvice" transaction-manager="transactionManager"&gt;
  &lt;tx:attributes&gt;
  &lt;tx:method name="*" propagation="REQUIRED" /&gt;
  &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  </code></pre>
  
  <p><strong>Service class StateService</strong></p>
  
  <pre><code>@Transactional(rollbackFor={Exception.class})
  public class StateService {
  
    private StateDaoImpl stateDao;
    private CityDao cityDao;
    private CustomerDao customerDao;
  
    public void setCustomerDao(CustomerDao customerDao) {
      this.customerDao = customerDao;
    }
  
    public void setStateDao(StateDaoImpl stateDao) {
      this.stateDao = stateDao;
    }
  
    public CityDao getCityDao() {
      return cityDao;
    }
  
    public void setCityDao(CityDao cityDao) {
      this.cityDao = cityDao;
    }
  
    public void addState() {
      try {
        State state=new State();
        state.setStateName("Delhi");
        stateDao.create(state);
        addCity();
        addCustomer();
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
  
    public void addCity() throws Exception {
      City city=new City();
      city.setCiytName("Delhi");
      city.setStateId(1);
      cityDao.create(city);
    }
  
    public void addCustomer() throws Exception {
      throw new java.lang.Exception();
    }
  </code></pre>
  
  <p><strong>DAO</strong></p>
  
  <pre><code>public class StateDaoImpl extends GenericDaoImpl&lt;State, Integer&gt; implements StateDao {
  }
  </code></pre>
  
  <p><strong>GenericDaoImpl</strong></p>
  
  <pre><code>public class GenericDaoImpl&lt;T,PK extends Serializable&gt; implements GenericDao&lt;T,PK&gt; {
    public SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
    }
  
    public Session getSession() {
      return sessionFactory.getCurrentSession();
    }
  
    public PK create(T o) {
      Session ss= getSession();
      ss.save(o);
      return null;
    }
  </code></pre>
  
  <p><strong>hibernate.cfg</strong></p>
  
  <pre><code>&lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
      &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
      &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/property&gt;
      &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
      &lt;property name="defaultAutoCommit"&gt;false&lt;/property&gt;
      &lt;mapping class="com.alw.imps.pojo.State"/&gt;
      &lt;mapping class="com.alw.imps.pojo.City"/&gt; 
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>So as I said my problem is transaction is not getting rollback when i throw an exception of type <strong>Exception</strong> from method <strong>addCustomer()</strong></p>