0

我对事务在 EJB 中的工作方式有点困惑。我一直认为,当 TransactionAttribute=REQUIRED_NEW 的方法完成时,容器管理的 EJB 中的所有事务感知对象都已提交或回滚,但不幸的是,我的情况并非如此。我面前没有我的代码,所以我不能包含整个示例,但我要求的只是确认它应该如何工作的想法。
仅显示我头脑中代码的关键点:

EntityManager em; //injected
[...]
public void someEJBMethod() {
  [...]
  em.persist(someObject);
  [...]
  Session session = JpaHelper.getEntityManager(em).getActiveSession();
  [...]
  session.executeQuery(query, args);
  [...]
  if (someCondition) {
    throw new EJBException();
  }  
  [...]
}  

我的问题是,当抛出 EJBException 时,由 em.persist 引起的数据库更改被回滚,但由 session.executeQuery 引起的更改被提交。这是预期的行为吗?
我正在使用 Glassfish 3.1.2、EclipseLink 2.3.2 和 Oracle 数据库

更新(添加了测试用例)
我创建了工作测试用例来显示问题

第一个数据库对象:

create table txtest
(id number not null primary key,
 name varchar2(50) not null);

create or replace function txtest_create(p_id number, p_name varchar2) return number is
begin
  insert into txtest
  (id, name)
  values
  (p_id, p_name);

  return p_id;
end;

数据库连接的定义(来自 domain.xml)

<jdbc-connection-pool driver-classname="" datasource-classname="oracle.jdbc.pool.OracleConnectionPoolDataSource" res-type="javax.sql.ConnectionPoolDataSource" description="" name="TxTest">
  <property name="User" value="rx"></property>
  <property name="Password" value="rx"></property>
  <property name="URL" value="jdbc:oracle:thin:@test:1529:test"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="TxTest" description="" jndi-name="jdbc/TxTest"></jdbc-resource>  

持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="txTest">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/TxTest</jta-data-source>
        <class>txtest.TxTest</class>
    </persistence-unit>
</persistence>  

会话 bean:

@Stateless
public class TxTestBean implements TxTestBeanRemote, TxTestBeanLocal {
    private static Logger log = Logger.getLogger(TxTestBean.class.getName());

    @PersistenceContext(unitName="txTest")
    EntityManager em;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void txTest(boolean throwException) {
        TxTest t = new TxTest();
        t.setId(1L);
        t.setName("em.persist");
        em.persist(t);

        Session session = JpaHelper.getEntityManager(em).getActiveSession();
        log.info("session : " + String.valueOf(System.identityHashCode(session)));
        PLSQLStoredFunctionCall call = new PLSQLStoredFunctionCall();
        call.setProcedureName("txtest_create");
        call.addNamedArgument("p_id", JDBCTypes.NUMERIC_TYPE);
        call.addNamedArgument("p_name", JDBCTypes.VARCHAR_TYPE, 50);
        call.setResult(JDBCTypes.NUMERIC_TYPE);

        ValueReadQuery query = new ValueReadQuery();
        query.setCall(call);
        query.addArgument("p_id");
        query.addArgument("p_name");

        t = new TxTest();
        t.setId(2L);
        t.setName("session.executeQuery");

        List args = new ArrayList();
        args.add(t.getId());
        args.add(t.getName());

        Long result = ((Number)session.executeQuery(query, args)).longValue();

        //added to see the state of txtest table in the database before exception is thrown
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info("result=" + result.toString());

        if (throwException) {
            throw new EJBException("Test error #1");
        }
    }
}  

调用 txTest(true) 时来自 server.log 的条目:

[#|2012-05-21T12:04:15.361+0200|FINER|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.connection|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|client acquired: 21069550|#]
[#|2012-05-21T12:04:15.362+0200|FINER|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.transaction|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|TX binding to tx mgr, status=STATUS_ACTIVE|#]
[#|2012-05-21T12:04:15.362+0200|FINER|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.transaction|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|acquire unit of work: 16022663|#]
[#|2012-05-21T12:04:15.362+0200|FINEST|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.transaction|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|persist() operation called on: txtest.TxTest@11b9605.|#]
[#|2012-05-21T12:04:15.363+0200|INFO|glassfish3.1.2|txtest.TxTestBean|_ThreadID=167;_ThreadName=Thread-2;|session : 16022663|#]
[#|2012-05-21T12:04:15.364+0200|FINEST|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.query|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|Execute query ValueReadQuery()|#]
[#|2012-05-21T12:04:15.364+0200|FINEST|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.connection|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|Connection acquired from connection pool [read].|#]
[#|2012-05-21T12:04:15.364+0200|FINEST|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.connection|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|reconnecting to external connection pool|#]
[#|2012-05-21T12:04:15.365+0200|FINE|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.sql|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|
DECLARE
  p_id_TARGET NUMERIC := :1;
  p_name_TARGET VARCHAR(50) := :2;
  RESULT_TARGET NUMERIC;
BEGIN
  RESULT_TARGET := txtest_create(p_id=>p_id_TARGET, p_name=>p_name_TARGET);
  :3 := RESULT_TARGET;
END;
  bind => [:1 => 2, :2 => session.executeQuery, RESULT => :3]|#]
[#|2012-05-21T12:04:15.370+0200|FINEST|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.connection|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|Connection released to connection pool [read].|#]
[#|2012-05-21T12:04:35.372+0200|INFO|glassfish3.1.2|txtest.TxTestBean|_ThreadID=167;_ThreadName=Thread-2;|result=2|#]
[#|2012-05-21T12:04:35.372+0200|FINER|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.transaction|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|TX afterCompletion callback, status=ROLLEDBACK|#]
[#|2012-05-21T12:04:35.372+0200|FINER|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.transaction|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|release unit of work|#]
[#|2012-05-21T12:04:35.372+0200|FINER|glassfish3.1.2|org.eclipse.persistence.session.file://txTest/_txTest.connection|_ThreadID=167;_ThreadName=Thread-2;ClassName=null;MethodName=null;|client released|#]
[#|2012-05-21T12:04:35.373+0200|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=167;_ThreadName=Thread-2;|EJB5184:A system exception occurred during an invocation on EJB TxTestBean, method: public void txtest.TxTestBean.txTest(boolean)|#]
[#|2012-05-21T12:04:35.373+0200|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=167;_ThreadName=Thread-2;|javax.ejb.EJBException: Test error #1  

最让我惊讶的是,当我在这 20 秒内检查 txtest 表时。sleep 记录(2,“session.executeQuery”)已经存在。
似乎 session.executeQuery 以某种方式提交了它的工作(但不是整个事务)。

有人可以解释这种行为吗?

4

2 回答 2

4

我不确定JpaHelper.getEntityManager(em).getActiveSession();应该做什么,但似乎这不会返回容器管理的实体管理器。根据它的具体实现方式,它可能不参与正在进行的(JTA)事务。

但通常情况下,事务资源都会自动参与正在进行的 JTA 事务。概括地说,他们通过检查是否存在正在进行的交易来做到这一点,如果确实存在,他们就在该交易中注册自己。

在 EJB 中,REQUIRES_NEW不是唯一可以启动事务“REQUIRES”(默认)的模式在客户端没有启动事务的情况下也会这样做。

于 2012-05-19T21:09:55.820 回答
1

我已经解决了!!!

事实证明,EclipseLink 使用读取连接池来处理读取查询(显然这种池使用自动提交,甚至根本不使用事务)和默认连接池用于数据修改查询。所以我必须做的是改变:

ValueReadQuery query = new ValueReadQuery();

进入

DataModifyQuery query = new DataModifyQuery();  

它就像一个魅力。

更新
DataModifyQuery不允许获取函数的结果。它返回修改的行数。所以我回到了ValueReadQuerypersistance.xml中但使用了配置参数

<property name="eclipselink.jdbc.exclusive-connection.mode" value="Always"/>  

此参数告诉 EclipseLink 使用默认连接池进行读取和写入。

于 2012-05-21T11:33:14.080 回答