Spring @Transactional-javax.persistence.TransactionRequiredException [英] Spring @Transactional - javax.persistence.TransactionRequiredException
问题描述
在以下模式中
控制器->服务-> DAO我正在尝试进行服务操作@Transactional
Controller -> Service -> DAO I'm trying to make Service operations @Transactional
在我调用的UserService.fooFunction()中
in my UserService.fooFunction() i call
Entity e = dao.find(key)
e.setProperty(something)
dao.update(e)
在dao.update(e)的末尾有
in dao.update(e) at the end there is
em.flush() //EntityManager obtained by @PersistenceContext annotation (injected by spring IoC)
调用flush()会引发persistenceException:
Calling flush() throws a 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)
我对我做错了什么的想法不多,任何帮助将不胜感激:)
I'm running low on ideas what I've done wrong, any help would be appreciated :)
您可以在下面找到我的配置文件:
You can find chunks of my configuration below:
<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: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应用程序时,可以看到
no transactional annotations are present. When deploying my spring app one can see
[<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:注释驱动仅寻找 @Transactional在同一个bean上 在其中定义的应用程序上下文. 这意味着,如果您 放在 WebApplicationContext的一个 DispatcherServlet,它仅检查 @您的@transactional bean 控制器,而不是您的服务. 请参见第15.2节 DispatcherServlet"以了解更多 信息.
tx:annotation-driven only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services. See Section 15.2, "The DispatcherServlet" for more information.
在与服务的组件扫描相同的应用程序上下文中放置此问题即可:)
Putting i the same application context as component-scan for services solved the problem :)
这篇关于Spring @Transactional-javax.persistence.TransactionRequiredException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!