Java EJB事务不启动pojo事务
我试图将EJB事务传播到带有@Transactional注释的Pojo 在EJB和Pojo中,事务都可以工作,但它们似乎彼此不通信 我在EJB中看不到Pojo的事务处于活动状态 我的EJB类似于:Java EJB事务不启动pojo事务,java,spring,ejb-3.0,Java,Spring,Ejb 3.0,我试图将EJB事务传播到带有@Transactional注释的Pojo 在EJB和Pojo中,事务都可以工作,但它们似乎彼此不通信 我在EJB中看不到Pojo的事务处于活动状态 我的EJB类似于: @Stateless(name = "MyEJB", mappedName = "MyEJB") @Remote(IMyEjb.class) @Interceptors(SpringBeanAutowiringInterceptor.class) public class MyEJBBean impl
@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事务:false
Ejb事务:0
对象中的Pojo事务:true。
Ejb事务:0表示它处于活动状态。
现在我期待一个错误的结果,或者我应该怎么做才能在ejb中打开pojo事务。将
@TransactionAttribute(TransactionAttributeType.REQUIRES\u NEW)
更改为@org.springframework.transaction.annotation.Transactional(propagation=propagation.REQUIRES\u NEW)
或@javax.Transactional(javax.transaction.Transactional.TxType.REQUIRES_NEW)
在查看您上面提到的日志语句时,我不确定您为什么会说“我看不到EJB中Pojo的事务处于活动状态。”?如果调用TransactionSynchronizationManager.isActualTransactionActive()在EJB中,它返回false…我必须移动Pojo中的所有业务代码,才能为JPA存储库创建有效的事务…我希望Spring以JTA的身份加入EJB事务。你成功了吗?谢谢我自己没有使用过它。但是在做一些研究时,我发现-。可能对你有用。谢谢有很多,但事实并非如此。在我的情况下,在Spring端,事务已经正确启动。在EJB端,它们也没有连接。