In Spring, a method that is annotated with @Transactional
will obtain a new transaction if there isn t one already, but I noticed that a transactional method does not obtain any transaction if it is called from a non-transactional one. Here s the code.
@Component
public class FooDao {
private EntityManager entityManager;
@PersistenceContext
protected void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Transactional
public Object save(Object bean) {
return this.entityManager.merge(bean);
}
public Object saveWrap(Object bean) {
return save(bean);
}
}
@Component
public class FooService {
private FooDao fooDao;
public void save(Object bean) {
this.fooDao.saveWrap(bean); // doesn t work.
this.fooDao.save(bean); // works
}
}
<>代码>saveWrap(>)是一种固定方法,称为<代码>save(>,即交易,但saveWrap(<>
得起任何改动。
I m 采用第3和第3版。 我在这里做了什么错误? 感谢。