无状态 EJB 中的事务感知对象



我有点困惑事务在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;

数据库连接的定义(从域.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>  

会话豆:

@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");
        }
    }
}  

来自服务器的条目.log当调用 txTest(true( 时:

[#|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 表时,记录 (2,"session.executeQuery"( 已经在那里了。
似乎session.executeQuery以某种方式提交其工作(但不是整个事务(。

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

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

但通常情况下,事务资源都会自动参与正在进行的 JTA 事务。从广义上讲,他们通过检查正在进行的交易中是否存在此类交易来做到这一点,如果确实存在,他们会在此交易中注册自己。

在 EJB 中,REQUIRES_NEW 不是唯一可以启动事务的模式 "REQUIRES"(缺省值(在客户端未启动事务的情况下也会执行此操作。

我已经解决了!!

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

ValueReadQuery query = new ValueReadQuery();

DataModifyQuery query = new DataModifyQuery();  

它就像一个魅力。

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

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

此参数指示 EclipseLink 使用缺省连接池进行读取和写入。

最新更新