1 package org.neo4j.driver;
2
3 import org.neo4j.driver.exception.Neo4jClientException;
4 import org.neo4j.driver.v1.*;
5
6 import java.util.Spliterator;
7 import java.util.stream.Stream;
8 import java.util.stream.StreamSupport;
9
10 import static java.util.Spliterators.spliterator;
11
12
13
14
15 public class Neo4jTransaction implements AutoCloseable {
16
17
18
19
20 private Session session;
21
22
23
24
25 private Transaction transaction;
26
27
28
29
30 public Neo4jTransaction(Session session) {
31 this.session = session;
32 this.transaction = session.beginTransaction();
33 }
34
35
36
37
38
39
40
41
42 public Stream<Record> run(String query, Value params) {
43 checkSessionAndTransaction();
44 try {
45 StatementResult result = this.transaction.run(query, params);
46 return StreamSupport.stream(spliterator(result, Long.MAX_VALUE, Spliterator.ORDERED), false);
47 } catch (Exception e) {
48 throw new Neo4jClientException(e);
49 }
50 }
51
52
53
54
55
56
57
58 public Stream<Record> run(String query) {
59 checkSessionAndTransaction();
60 return this.run(query, null);
61 }
62
63
64
65
66 public void success() {
67 checkSessionAndTransaction();
68 this.transaction.success();
69 this.transaction.close();
70 }
71
72
73
74
75 public void failure() {
76 checkSessionAndTransaction();
77 this.transaction.failure();
78 this.transaction.close();
79 }
80
81
82
83
84
85
86
87
88 public String getBookmarkId() {
89 return this.session.lastBookmark();
90 }
91
92 @Override public void close() {
93 if (this.transaction != null)
94 this.transaction.close();
95 if (this.session != null)
96 this.session.close();
97 }
98
99
100
101
102
103 private void checkSessionAndTransaction() throws Neo4jClientException {
104 if (this.session == null || !this.session.isOpen() || this.transaction == null || !this.transaction.isOpen()) {
105 throw new Neo4jClientException("Session or transaction is closed");
106 }
107 }
108
109 }