我有一个Service类,如下所示:
@Transactional
@Component(value = "userServiceImpl")
public class UserServiceImpl implements UserService
{
@Autowired
private UserDao userDaoiBatis;
public boolean X()
{
// Call UserDao.A and UserDao.B in transaction.
}
}
和Dao类如下:
@Transactional
@Component(value = "userDaoiBatis")
public class UserDaoiBatis extends SqlMapClientDaoSupport implements UserDao
{
@Autowired
private void injectSqlMapClient(SqlMapClient sqlMapClient)
{
setSqlMapClient(sqlMapClient);
}
@Override
public boolean A()
{
throw new RuntimeException("Not implemented");
}
@Override
public boolean B()
{
throw new RuntimeException("Not implemented");
}
}
如何在UserServiceImpl.Z()中将UserDaoiBatis.A()和UserDaoiBatis.B()作为事务的一部分调用???
答案 0 :(得分:2)
您的设置看起来是正确的。该事务将由UserServiceImpl
启动,然后对UserDaoiBatis
的两次调用都将继承现有事务(@Transactional
默认为REQUIRES传播)。您可以通过在AbstractPlatformTransactionManager
类上启用调试级别日志记录来确认这一点。
请注意,如果A()
抛出异常,则tx将回滚。