Spring / Neo4J-impermanentGraphDatabase是否与事务注释兼容?

前端之家收集整理的这篇文章主要介绍了Spring / Neo4J-impermanentGraphDatabase是否与事务注释兼容? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先,我明确指出我将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注释期望发生事务,并且听起来在测试期间未创建任何事务.

我将两种不同的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,repositories"/>


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


</beans>

实际上,ImpermanentGraphDatabase似乎与Spring声明的任何事务都不相关.
我怎么能这么确定?因为我最初将生产环境中的应用程序-context.xml切换为测试(使用Neo4j REST调用),而没有触及测试代码中的任何内容,所以一切正常.

我错过了什么?

提前致谢 :)

– – – – – – – -编辑 – – – – – –

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

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

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

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

不知道这是否对您有帮助:http://www.cakesolutions.net/teamblogs/2012/03/29/neo4j-spring-data-scala/

猜你在找的Spring相关文章