まず、Neo4J 2.2.0 リリースと Scala 2.10 に Spring Data を使用していることを明確にします。
アプリケーション サービスをテストするために、いくつかのテストを作成しています。
ここで、私のさまざまなファイルを呼び出し順に示します (概念的なサンプルですが、テスト済みであると正確に言います):
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 注釈がトランザクションを想定しており、テスト中にトランザクションが作成されないように聞こえるためです。
Spring 構成には 2 つの異なる application-context.xml を使用します。1 つはテスト用、もう 1 つは本番環境のアプリケーション用です。テストに使用するものは次のとおりです。
<?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 によって宣言されたトランザクションに関係していないようです....どうすればそんなに確信が持てますか? テストのコードに何も触れずに(Neo4j REST呼び出しを使用して)本番環境からのテストに最初に切り替えたためapplication-context.xml
、すべてがうまく機能しました。
私は何か見落としてますか?
前もって感謝します :)
- - - - - - - -編集 - - - - - -
実際には、Noe4j Rest 呼び出しでのみ機能します。組み込みデータベースまたは ImpermanentDatabase では、エラーが発生します。