19
19
package org .neo4j .driver .v1 ;
20
20
21
21
import java .util .concurrent .CompletionStage ;
22
+ import java .util .concurrent .Executor ;
23
+ import java .util .function .Function ;
22
24
23
25
import org .neo4j .driver .v1 .util .Resource ;
24
26
42
44
* the graph seen by the previous query. For more on causal consistency, see
43
45
* the Neo4j clustering manual.
44
46
* <p>
45
- * Typically, a session will wrap a TCP connection. Such a connection will be
46
- * acquired from a connection pool and released back there when the session is
47
- * destroyed. One connection can therefore be adopted by many sessions,
48
- * although by only one at a time. Application code should never need to deal
49
- * directly with connection management.
47
+ * Typically, a session will acquire a TCP connection to execute query or
48
+ * transaction. Such a connection will be acquired from a connection pool
49
+ * and released back there when query result is consumed or transaction is
50
+ * committed or rolled back. One connection can therefore be adopted by many
51
+ * sessions, although by only one at a time. Application code should never need
52
+ * to deal directly with connection management.
50
53
* <p>
51
54
* A session inherits its destination address and permissions from its
52
- * underlying connection. This means that one session may only ever target one
53
- * machine within a cluster and does not support re-authentication. To achieve
54
- * otherwise requires creation of a separate session.
55
+ * underlying connection. This means that for a single query/transaction one
56
+ * session may only ever target one machine within a cluster and does not
57
+ * support re-authentication. To achieve otherwise requires creation of a
58
+ * separate session.
55
59
* <p>
56
60
* Similarly, multiple sessions should be used when working with concurrency;
57
- * session implementations are generally not thread safe.
61
+ * session implementations are not thread safe.
58
62
*
59
63
* @since 1.0
60
64
*/
@@ -65,6 +69,9 @@ public interface Session extends Resource, StatementRunner
65
69
* most one transaction may exist in a session at any point in time. To
66
70
* maintain multiple concurrent transactions, use multiple concurrent
67
71
* sessions.
72
+ * <p>
73
+ * This operation works the same way as {@link #beginTransactionAsync()} but blocks until transaction is actually
74
+ * started.
68
75
*
69
76
* @return a new {@link Transaction}
70
77
*/
@@ -84,34 +91,101 @@ public interface Session extends Resource, StatementRunner
84
91
@ Deprecated
85
92
Transaction beginTransaction ( String bookmark );
86
93
94
+ /**
95
+ * Begin a new <em>explicit {@linkplain Transaction transaction}</em>. At
96
+ * most one transaction may exist in a session at any point in time. To
97
+ * maintain multiple concurrent transactions, use multiple concurrent
98
+ * sessions.
99
+ * <p>
100
+ * This operation is asynchronous and returns a {@link CompletionStage}. This stage is completed with a new
101
+ * {@link Transaction} object when begin operation is successful. It is completed exceptionally if
102
+ * transaction can't be started.
103
+ * <p>
104
+ * Returned stage can be completed by an IO thread which should never block. Otherwise IO operations on this and
105
+ * potentially other network connections might deadlock. Please do not chain blocking operations like
106
+ * {@link #run(String)} on the returned stage. Driver will throw {@link IllegalStateException} when blocking API
107
+ * call is executed in IO thread. Consider using asynchronous calls throughout the chain or offloading blocking
108
+ * operation to a different {@link Executor}. This can be done using methods with "Async" suffix like
109
+ * {@link CompletionStage#thenApplyAsync(Function)} or {@link CompletionStage#thenApplyAsync(Function, Executor)}.
110
+ *
111
+ * @return a {@link CompletionStage completion stage} that represents the asynchronous begin of a transaction.
112
+ */
87
113
CompletionStage <Transaction > beginTransactionAsync ();
88
114
89
115
/**
90
116
* Execute given unit of work in a {@link AccessMode#READ read} transaction.
91
117
* <p>
92
118
* Transaction will automatically be committed unless exception is thrown from the unit of work itself or from
93
119
* {@link Transaction#close()} or transaction is explicitly marked for failure via {@link Transaction#failure()}.
120
+ * <p>
121
+ * This operation works the same way as {@link #readTransactionAsync(TransactionWork)} but blocks until given
122
+ * blocking unit of work is completed.
94
123
*
95
124
* @param work the {@link TransactionWork} to be applied to a new read transaction.
96
125
* @param <T> the return type of the given unit of work.
97
126
* @return a result as returned by the given unit of work.
98
127
*/
99
128
<T > T readTransaction ( TransactionWork <T > work );
100
129
130
+ /**
131
+ * Execute given unit of asynchronous work in a {@link AccessMode#READ read} asynchronous transaction.
132
+ * <p>
133
+ * Transaction will automatically be committed unless given unit of work fails or
134
+ * {@link Transaction#commitAsync() async transaction commit} fails. It will also not be committed if explicitly
135
+ * rolled back via {@link Transaction#rollbackAsync()}.
136
+ * <p>
137
+ * Returned stage and given {@link TransactionWork} can be completed/executed by an IO thread which should never
138
+ * block. Otherwise IO operations on this and potentially other network connections might deadlock. Please do not
139
+ * chain blocking operations like {@link #run(String)} on the returned stage and do not use them inside the
140
+ * {@link TransactionWork}. Driver will throw {@link IllegalStateException} when blocking API
141
+ * call is executed in IO thread. Consider using asynchronous calls throughout the chain or offloading blocking
142
+ * operation to a different {@link Executor}. This can be done using methods with "Async" suffix like
143
+ * {@link CompletionStage#thenApplyAsync(Function)} or {@link CompletionStage#thenApplyAsync(Function, Executor)}.
144
+ *
145
+ * @param work the {@link TransactionWork} to be applied to a new read transaction. Operation executed by the
146
+ * given work must be asynchronous.
147
+ * @param <T> the return type of the given unit of work.
148
+ * @return a {@link CompletionStage completion stage} completed with the same result as returned by the given
149
+ * unit of work. Stage can be completed exceptionally if given work or commit fails.
150
+ */
101
151
<T > CompletionStage <T > readTransactionAsync ( TransactionWork <CompletionStage <T >> work );
102
152
103
153
/**
104
- * Execute given unit of work in a {@link AccessMode#WRITE write} transaction.
154
+ * Execute given unit of work in a {@link AccessMode#WRITE write} transaction.
105
155
* <p>
106
156
* Transaction will automatically be committed unless exception is thrown from the unit of work itself or from
107
157
* {@link Transaction#close()} or transaction is explicitly marked for failure via {@link Transaction#failure()}.
158
+ * <p>
159
+ * This operation works the same way as {@link #writeTransactionAsync(TransactionWork)} but blocks until given
160
+ * blocking unit of work is completed.
108
161
*
109
162
* @param work the {@link TransactionWork} to be applied to a new write transaction.
110
163
* @param <T> the return type of the given unit of work.
111
164
* @return a result as returned by the given unit of work.
112
165
*/
113
166
<T > T writeTransaction ( TransactionWork <T > work );
114
167
168
+ /**
169
+ * Execute given unit of asynchronous work in a {@link AccessMode#WRITE write} asynchronous transaction.
170
+ * <p>
171
+ * Transaction will automatically be committed unless given unit of work fails or
172
+ * {@link Transaction#commitAsync() async transaction commit} fails. It will also not be committed if explicitly
173
+ * rolled back via {@link Transaction#rollbackAsync()}.
174
+ * <p>
175
+ * Returned stage and given {@link TransactionWork} can be completed/executed by an IO thread which should never
176
+ * block. Otherwise IO operations on this and potentially other network connections might deadlock. Please do not
177
+ * chain blocking operations like {@link #run(String)} on the returned stage and do not use them inside the
178
+ * {@link TransactionWork}. Driver will throw {@link IllegalStateException} when blocking API
179
+ * call is executed in IO thread. Consider using asynchronous calls throughout the chain or offloading blocking
180
+ * operation to a different {@link Executor}. This can be done using methods with "Async" suffix like
181
+ * {@link CompletionStage#thenApplyAsync(Function)} or {@link CompletionStage#thenApplyAsync(Function, Executor)}.
182
+ *
183
+ * @param work the {@link TransactionWork} to be applied to a new write transaction. Operation executed by the
184
+ * given work must be asynchronous.
185
+ * @param <T> the return type of the given unit of work.
186
+ * @return a {@link CompletionStage completion stage} completed with the same result as returned by the given
187
+ * unit of work. Stage can be completed exceptionally if given work or commit fails.
188
+ */
115
189
<T > CompletionStage <T > writeTransactionAsync ( TransactionWork <CompletionStage <T >> work );
116
190
117
191
/**
@@ -142,14 +216,23 @@ public interface Session extends Resource, StatementRunner
142
216
void reset ();
143
217
144
218
/**
145
- * Signal that you are done using this session. In the default driver usage, closing
146
- * and accessing sessions is very low cost, because sessions are pooled by {@link Driver}.
147
- *
148
- * When this method returns, all outstanding statements in the session are guaranteed to
149
- * have completed, meaning any writes you performed are guaranteed to be durably stored.
219
+ * Signal that you are done using this session. In the default driver usage, closing and accessing sessions is
220
+ * very low cost.
221
+ * <p>
222
+ * This operation works the same way as {@link #closeAsync()} but blocks until session is actually closed.
150
223
*/
151
224
@ Override
152
225
void close ();
153
226
227
+ /**
228
+ * Signal that you are done using this session. In the default driver usage, closing and accessing sessions is
229
+ * very low cost.
230
+ * <p>
231
+ * This operation is asynchronous and returns a {@link CompletionStage}. Stage is completed when all outstanding
232
+ * statements in the session have completed, meaning any writes you performed are guaranteed to be durably stored.
233
+ * It might be completed exceptionally when there are unconsumed errors from previous statements or transactions.
234
+ *
235
+ * @return a {@link CompletionStage completion stage} that represents the asynchronous close.
236
+ */
154
237
CompletionStage <Void > closeAsync ();
155
238
}
0 commit comments