【问题标题】:Transaction aware objects in stateless EJB无状态 EJB 中的事务感知对象
【发布时间】:2012-05-19 20:40:01
【问题描述】:

我对事务在 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>  

persistence.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 以某种方式提交了它的工作(但不是整个事务)。

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

【问题讨论】:

    标签: jpa transactions ejb eclipselink


    【解决方案1】:

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

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

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

    【讨论】:

    • JpaHelper.getEntityManager(em).getActiveSession() 是 EclipseLink 特定的方法,在这种情况下返回 UnitOfWork 对象,它(根据日志判断)与实体管理器使用的 UnitOfWork 完全相同。所以现在当你确认它应该完全按照我的预期工作时,我会尝试准备真实的测试用例来说明这个问题。
    【解决方案2】:

    我已经解决了!!!

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

    ValueReadQuery query = new ValueReadQuery();
    

    进入

    DataModifyQuery query = new DataModifyQuery();  
    

    它就像一个魅力。

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

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

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

    【讨论】:

    • 您也可以使用 UnitOfWork.beginEarlyTransaction 或 em.unwrap(JpaEntityManager).createQuery(plsqlCall) 来获取 JPA 查询。
    猜你喜欢
    • 2015-10-24
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多