F.Z.Yang
F.Z.Yang

Reputation: 66

Spring Neo4j Transaction Rollback doesn't work

I'm using spring neo4j (version 3.3.4) and trying to use spring neo4j transactions. I configured it as Spring neo4j configurations. Everything was fine, but the rollback cannot work.

For example, I tried to add one new node and and one existing node in one transaction. It raised a RuntimeException and should rollback. But the first node is created without rolling back in the database. The log shows that rolling back has happened. Does anyone had the same problem or know how to figure it out? Thanks in advance.

The log is as follows:

[2018-08-14 17:48:20.845][http-nio-8888-exec-4] DEBUG o.s.d.n.t.Neo4jTransactionManager - Rolling back Neo4j transaction [org.neo4j.ogm.drivers.bolt.transaction.BoltTransaction@421cb02f] on Session [org.neo4j.ogm.session.Neo4jSession@2f8c5762]
[2018-08-14 17:48:20.845][http-nio-8888-exec-4] DEBUG o.n.o.d.b.t.BoltTransaction - Rolling back native transaction: org.neo4j.driver.internal.ExplicitTransaction@5e10b6b3
[2018-08-14 17:48:20.846][http-nio-8888-exec-4] DEBUG o.neo4j.ogm.transaction.Transaction - Thread 54: Rollback transaction extent: 0
[2018-08-14 17:48:20.846][http-nio-8888-exec-4] DEBUG o.neo4j.ogm.transaction.Transaction - Thread 54: Rolled back
[2018-08-14 17:48:20.846][http-nio-8888-exec-4] DEBUG o.neo4j.ogm.transaction.Transaction - Thread 54: Close transaction extent: 0
[2018-08-14 17:48:20.846][http-nio-8888-exec-4] DEBUG o.neo4j.ogm.transaction.Transaction - Thread 54: Closing transaction
[2018-08-14 17:48:20.846][http-nio-8888-exec-4] DEBUG o.s.d.n.t.Neo4jTransactionManager - Closing Neo4j Session [org.neo4j.ogm.session.Neo4jSession@2f8c5762] after transaction
[2018-08-14 17:48:20.846][http-nio-8888-exec-4] DEBUG o.s.d.n.t.Neo4jTransactionManager - Resuming suspended transaction after completion of inner transaction
[2018-08-14 17:48:20.848][http-nio-8888-exec-4] ERROR c.s.k.c.s.i.GraphOperationServiceImpl - Error in committing directives: Node(19354) already exists with label `Product` and property `prodName` = 'test1'
org.neo4j.driver.v1.exceptions.ClientException: Node(19354) already exists with label `Product` and property `prodName` = 'test1'
[2018-08-14 17:48:20.849][http-nio-8888-exec-4] DEBUG o.s.j.d.DataSourceTransactionManager - Initiating transaction rollback
[2018-08-14 17:48:20.849][http-nio-8888-exec-4] DEBUG o.s.j.d.DataSourceTransactionManager - Rolling back JDBC transaction on Connection [HikariProxyConnection@1121924585 wrapping com.mysql.jdbc.JDBC4Connection@41813449]
[2018-08-14 17:48:20.850][http-nio-8888-exec-4] DEBUG o.s.j.d.DataSourceTransactionManager - Releasing JDBC Connection [HikariProxyConnection@1121924585 wrapping com.mysql.jdbc.JDBC4Connection@41813449] after transaction
[2018-08-14 17:48:20.850][http-nio-8888-exec-4] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource

Upvotes: 2

Views: 822

Answers (1)

F.Z.Yang
F.Z.Yang

Reputation: 66

It is solved. I post this answer in case someone else has the same problem.

The problem is that Rolling back JDBC transaction is on a mysql jdbc connection instead of using neo4j transaction manager, which is indicated by the following log. It happens only during rolling back the neo4j transaction with no error.

[2018-08-14 17:48:20.849][http-nio-8888-exec-4] DEBUG o.s.j.d.DataSourceTransactionManager - Rolling back JDBC transaction on Connection [HikariProxyConnection@1121924585 wrapping com.mysql.jdbc.JDBC4Connection@41813449]

The solution is to specify a bean name for neo4jTransactionManager instead of the default name "transactionManager", so that it won't be confusing with the default mysql transaction manager. And then specify the transaction manager in the transactional annotator e.g. @transactional("neo4jTransactionManager").

Upvotes: 1

Related Questions