首先,我确切地说我将 Spring Data 用于 Neo4J 2.2.0-release 和 Scala 2.10。
我正在构建一些测试以测试我的应用程序服务。
这是我按调用顺序排列的不同文件(我准确地说它是一个概念样本,但经过测试):
会议服务.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 注释需要一个事务,并且听起来在测试期间没有创建事务。
我为 Spring 配置使用了两种不同的 application-context.xml,一种用于我的测试,另一种用于生产中的应用程序。用于测试的一个如下:
<?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,会发生错误。