EJB Transaction不会启动pojo事务

时间:2017-04-18 12:49:22

标签: java spring ejb-3.0

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

在EJB和Pojo中,事务都有效,但它们似乎没有相互通信。

我看不到Pojo的事务在EJB中活跃。

我的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交易:0表示它是活跃的。 http://docs.oracle.com/javaee/5/api/constant-values.html#javax.transaction.Status.STATUS_ACTIVE

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

1 个答案:

答案 0 :(得分:0)

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

相关问题