Spring@Transactional-javax.persistence.TransactionRequiredException
在下面的模式中 控制器->服务->DAO我正在尝试使服务操作@Transactional 在我的UserService.foo函数中,我调用Spring@Transactional-javax.persistence.TransactionRequiredException,spring,jta,spring-aop,transactional,Spring,Jta,Spring Aop,Transactional,在下面的模式中 控制器->服务->DAO我正在尝试使服务操作@Transactional 在我的UserService.foo函数中,我调用 Entity e = dao.find(key) e.setProperty(something) dao.update(e) 在dao.updatee的末尾有 em.flush() //EntityManager obtained by @PersistenceContext annotation (injected by spring IoC) 调
Entity e = dao.find(key)
e.setProperty(something)
dao.update(e)
在dao.updatee的末尾有
em.flush() //EntityManager obtained by @PersistenceContext annotation (injected by spring IoC)
调用flush会引发persistenceException:
javax.persistence.TransactionRequiredException No externally managed transaction is currently active for this thread
at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.throwCheckTransactionFailedException(JTATransactionWrapper.java:86)
我不知道我做错了什么,如果有任何帮助,我将不胜感激:
您可以在下面找到我的配置块:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="myPU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" id="eclipselinkVendorAdapter">
..
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
aop部分:
<aop:config>
<aop:pointcut id="userServiceOperation"
expression="execution(* org.mypackage.UserServiceImpl.*(..))"/>
<aop:advisor pointcut-ref="userServiceOperation" advice-ref="txUserServiceAdvice"/>
</aop:config>
<tx:advice id="txUserServiceAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRES_NEW"/>
<tx:method name="update*" read-only="false" propagation="REQUIRES_NEW"/>
<tx:method name="*" propagation="REQUIRES_NEW"/>
</tx:attributes>
</tx:advice>
不存在事务性批注。部署我的spring应用程序时,可以看到
[<date>] DEBUG support.DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
[<date>] DEBUG interceptor.NameMatchTransactionAttributeSource: Adding transactional method [get*] with attribute [PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT,readOnly]
[<date>] DEBUG interceptor.NameMatchTransactionAttributeSource: Adding transactional method [update*] with attribute [PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT]
[<date>] DEBUG interceptor.NameMatchTransactionAttributeSource: Adding transactional method [*] with attribute [PROPAGATION_MANDATORY,ISOLATION_DEFAULT]
关键问题就在这里
tx:注释驱动仅查找
@同一环境中bean上的事务
它是在应用程序上下文中定义的。
这意味着,如果你
投入
应用程序的WebApplicationContext
DispatcherServlet,它只检查
@您的应用程序中的事务bean
控制器,而不是您的服务。
见第15.2节“本文件”
DispatcherServlet“了解更多信息
信息
将i与服务的组件扫描放在相同的应用程序上下文中解决了问题:您将JtaTransactionManager声明为事务管理器。您是否确定您的程序运行在支持JTA的环境中,例如成熟的应用服务器JBoss、WebSphere、WebLogic等
如果您没有JTA环境,则需要改用JPATransactionManager:
是-我正在Sun的Glassfish应用程序服务器上运行此应用程序。我认为我的问题解决了:谢谢你的回复!
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>