2

在以下架构中

Controller -> Service -> DAO 我正在尝试进行服务操作@Transactional

在我的 UserService.fooFunction() 我打电话

Entity e = dao.find(key)
e.setProperty(something)
dao.update(e)

在 dao.update(e) 最后有

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]
4

2 回答 2

4

关键问题在这里

tx:annotation-driven 仅在定义它的同一应用程序上下文中的 bean 上查找 @Transactional。这意味着,如果您为 DispatcherServlet 放入 WebApplicationContext,它只会检查控制器中的 @Transactional bean,而不是您的服务。有关详细信息,请参阅第 15.2 节,“DispatcherServlet”。

将我与服务组件扫描相同的应用程序上下文解决了这个问题:)

于 2011-03-07T13:57:13.167 回答
1

您声明JtaTransactionManager为您的事务管理器。您确定您的程序在支持 JTA 的环境中运行,例如成熟的应用程序服务器(JBoss、WebSphere、WebLogic 等)吗?

如果您没有 JTA 环境,则需要JPATransactionManager改用:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
于 2011-03-07T14:52:55.453 回答