title: Annotation @Transactional. How to rollback?
body: <p>I used this annotation successfully for a Dao class. And rollback works for tests. </p>
  
  <p>But now I need to rollback real code, not just tests.
  There are special annotations for use in tests. But which annotations are for non-test code?
  It is a big question for me. I spent a day for that already.  The official documentation did not meet my needs. </p>
  
  <pre><code>class MyClass { // this does not make rollback! And record appears in DB.
          EmployeeDaoInterface employeeDao;
  
          public MyClass() {
              ApplicationContext context = new ClassPathXmlApplicationContext(
                      new String[] { "HibernateDaoBeans.xml" });
              employeeDao = (IEmployeeDao) context.getBean("employeeDao");
           }
  
          @Transactional(rollbackFor={Exception.class})
      public void doInsert( Employee newEmp ) throws Exception {
          employeeDao.insertEmployee(newEmp);
          throw new RuntimeException();
      }
  }
  </code></pre>
  
  <p>employeeDao is </p>
  
  <pre><code>@Transactional
  public class EmployeeDao implements IEmployeeDao {
      private SessionFactory sessionFactory;
  
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
      public void insertEmployee(Employee emp) {
          sessionFactory.getCurrentSession().save(emp);
      }
  }
  </code></pre>
  
  <p>And here is a test for which the annotations work well: </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "/HibernateDaoBeans.xml" })
  @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
  @Transactional
  public class EmployeeDaoTest {
  
      @Autowired
      EmployeeDaoInterface empDao;
  
      @Test
      public void insert_record() {
         ...
         assertTrue(empDao.insertEmployee(newEmp));
      }
  </code></pre>
  
  <p>HibernateDaoBeans.xml</p>
  
  <pre><code>   ...
  &lt;bean id="employeeDao" class="Hibernate.EmployeeDao"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
      &lt;tx:annotation-driven transaction-manager="txManager"/&gt;
  
  &lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
     ...
  </code></pre>
  
  <hr>
  
  <hr>
  
  <hr>
  
  <p>**YES, I rolled back the transaction. I just added BEAN for the service... and then annotation @Transactional begin to work :-) **</p>
  
  <pre><code>&lt;bean id="service" class="main.MyClass"&gt;
      &lt;property name="employeeDao" ref="employeeDao" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Thanks all, Russia will not forget you!</p>