0

I'm using @Trasactional in one service class and inside that calling multiple update method. if i have any error in update any one method means all updated process want to rollback. how to handle this?

my service class is

@Service
@Transactional(rollbackFor = Exception.class)
 public class empSaveImpl implements empSave{
 @Autowired
 private EmpDaoConvert empDaoConvert;
 public void empSaveOrUpdate() throws Exception{   
   try {
    String status = empDaoConvert.empSaveOrUpdate();
} catch (Exception e) {
    e.printStackTrace();
            throw e;
}

}

my EmpDaoConvert class is

@Component
 public class EmpDaoConvert throws Exception {
    @Autowired
     private EmpDao empDao;
     public String empSaveOrUpdate() throws Exception{
             String ejb_up = empDao.saveejb_up("Y");      //ejb
         String jdbc_up = empDao.savejdbc_up('Y');   //jdbc
             String hyp_up = empDao.savehyp_up("Y");    //hyb
    return "success";
        }
 }

Dao class is

 @Repository
  public class EmpDao{
        @Autowired
    SessionFactory sessionFactory;
    public String saveejb_up(String test) throws Exception{
    ..... some update process using EJB connection.......
    }
   public String savejdbc_up(char test) throws Exception{
            .......some update process using JDBC template.......
   }
      public String savehyp_up(String test){
        SQLQuery query                    = sessionFactory.getCurrentSession().createSQLQuery("update empProfile set morning_shift= '"+test+"' where emp_id = 658954");
        query.executeUpdate();         //hibernate update
    return "success";
}
}

Transaction manager

 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager"/>
Manihtraa
  • 968
  • 3
  • 12
  • 28
  • 2
    Don't catch your exception. Als using an EJB is a remote call (generally) make sure you are using the appropriate tx managed for this (you probably need JTA for this and not a plain `HibernateTransactionManager`. – M. Deinum Jun 21 '17 at 07:53
  • 1
    @deinum. my transaction not handled rollback only when exception comes. otherwise it's working fine. i was added my transaction manager – Manihtraa Jun 21 '17 at 08:02
  • As stated don't catch the exception that breaks tx management. You also don't show all the implementations of the methods, so hard to determine if you are doing things wrong there. – M. Deinum Jun 21 '17 at 08:05
  • i removed all catch block but nothing happen. still it was not rollback @deinum – Manihtraa Jun 21 '17 at 08:24
  • @Manihtraa add @ Transactional to EmpDaoConvert and for empSaveImpl @T ransactional add rollback for – xyz Jun 21 '17 at 09:12
  • @sbjavateam which class i want to add rollback. please explain detail. i'm new in spring and hibernate – Manihtraa Jun 21 '17 at 09:19

1 Answers1

0
@Service
@Transactional   -add this -> (rollbackFor =Exception.class)
 public class empSaveImpl implements empSave{
 @Autowired
 private EmpDaoConvert empDaoConvert;
 public void empSaveOrUpdate(){   
   try {
    String status = empDaoConvert.empSaveOrUpdate();
} catch (Exception e) {
    e.printStackTrace();
   -add this -> throw e;
}

By default, a transaction will be rolling back on RuntimeException and Error but not on checked exceptions (business exceptions).

you catch exception , log it , but don't re throw it. so for spring - there is no exception for rollback

xyz
  • 5,228
  • 2
  • 26
  • 35
  • where do you get a exception (what method ) ? could you show log for pransaction manager ? add into log config ,if you use jog4j or your logger , log4j.logger.org.springframework.transaction=debug , and show the log. do you have @Transactional(propagation = Propagation.REQUIRES_NEW) for repositories ? – xyz Jun 21 '17 at 11:03
  • i was changed my code based on your answer but still not rollback. propagation is not used anywhere in my code. – Manihtraa Jun 21 '17 at 13:41
  • pls post transaction log , and what type /class exception do you get ? – xyz Jun 21 '17 at 13:47
  • Like this https://stackoverflow.com/questions/25197774/how-to-set-autocommit-to-false-in-spring-jdbc-template – xyz Jun 21 '17 at 13:58
  • Or in hibernate config file. Google hibernate autocommit – xyz Jun 21 '17 at 13:58
  • my code is working fine, but in testing time only i give invalid query to create exception in lost method on that time other two methods want to roll back but it was not rollback. it was shows SQL EXception but rollback is not done in other methods. – Manihtraa Jun 21 '17 at 14:00
  • no, i was set auto commit is false in hibernate and jdbc. – Manihtraa Jun 21 '17 at 14:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147281/discussion-between-manihtraa-and-sbjavateam). – Manihtraa Jun 21 '17 at 14:02