对我(JavaEE 开发新手)来说,我认为容器管理的 EntityManager 将负责回滚失败的事务,而不是 SessionContext 实例。假设以下场景...
@Stateless
public class MySessionBean implements MySessionBeanRemoteInterface {
@PersistenceContext(unitName="MYPu")
private EntityManager em;
@Resource
private SessionContext sctx;
@Override
public StackOverFlowUser createSOUser(String userName, int rep) {
try {
StackOverFlowUser su = new StackOverFlowUser();
su.setUserName(stackOverflowName);
su.setRep(rep);
su.setIsBalusC(userName.equals("BalusC");
su.setIsTheJonSkeet(userName.equals("jon skeet"));
return em.merge(su);
} catch (Exception e) {
//sctx.setRollbackOnly();
return null;
}
}
}
为什么 EntityManager 不对此负责?为什么要使用 SessionContext?