View Javadoc
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   * Client transaction.
14   */
15  public class Neo4jTransaction implements AutoCloseable {
16  
17      /**
18       * Neo4j's underlying session.
19       */
20      private Session session;
21  
22      /**
23       * Neo4j's underlying transaction
24       */
25      private Transaction transaction;
26  
27      /**
28       * Default constructor with a session.
29       */
30      public Neo4jTransaction(Session session) {
31          this.session = session;
32          this.transaction = session.beginTransaction();
33      }
34  
35      /**
36       * Execute the given cypher query with its parameters.
37       *
38       * @param query  Cypher query
39       * @param params Query's parameters
40       * @return A Stream of record
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       * Execute the given cypher query without parameters.
54       *
55       * @param query Cypher query
56       * @return A Stream of record
57       */
58      public Stream<Record> run(String query) {
59          checkSessionAndTransaction();
60          return this.run(query, null);
61      }
62  
63      /**
64       * Commit and close the current transaction.
65       */
66      public void success() {
67          checkSessionAndTransaction();
68          this.transaction.success();
69          this.transaction.close();
70      }
71  
72      /**
73       * Rollback and close the current transaction.
74       */
75      public void failure() {
76          checkSessionAndTransaction();
77          this.transaction.failure();
78          this.transaction.close();
79      }
80  
81      /**
82       * Get the last bookmarkId.
83       * It's only works when the underline transaction has been close.
84       * So you have to call {@link #failure()} or {@link #success()} before.
85       *
86       * @return the latest bookmarkId of this session.
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      * Check if one the underlying session or transaction is closed or not.
101      * If so, a {@link Neo4jClientException} is thrown.
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 }