我从服务层调用 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 Layer
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;
}
}
//控制器
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中的交易配置
//数据源和hiibernate配置在这里.....
<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包的方法抛出错误,那么应该执行回滚 有什么问题请建议我回答?
答案 0 :(得分:3)
为什么要回滚......你正在自己开设新会议并自己管理交易。春天应该怎么知道呢。
你的daos在多方面都是错误的。
openSession
所以简而言之,修复你的道具。
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;
}
}
你需要的是什么(当然这适合你所有的人)。
另外,您的配置也有误,您只需要<tx:annotation-driven />
<tx:advice />
而<aop:config />
和{{1}}删除它们。
所有这些都在the reference guide中解释。