【问题标题】:Spring/Neo4J - Is impermanentGraphDatabase compatible with Transaction Annotation?Spring/Neo4J - impermanentGraphDatabase 与事务注释兼容吗?
【发布时间】:2013-03-07 11:05:40
【问题描述】:

首先,我将 Spring Data 用于 Neo4J 2.2.0-release 和 Scala 2.10。

我正在构建一些测试来测试我的应用程序服务。

这是我按调用顺序排列的不同文件(我准确地说它是一个概念样本,但经过测试):

MeetingServices.scala

@Service
class MeetingServices {


  @Transactional
    def save(meeting: Meeting, creator: User): ValidationNel[MeetingFailure, Meeting] = {
      try {
        meetingRepository.save(meeting, creator).successNel[MeetingFailure]
      } catch {
        case e: Throwable => MeetingFailure("Fail to create the Meeting " + meeting + "for the user :" + creator, Some(e)).failNel[Meeting]
      }
    }
}

MeetingRepository.scala 中的保存方法:

def save(meeting: Meeting, creator: User): Meeting = { //no need to declare a transaction since this method call is wrapped inside the previous save() method of MeetingServices 
    creator.participateIn(meeting)                                           
    meeting.creator = creator
    meetingRepository.save(meeting)
}

有趣的 User.class 摘录:

@RelatedTo(`type` = "PARTICIPATES_IN")
  val meetings: java.util.Set[Meeting] = new java.util.HashSet[Meeting]()


def participateIn(meeting: Meeting) = {
    meeting.creator = this   // since bidirectional relation
    meetings.add(meeting)    //#needs_transaction 
}

那么问题出在哪里?:

在生产中,一切正常。交易发生,我的会议持续存在并与其创建者(用户 neo4j-relationship)相关联。 但在我的环境测试中,我得到了这个:NotInTransactionException。 为什么 ?因为在用#needs_transaction 注释的行(在上面的摘录中),Relationship annotation 需要一个事务,并且听起来在测试期间没有创建事务。

我使用两个不同的 application-context.xml 进行 Spring 配置,一个用于我的测试,第二个用于生产中的应用程序。 用于测试的如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/neo4j
       http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


    <neo4j:config graphDatabaseService="graphDatabaseService"/>


    <bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" />




    <neo4j:repositories base-package="repositories"/>


    <context:spring-configured/>


    <context:annotation-config/>
    <context:component-scan base-package="controllers, services, models,repositories"/>


    <tx:annotation-driven mode="aspectj"/>


</beans>

我在生产中使用的那个:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/neo4j
       http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">




    <neo4j:config graphDatabaseService="graphDatabaseService"/>
    <bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
        <constructor-arg index="0" value="http://myUrl/db/data/"/>
        <constructor-arg index="1" value="111111"/>
        <constructor-arg index="2" value="111111"/>
    </bean>




    <neo4j:repositories base-package="repositories" />


    <context:spring-configured/>


    <context:annotation-config/>
    <context:component-scan base-package="controllers, applicationservices, models,repositories"/>


    <tx:annotation-driven mode="aspectj"/>


</beans>

ImpermanentGraphDatabase 似乎真的不关心 Spring 声明的任何事务...... 我怎么能这么肯定?因为我最初将application-context.xml 切换为来自生产环境的测试(使用Neo4j REST 调用),而没有触及我的测试代码中的任何内容,并且一切正常。

我错过了什么吗?

提前致谢:)

----------------编辑------------

实际上,它仅适用于 Noe4j Rest 调用。对于嵌入式数据库或 ImpermanentDatabase,会发生错误。

【问题讨论】:

    标签: spring neo4j spring-transactions spring-data-neo4j


    【解决方案1】:

    REST API 中没有事务,每个 http 请求都有自己的事务。 java-rest-binding 使用 ?Null-Transaction(Manager)`。

    您的服务是如何实例化/注入的?以及如何以及在哪里使用它?也许您可以在 github 上分享一个单元测试失败的示例项目?

    您的配置看起来是正确的,我认为这与您从 Spring 注入服务的方式有关,因此无法正确应用事务方面。

    不知道对你有没有帮助:http://www.cakesolutions.net/teamblogs/2012/03/29/neo4j-spring-data-scala/

    【讨论】:

    猜你喜欢
    • 2015-07-30
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    相关资源
    最近更新 更多