仅在Spring Data Neo4j中将事务标记为回滚

时间:2015-01-13 22:55:48

标签: spring java-8 spring-boot spring-data-neo4j spring-transactions

我正在尝试使用Spring Boot(1.2.1)和Java 8设置一个简单的Spring Data Neo4j(3.2.1)应用程序。可以访问源here。鉴于我根据Spring References遵循了每条指令,我的CommandLineRunner失败如下。

23:36:11.762 [main] ERROR o.s.boot.SpringApplication - Application startup failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:680) [spring-boot-1.2.1.RELEASE.jar:1.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:695) [spring-boot-1.2.1.RELEASE.jar:1.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-1.2.1.RELEASE.jar:1.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:961) [spring-boot-1.2.1.RELEASE.jar:1.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:950) [spring-boot-1.2.1.RELEASE.jar:1.2.1.RELEASE]
    at com.vlkan.springdataneo4jdemo.Application.main(Application.java:39) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) [idea_rt.jar:na]
Caused by: org.neo4j.graphdb.TransactionFailureException: Failed to mark transaction as rollback only.
    at org.neo4j.kernel.TopLevelTransaction.markAsRollbackOnly(TopLevelTransaction.java:97) ~[neo4j-kernel-2.1.5.jar:2.1.5]
    at org.neo4j.kernel.TopLevelTransaction.failure(TopLevelTransaction.java:86) ~[neo4j-kernel-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.spi.v2_1.TransactionBoundQueryContext.close(TransactionBoundQueryContext.scala:65) ~[neo4j-cypher-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.spi.DelegatingQueryContext.close(DelegatingQueryContext.scala:38) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_1$spi$ExceptionTranslatingQueryContext$$super$close(ExceptionTranslatingQueryContext.scala:34) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply$mcV$sp(ExceptionTranslatingQueryContext.scala:34) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply(ExceptionTranslatingQueryContext.scala:34) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply(ExceptionTranslatingQueryContext.scala:34) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_1$spi$ExceptionTranslatingQueryContext$$translateException(ExceptionTranslatingQueryContext.scala:152) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext.close(ExceptionTranslatingQueryContext.scala:34) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.spi.DelegatingQueryContext.close(DelegatingQueryContext.scala:38) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.executionplan.ExecutionWorkflowBuilder$$anonfun$runWithQueryState$1.apply(ExecutionPlanBuilder.scala:164) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.executionplan.ExecutionWorkflowBuilder$$anonfun$runWithQueryState$1.apply(ExecutionPlanBuilder.scala:164) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.TaskCloser$$anonfun$1.apply(TaskCloser.scala:43) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.TaskCloser$$anonfun$1.apply(TaskCloser.scala:41) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251) ~[scala-library-2.10.4.jar:na]
    at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251) ~[scala-library-2.10.4.jar:na]
    at scala.collection.immutable.List.foreach(List.scala:318) ~[scala-library-2.10.4.jar:na]
    at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251) ~[scala-library-2.10.4.jar:na]
    at scala.collection.AbstractTraversable.flatMap(Traversable.scala:105) ~[scala-library-2.10.4.jar:na]
    at org.neo4j.cypher.internal.compiler.v2_1.TaskCloser.close(TaskCloser.scala:40) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$close$1.apply$mcV$sp(ClosingIterator.scala:67) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$close$1.apply(ClosingIterator.scala:67) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$close$1.apply(ClosingIterator.scala:67) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$translateException$1.apply(ClosingIterator.scala:72) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.decoratedCypherException(ClosingIterator.scala:102) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.translateException(ClosingIterator.scala:70) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.close(ClosingIterator.scala:66) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$failIfThrows$1.apply(ClosingIterator.scala:96) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.decoratedCypherException(ClosingIterator.scala:102) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.failIfThrows(ClosingIterator.scala:91) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.next(ClosingIterator.scala:44) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.PipeExecutionResult.next(PipeExecutionResult.scala:169) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.cypher.internal.compiler.v2_1.PipeExecutionResult.next(PipeExecutionResult.scala:35) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:328) ~[scala-library-2.10.4.jar:na]
    at scala.collection.convert.Wrappers$IteratorWrapper.next(Wrappers.scala:30) ~[scala-library-2.10.4.jar:na]
    at org.neo4j.cypher.internal.compiler.v2_1.PipeExecutionResult$$anon$1.next(PipeExecutionResult.scala:77) ~[neo4j-cypher-compiler-2.1-2.1.5.jar:2.1.5]
    at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47) ~[neo4j-kernel-2.1.5.jar:2.1.5]
    at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47) ~[neo4j-kernel-2.1.5.jar:2.1.5]
    at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_25]
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_25]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512) ~[na:1.8.0_25]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502) ~[na:1.8.0_25]
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_25]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_25]
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_25]
    at com.vlkan.springdataneo4jdemo.Application.run(Application.java:30) [classes/:na]
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:677) [spring-boot-1.2.1.RELEASE.jar:1.2.1.RELEASE]
    ... 10 common frames omitted
Caused by: java.lang.NullPointerException: null
    at org.neo4j.kernel.TopLevelTransaction.markAsRollbackOnly(TopLevelTransaction.java:93) ~[neo4j-kernel-2.1.5.jar:2.1.5]
    ... 57 common frames omitted
23:36:11.763 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@15b3e5b: startup date [Tue Jan 13 23:36:06 CET 2015]; root of context hierarchy
23:36:11.764 [main] INFO  o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown
Exception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:680)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:695)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
    at com.vlkan.springdataneo4jdemo.Application.main(Application.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.neo4j.graphdb.TransactionFailureException: Failed to mark transaction as rollback only.
    at org.neo4j.kernel.TopLevelTransaction.markAsRollbackOnly(TopLevelTransaction.java:97)
    at org.neo4j.kernel.TopLevelTransaction.failure(TopLevelTransaction.java:86)
    at org.neo4j.cypher.internal.spi.v2_1.TransactionBoundQueryContext.close(TransactionBoundQueryContext.scala:65)
    at org.neo4j.cypher.internal.compiler.v2_1.spi.DelegatingQueryContext.close(DelegatingQueryContext.scala:38)
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_1$spi$ExceptionTranslatingQueryContext$$super$close(ExceptionTranslatingQueryContext.scala:34)
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply$mcV$sp(ExceptionTranslatingQueryContext.scala:34)
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply(ExceptionTranslatingQueryContext.scala:34)
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext$$anonfun$close$1.apply(ExceptionTranslatingQueryContext.scala:34)
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext.org$neo4j$cypher$internal$compiler$v2_1$spi$ExceptionTranslatingQueryContext$$translateException(ExceptionTranslatingQueryContext.scala:152)
    at org.neo4j.cypher.internal.compiler.v2_1.spi.ExceptionTranslatingQueryContext.close(ExceptionTranslatingQueryContext.scala:34)
    at org.neo4j.cypher.internal.compiler.v2_1.spi.DelegatingQueryContext.close(DelegatingQueryContext.scala:38)
    at org.neo4j.cypher.internal.compiler.v2_1.executionplan.ExecutionWorkflowBuilder$$anonfun$runWithQueryState$1.apply(ExecutionPlanBuilder.scala:164)
    at org.neo4j.cypher.internal.compiler.v2_1.executionplan.ExecutionWorkflowBuilder$$anonfun$runWithQueryState$1.apply(ExecutionPlanBuilder.scala:164)
    at org.neo4j.cypher.internal.compiler.v2_1.TaskCloser$$anonfun$1.apply(TaskCloser.scala:43)
    at org.neo4j.cypher.internal.compiler.v2_1.TaskCloser$$anonfun$1.apply(TaskCloser.scala:41)
    at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
    at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251)
    at scala.collection.AbstractTraversable.flatMap(Traversable.scala:105)
    at org.neo4j.cypher.internal.compiler.v2_1.TaskCloser.close(TaskCloser.scala:40)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$close$1.apply$mcV$sp(ClosingIterator.scala:67)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$close$1.apply(ClosingIterator.scala:67)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$close$1.apply(ClosingIterator.scala:67)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$translateException$1.apply(ClosingIterator.scala:72)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.decoratedCypherException(ClosingIterator.scala:102)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.translateException(ClosingIterator.scala:70)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.close(ClosingIterator.scala:66)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator$$anonfun$failIfThrows$1.apply(ClosingIterator.scala:96)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.decoratedCypherException(ClosingIterator.scala:102)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.failIfThrows(ClosingIterator.scala:91)
    at org.neo4j.cypher.internal.compiler.v2_1.ClosingIterator.next(ClosingIterator.scala:44)
    at org.neo4j.cypher.internal.compiler.v2_1.PipeExecutionResult.next(PipeExecutionResult.scala:169)
    at org.neo4j.cypher.internal.compiler.v2_1.PipeExecutionResult.next(PipeExecutionResult.scala:35)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
    at scala.collection.convert.Wrappers$IteratorWrapper.next(Wrappers.scala:30)
    at org.neo4j.cypher.internal.compiler.v2_1.PipeExecutionResult$$anon$1.next(PipeExecutionResult.scala:77)
    at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47)
    at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at com.vlkan.springdataneo4jdemo.Application.run(Application.java:30)
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:677)
    ... 10 more
Caused by: java.lang.NullPointerException
    at org.neo4j.kernel.TopLevelTransaction.markAsRollbackOnly(TopLevelTransaction.java:93)
    ... 57 more
之前已经问过{p> TransactionFailureException: Failed to mark transaction as rollback only.相关的问题,我相信我几乎读了所有这些问题,但没有一个解决了我的问题。所以任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:2)

你的问题就在这一行

List<UserEntity> users = userService.findAll().collect(Collectors.toList());

当它收集用户服务返回的结果(=迭代来自userRepository的Result)时,事务已经关闭。

将@Transactional添加到整个方法可以解决问题

@Transactional
public void run(String[] args) {

但是如果你想在事务方法之外调用用户服务,你需要重新设计代码,以便在事务方法中急切地读取迭代器。

答案 1 :(得分:1)

Fixed the problem首先使用Application.run()@Transactional RepositoryConfiguration注释@EnableTransactionManagement