【问题标题】:EJB Transaction does not starts pojo transactionEJB 事务不启动 pojo 事务
【发布时间】:2017-09-14 07:42:59
【问题描述】:

我正在尝试将 EJB 事务传播到使用 @Transactional 注释的 Pojo。

在 EJB 和 Pojo 中,事务都可以正常工作,但它们似乎不能相互交流。

我在 EJB 中看不到 Pojo 的事务处于活动状态。

我的 EJB 是这样的:

@Stateless(name = "MyEJB", mappedName = "MyEJB")
@Remote(IMyEjb.class)
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class MyEJBBean implements IMyEjb {

@Autowired
private MyService myService;

@Resource
TransactionSynchronizationRegistry tsr;

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public Customer doMyStuff(String username) throws BusinessException{
      Customer customer = myService.doMyStuff(username);
    System.out.println("Pojo transaction:" + TransactionSynchronizationManager.isActualTransactionActive());
    System.out.println("Ejb transaction:" + tsr.getTransactionStatus());  
    customer = myService.doMyStuff(username);

    return customer;       

}
}

Pojo 实现如下:

@Component
public class MyService {

@Transactional(Transactional.TxType.REQUIRED)
public Customer doMyStuff(String username){
  System.out.println("Pojo transaction in object:" + TransactionSynchronizationManager.isActualTransactionActive());

   return new Customer();

}
}

我已经尝试了许多 Spring 配置,实际上我正在使用以下配置:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:orcl="http://www.springframework.org/schema/data/orcl"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/jdbc 
                       http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
  http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
                       http://www.springframework.org/schema/data/orcl 
   http://www.springframework.org/schema/data/orcl/spring-data-orcl-1.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd">

<context:component-scan base-package="com.ericsson.noc"/>
<context:annotation-config />
<jpa:repositories base-package="com.ericsson.noc" />    

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/OracleDS"
        expected-type="javax.sql.DataSource" />

 <bean id="transactionManager" 
      class="org.springframework.transaction.jta.JtaTransactionManager">
   <property name="transactionManagerName" value="java:appserver/TransactionManager"/>
   <property name="userTransactionName" value="java:comp/UserTransaction"/>
</bean>             

<bean id="entityManagerFactory" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:dataSource-ref="dataSource"
      p:persistenceUnitName="oracle">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
</bean>


<context:load-time-weaver/>
<tx:annotation-driven transaction-manager="transactionManager"  mode="aspectj" />    
 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
 </beans>

如果我运行代码,我会得到以下输出: Pojo 交易:假 Ejb 事务:0 对象中的 Pojo 交易:true。 Ejb transaction:0 表示它处于活动状态。 http://docs.oracle.com/javaee/5/api/constant-values.html#javax.transaction.Status.STATUS_ACTIVE

现在我期待一个错误的结果,或者我应该怎么做才能在 ejb 中打开 pojo 事务。

【问题讨论】:

  • 在查看您上面提到的日志语句时,我不确定是什么让您说“我无法在 EJB 中看到 Pojo 的事务处于活动状态”。 ?
  • 如果我在 EJB 中调用 TransactionSynchronizationManager.isActualTransactionActive() 它返回 false...我必须移动 Pojo 中的所有业务代码才能为 JPA 存储库生成有效事务...我会期望Spring加入EJB事务是JTA ..你成功了吗?谢谢
  • 我自己没用过。然而,在对此进行一些研究时,我发现了这个 - jira.spring.io/browse/SPR-4249 。可能对你有用。
  • 非常感谢.. 但事实并非如此。在我在 Spring 方面的情况下,事务已正确启动.. 在 EJB 方面也是如此,但它们没有加入。

标签: java spring ejb-3.0


【解决方案1】:

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) 更改为@org.springframework.transaction.annotation.Transactional(propagation = Propagation.REQUIRES_NEW)@javax.transaction.Transactional(javax.transaction.Transactional.TxType.REQUIRES_NEW)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    相关资源
    最近更新 更多