[英]XA Datasource does not create a transaction
I have an XA Datasource setup in Wildfly 8.2. 我在Wildfly 8.2中有一个XA数据源设置。 It all works fine but when I call:
一切正常,但是当我打电话时:
sql.query("LOCK TABLE table_name IN EXCLUSIVE MODE").execute();
I get an exception showing that Wildfly has not created a transaction: 我得到一个异常,表明Wildfly 尚未创建事务:
org.postgresql.util.PSQLException: ERROR: LOCK TABLE can only be used in transaction blocks
org.postgresql.util.PSQLException:错误:LOCK TABLE只能在事务块中使用
If I manually create the transaction with a BEGIN
and a COMMIT
around my lock query, everything works as expected - but I would like Wildfly to do that for me automatically. 如果我在锁定查询周围使用
BEGIN
和COMMIT
手动创建事务,那么一切都会按预期进行-但我希望Wildfly自动为我执行此操作。
Why doesn't Wildfly create a transaction automatically and what do I need to do to fix it? Wildfly为什么不自动创建交易,我需要怎么做才能解决该问题?
For reference, the code is called in a method like: 作为参考,该代码在以下方法中调用:
@RequestScoped
@Path("abc")
public class Controller {
@PUT
public Response m(Object data) {
//HERE
}
If you want wildfly to create a transaction for you, use a ejb bean.. For your example, injecting a @Stateless session bean should work (since you are using RequestScoped, and stateless session beans have a similar lifecycle) 如果希望Wildfly为您创建事务,请使用ejb Bean。例如,注入@Stateless会话Bean应该可以工作(因为您使用的是RequestScoped,而无状态会话Bean具有相似的生命周期)
By default, each method of the stateless ejb will create a transaction for you, or if the client already has a current transaction, it will use it. 默认情况下,无状态ejb的每种方法都会为您创建一个事务,或者如果客户端已经具有当前事务,它将使用它。
Code example: 代码示例:
@RequestScoped
@Path("abc")
public class Controller {
@EJB
private YourStatelessEJB statelessBean;
@PUT
public Response m(Object data) {
//HERE
statelessBean.doSomething(data)
}
@Stateless
@LocalBean
public class YourStatelessEJB {
@PersistenceContext
private EntityManager em;
public YourStatelessEJB() {
}
public void doSomething(Object data) {
// here you already have a transaction, created by the ejbcontainer
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.