即使使用 @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) 也不会自动创建事务
我的 Java EE 6 应用程序中有这样的托管 bean:
@Named
@RequestScoped
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class RegistrationBean implements Serializable {
@PersistenceContext
EntityManager em;
public String doRegistration() {
MyEntity entity = new MyEntity();
em.persist(entity);
return "view";
}
}
据我了解 @TransactionAttribute,应该自动创建新事务。但显然不是,因为我遇到了异常: javax.persistence.TransactionRequiredException: EntityManager 必须在事务内访问
我的持久性单元有 transaction-type="JTA"
属性。我正在使用 JBoss 6 cr1。
I have such managed bean in my Java EE 6 app:
@Named
@RequestScoped
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class RegistrationBean implements Serializable {
@PersistenceContext
EntityManager em;
public String doRegistration() {
MyEntity entity = new MyEntity();
em.persist(entity);
return "view";
}
}
As far as I understand @TransactionAttribute, new transaction should be automatically created. But apparently it is not, because I am getting an exception: javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
My persistence unit has transaction-type="JTA"
attribute. I'm using JBoss 6 cr1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尝试做的事情并不完全正确。您定义的是一个普通的 CDI bean,它本身不支持 @TransactionAttribute 注释。此注释用于 EJB bean,您可以通过使用 @Stateless 注释来获得该 EJB bean。
请注意,在这种情况下,您不一定需要使用 TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) 注释。默认情况下,当您使用 @Stateless 注释您的 bean 时,您已经获得了 TransactionAttributeType.REQUIRES,这在大多数情况下都是您想要的。
What you are trying to do is not exactly correct. What you defined is a plain CDI bean, which does not natively supports the @TransactionAttribute annotation. This annotation is for use with an EJB bean, which you'll get by using the @Stateless annotation.
Note that in this case you'll don't necessarily need to use the TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) annotation. By default you are already getting TransactionAttributeType.REQUIRES when annotating your bean with @Stateless, which in most cases is what you want.
@TransactionAttribute 是一个 EJB 注释。我不认为 CDI 容器提供像 EJB 容器那样的事务管理。
针对您的情况的一种解决方案是将数据库访问功能放入无状态 EJB 中并将其注入 RegistrationBean。话虽这么说,您不需要指定 TransactionAttributeType.REQUIRES_NEW 即可获得自动事务处理。隐式默认值 REQUIRES 应该足够了,除非您计划从运行另一个事务的方法调用 EJB 方法,并且希望被调用的方法在与原始事务不同的单独事务中运行。
@TransactionAttribute is an EJB annotation. I don't believe the CDI container provides transaction management like the EJB container does.
One solution for your case would be to put the database access functionality into a stateless EJB and inject that into RegistrationBean. That being said, you don't need to specify TransactionAttributeType.REQUIRES_NEW in order to get automatic transaction handling. The implicit default, REQUIRES, should suffice unless you are planning on calling the EJB method from method running another transaction, and you want the called method to run in a separate transaction from the original.
默认情况下,CDI 不支持容器管理的事务。然而,使用 Seam 3 Persistence 模块,您可以将 CMT 支持添加到您的应用程序中。
Seam Persistence 文档在这里 --> 链接
首先,添加Seam Persistence jars 到您的项目:
接下来,在您的 beans.xml 中启用声明式事务管理
现在,根据文档,您应该能够在托管 bean 中使用 @TransactionAttribute 注释,就像您使用然而,我无法让它工作。但是,您可以在类或方法上使用 @Transactional 注释。这是Seam Persistence 的等效项并且工作正常。
希望这有帮助。有任何问题尽管问。
By default, CDI does not support container managed transactions. However, using the Seam 3 Persistence module, you can add CMT support to your application.
The Seam Persistence documentation is here --> Link
Firstly, add the Seam Persistence jars to your project:
Next, enable declarative transaction management in your beans.xml
Now, according to the documentation, you should be able to use the @TransactionAttribute annotations with your managed bean like you would with an EJB, however, I couldn't get that to work. Instead, however, you can use the @Transactional annotation on your class or method. This is the Seam Persistence equivalent and works fine.
Hope this helps. Any questions, just ask.
TransactionAttributeType.REQUIRE*D*
TransactionAttributeType.REQUIRE*D*