我正在从服务层调用2种不同的dao方法。
第二种方法抛出空指针异常(列“地址”不能为空),但第一种方法完成的任务无法回滚。
服务层
package com.yetistep.dboy.business.impl;
@Service
@Transactional
public class TestServiceImpl implements TestService {
@Autowired
TestDaoService testDao;
@Autowired
CustomerDaoService customerService;
@Override
@Transactional //I supposed propagation control from aop config
public Boolean saveMulTransactions(User user, Customer customer) throws Exception {
testDao.saveUser(user);
customerService.saveCustomer(customer);
return true;
}
}
//Dao层
public class TestDaoServiceImpl implements TestDaoService {
@Autowired
SessionFactory sessionFactory;
Session session = null;
Transaction tx = null;
@Override
public boolean saveUser(User user) throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
return false;
}
}
public class CustomerDaoServiceImpl implements CustomerDaoService{
@Autowired
SessionFactory sessionFactory;
Session session = null;
Transaction tx = null;
@Override
public boolean saveCustomer(Customer customer) throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(customer);
tx.commit();
session.close();
return false;
}
}
// Controller
public @ResponseBody
ServiceResponse createAdmin(@RequestBody User user) throws Exception{
log.debug("Saving User to Database");
ServiceResponse serviceResponse = null;
try {
Customer customer = new Customer();
customer.setName("Jeka");
customer.setContact("87897898788978979");
customer.setContact("Ktm");
testService.saveMulTransactions(user, customer);
serviceResponse = new ServiceResponse("User Added Successfully!!!");
} catch (Exception e) {
e.printStackTrace();
}
return serviceResponse;
}
/xml 中的事务配置
//数据源和hibernate配置在这里......
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- MUST have transaction manager, using aop and aspects -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userServicePointCut"
expression="execution(* com.yetistep.dboy.business.impl.*ServiceImpl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointCut" />
</aop:config>
我认为,如果 impl 包的任何方法抛出错误,则应该执行回滚 有什么问题请通过回答建议我吗?
最佳答案
为什么它应该回滚...您自己打开新 session 并自己管理事务。 Spring 如何知道这一点。
你的 daos 在很多方面都是错误的。
- 切勿将 session /事务存储在实例变量中
- 切勿使用
openSession
使用 Spring 管理事务时 - 如果您希望由容器管理事务,则切勿管理自己的事务。
所以简而言之,修复你的 daos。
public class CustomerDaoServiceImpl implements CustomerDaoService{
@Autowired
SessionFactory sessionFactory;
@Override
public boolean saveCustomer(Customer customer) throws Exception {
Session session = sessionFactory.getCurrentSession();
session.save(customer);
return false;
}
}
这就是您所需要的(当然这适用于您所有的 daos)。
另外你的配置也是错误的,你只需要 <tx:annotation-driven />
你不需要<tx:advice />
和<aop:config />
删除那些。
所有这些都在the reference guide中进行了解释。 .
关于java - Spring Hibernate 事务回滚不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26924569/