我在一个服务类中使用@Trasactional,在调用多个更新方法的内部。如果我在更新中有任何错误,任何一种方法都意味着所有更新的进程都想要回滚。怎么处理这个?
我的服务类是
@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;
}
}
我的EmpDaoConvert课程是
@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班是
@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";
}
}
交易经理
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager"/>
答案 0 :(得分: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;
}
默认情况下,事务将在RuntimeException和Error上回滚,但不会在已检查的异常(业务异常)上回滚。
你捕获异常,记录它,但不要重新抛出它。所以对于春天来说 - 回滚没有例外