spring-data-neo4j ChainedTransactionManager导致javax.persistence.TransactionRequiredException

时间:2015-02-26 17:51:35

标签: hibernate spring-data spring-data-neo4j

我有一个使用spring-data-neo4j和Spring数据与Hibernate的项目。 Neo4j和Hibernate持有不同的实体,因此不需要spring-data-neo4j-cross-store。

我正在尝试使用 org.springframework.data.transaction.ChainedTransactionManager 类作为我的事务管理器,但每当我尝试将事务用于Hibernate时,我都会得到一个 javax。 persistence.TransactionRequiredException

相关applicationContext.xml:

<context:annotation-config />
<context:spring-configured/>
<context:component-scan base-package="my.package" />

<bean id="entityManagerFactoryJpa"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="my-persistence-unit-jpa" />
</bean>

<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoryJpa"/>
</bean>
<bean id="jtaTransactionManager" class="org.springframework.data.neo4j.config.JtaTransactionManagerFactoryBean">
    <constructor-arg ref="graphDatabaseService"/>
</bean>
<bean id="transactionManager" class="org.springframework.data.transaction.ChainedTransactionManager">
    <constructor-arg>
        <list>
            <ref bean="jpaTransactionManager"/>
            <ref bean="jtaTransactionManager"/>
        </list>
    </constructor-arg>
</bean>

<neo4j:repositories base-package="my.package" />
<neo4j:config base-package="my.package" graphDatabaseService="graphDatabaseService" />

<bean id="graphDatabaseService"
    class="org.springframework.data.neo4j.support.GraphDatabaseServiceFactoryBean"
    destroy-method="shutdown" scope="singleton">
    <constructor-arg value="target/graph.db" />
    <constructor-arg>
        <map>
            <entry key="enable_remote_shell" value="false" />
        </map>
    </constructor-arg>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

每当任何Hibernate事务尝试执行时,我都会收到此错误:

  

javax.persistence.TransactionRequiredException:没有任何交易   进展

如何在没有这些交易错误的情况下将spring-data-neo4j与其他Spring项目一起使用?

1 个答案:

答案 0 :(得分:0)

为了让我的项目顺利运行,我不得不更改ChainedTransactionManager bean的名称。

相关applicationContext.xml:

<context:annotation-config />
<context:spring-configured/>
<context:component-scan base-package="my.package" />

<bean id="entityManagerFactoryJpa"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="my-persistence-unit-jpa" />
</bean>

<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoryJpa"/>
</bean>
<bean id="jtaTransactionManager" class="org.springframework.data.neo4j.config.JtaTransactionManagerFactoryBean">
    <constructor-arg ref="graphDatabaseService"/>
</bean>
<bean id="chainedTransactionManager" class="org.springframework.data.transaction.ChainedTransactionManager">
    <constructor-arg>
        <list>
            <ref bean="jpaTransactionManager"/>
            <ref bean="jtaTransactionManager"/>
        </list>
    </constructor-arg>
</bean>

<neo4j:repositories base-package="my.package" />
<neo4j:config base-package="my.package" graphDatabaseService="graphDatabaseService" />

<bean id="graphDatabaseService"
    class="org.springframework.data.neo4j.support.GraphDatabaseServiceFactoryBean"
    destroy-method="shutdown" scope="singleton">
    <constructor-arg value="target/graph.db" />
    <constructor-arg>
        <map>
            <entry key="enable_remote_shell" value="false" />
        </map>
    </constructor-arg>
</bean>

<tx:annotation-driven transaction-manager="chainedTransactionManager" />

完成此更改后,我的项目开始按预期工作。显然,Spring会在后台自动创建名为“transactionManager”的事务管理器,因此,如果您将ChainedTransactionManager命名为“transactionManager”,则会出现某种冲突,而Spring不知道要用于事务的事务管理器。