Spring Neo4j事务回滚不起作用

时间:2018-08-14 10:18:19

标签: java spring spring-boot neo4j transactions

我正在使用spring neo4j(版本3.3.4)并尝试使用spring neo4j事务。我将其配置为Spring neo4j configurations。一切都很好,但是回滚无法正常进行。

例如,我尝试在一个事务中添加一个新节点和一个现有节点。它引发了RuntimeException并应回滚。但是创建第一个节点时不会回滚数据库。日志显示发生了回滚。有谁遇到过同样的问题,或者知道如何解决?预先感谢。

日志如下:

[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

1 个答案:

答案 0 :(得分:1)

已解决。如果有人遇到同样的问题,我会发布此答案。

问题是回滚JDBC事务是在mysql jdbc连接上,而不是使用neo4j事务管理器,这由以下日志指示。它仅在回滚neo4j事务期间发生,并且没有错误。

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

//            let imageToUse = PhotoArray()

//            let data = UIImagePNGRepresentation(userPickedImage) //here convert to data

            PhotoArray.sharedInstance.photosArray.append(userPickedImage)  //append converted data in array

            imageView.image = userPickedImage
//-----------------------------//
//            //begin code from firebase docs
            // Create a root reference
            let storageRef = Storage.storage().reference()

            // Create a reference to "mountains.jpg"
            let ImgRef = storageRef.child("ImgRef.jpg")

            // Create a reference to 'images/mountains.jpg'
            let userImagesRef = storageRef.child("images/userImagesRef.jpg")

            // While the file names are the same, the references point to different files
            ImgRef.name == userImagesRef.name;            // true
            ImgRef.fullPath == userImagesRef.fullPath;    // false

            // Local file you want to upload
            let localFile = URL(string: "path/to/image")!

            // Create the file metadata
            let metadata = StorageMetadata()
            metadata.contentType = "image/jpeg"

            // Upload file and metadata to the object 'images/mountains.jpg'
            let uploadTask = storageRef.putFile(from: localFile, metadata: metadata)

            // Listen for state changes, errors, and completion of the upload.
            uploadTask.observe(.resume) { snapshot in
                // Upload resumed, also fires when the upload starts
            }

            uploadTask.observe(.pause) { snapshot in
                // Upload paused
            }

            uploadTask.observe(.progress) { snapshot in
                // Upload reported progress
                let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
                    / Double(snapshot.progress!.totalUnitCount)
            }

            uploadTask.observe(.success) { snapshot in
                // Upload completed successfully
            }

            uploadTask.observe(.failure) { snapshot in
                if let error = snapshot.error as? NSError {
                    switch (StorageErrorCode(rawValue: error.code)!) {
                    case .objectNotFound:
                        // File doesn't exist
                        break
                    case .unauthorized:
                        // User doesn't have permission to access file
                        break
                    case .cancelled:
                        // User canceled the upload
                        break

                        /* ... */

                    case .unknown:
                        // Unknown error occurred, inspect the server response
                        break
                    default:
                        // A separate error occurred. This is a good place to retry the upload.
                        break
                    }
                }
            }


        imagePicker.dismiss(animated: true, completion: nil)
    }

解决方案是为neo4jTransactionManager指定一个bean名称,而不是默认名称“ transactionManager”,以免与默认的mysql事务管理器混淆。然后在事务注释器中指定事务管理器,例如@transactional(“ neo4jTransactionManager”)。