EJB,持续期间出现异常:"EntityManager must be access within a transaction"
EJB, exception during persist: "EntityManager must be access within a transaction"
我收到此错误:"EntityManager must be access within a transaction" 这对我来说很奇怪,因为我的 bean 是带有注释的标记:
@Stateless(name = "UserControllerSession")
我是这样注入 EntityManager 的:
@PersistenceContext(unitName = "app")
private EntityManager em;
这是我的方法:
@Override
@PermitAll
public String updateBlockedAndDeactivationReasonForIds(String userPk, String iban, List<String> associatedIds) {
UserLocal user = em.find(UserLocal.class, Long.valueOf(userPk));
if (user == null) {
return ("User with id: " + userPk + " does not exist ! \n");
}
user.setBlocked(true);
user.setActive(false);
user.setDeactivationReason(DeactivationReason.DEACTIVATION_MULTIPLY_IBAN_REASON);
user.setDeactivationDate(new Date());
StringBuilder message = new StringBuilder();
message.append(" IBAN " + iban + " is linked to multiple accounts: ");
for (String id : associatedIds) {
message.append(id + " ");
}
ContactJournalEntryBean cjb = new ContactJournalEntryBean("USER", user.getLogin(), new Date(), "Internet",
message.toString());
em.persist(user);
em.persist(cjb);
return "SUCCESS operation for user with id: " + userPk;
}
据我所知,EJB 方法的默认值是 Transactioanl.REQUIRED,因此默认情况下应该创建事务。另一个问题是我可以在一次交易中保留 2 个吗?感谢您的帮助
我认为默认的 REQUIRED
仅在您实际使用 @TransactionAttribute
注释时才适用。尝试将它添加到您的方法中(或者 class 如果您希望它作为所有方法的默认值)。
我收到此错误:"EntityManager must be access within a transaction" 这对我来说很奇怪,因为我的 bean 是带有注释的标记:
@Stateless(name = "UserControllerSession")
我是这样注入 EntityManager 的:
@PersistenceContext(unitName = "app")
private EntityManager em;
这是我的方法:
@Override
@PermitAll
public String updateBlockedAndDeactivationReasonForIds(String userPk, String iban, List<String> associatedIds) {
UserLocal user = em.find(UserLocal.class, Long.valueOf(userPk));
if (user == null) {
return ("User with id: " + userPk + " does not exist ! \n");
}
user.setBlocked(true);
user.setActive(false);
user.setDeactivationReason(DeactivationReason.DEACTIVATION_MULTIPLY_IBAN_REASON);
user.setDeactivationDate(new Date());
StringBuilder message = new StringBuilder();
message.append(" IBAN " + iban + " is linked to multiple accounts: ");
for (String id : associatedIds) {
message.append(id + " ");
}
ContactJournalEntryBean cjb = new ContactJournalEntryBean("USER", user.getLogin(), new Date(), "Internet",
message.toString());
em.persist(user);
em.persist(cjb);
return "SUCCESS operation for user with id: " + userPk;
}
据我所知,EJB 方法的默认值是 Transactioanl.REQUIRED,因此默认情况下应该创建事务。另一个问题是我可以在一次交易中保留 2 个吗?感谢您的帮助
我认为默认的 REQUIRED
仅在您实际使用 @TransactionAttribute
注释时才适用。尝试将它添加到您的方法中(或者 class 如果您希望它作为所有方法的默认值)。