/
Session.java
337 lines (318 loc) · 16.3 KB
/
Session.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [https://neo4j.com]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.util.Resource;
/**
* Provides a context of work for database interactions.
* <p>
* A <em>Session</em> is a logical container for a causally chained series of
* {@linkplain Transaction transactions}. Client applications typically work
* with <em>managed transactions</em>, which come in two flavours: transaction
* functions and auto-commit transactions. Managed transactions automatically
* handle the transaction boundaries (<code>BEGIN</code> and
* <code>COMMIT</code>/<code>ROLLBACK</code>) and can also provide other
* supporting features; transaction functions offer retry capabilities, for
* example.
* <p>
* Unmanaged transactions are also available but tend to be used by libraries
* or tooling that require more fine-grained control.
* <p>
* Typically, a session will acquire a TCP connection from a connection pool
* in order to carry out a transaction. Once the transaction has completed,
* and the entire result has been consumed, this connection will be released
* back into the pool. One connection can therefore be adopted by several
* sessions over its lifetime, although it will only be owned by one at a
* time. Client applications should never need to deal directly with
* connection management.
* <p>
* Session implementations are not generally thread-safe. Therefore, multiple
* sessions should be used when an application requires multiple concurrent
* threads of database work to be carried out.
*
* @since 1.0 (Removed async API to {@link AsyncSession} in 4.0)
*/
public interface Session extends BaseSession, Resource, QueryRunner {
/**
* Begin a new <em>unmanaged {@linkplain Transaction transaction}</em>. At
* most one transaction may exist in a session at any point in time. To
* maintain multiple concurrent transactions, use multiple concurrent
* sessions.
*
* @return a new {@link Transaction}
*/
Transaction beginTransaction();
/**
* Begin a new <em>unmanaged {@linkplain Transaction transaction}</em> with
* the specified {@link TransactionConfig configuration}. At most one
* transaction may exist in a session at any point in time. To maintain
* multiple concurrent transactions, use multiple concurrent sessions.
*
* @param config configuration for the new transaction.
* @return a new {@link Transaction}
*/
Transaction beginTransaction(TransactionConfig config);
/**
* Execute a unit of work in a managed {@link AccessMode#READ read} transaction.
* <p>
* This transaction will automatically be committed unless an exception is thrown during query execution or by the user code.
* <p>
* Managed transactions should not be explicitly committed (via {@link Transaction#commit()}).
*
* @param work the {@link TransactionWork} to be applied to a new read transaction.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
* @deprecated superseded by {@link #executeRead(TransactionCallback)}.
*/
@Deprecated
<T> T readTransaction(TransactionWork<T> work);
/**
* Execute a unit of work as a single, managed transaction with {@link AccessMode#READ read} access mode and retry behaviour. The transaction allows for one
* or more statements to be run.
* <p>
* The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work will
* result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver.
* <p>
* The provided unit of work should not return {@link Result} object as it won't be valid outside the scope of the transaction.
*
* @param callback the callback representing the unit of work.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
*/
default <T> T executeRead(TransactionCallback<T> callback) {
return executeRead(callback, TransactionConfig.empty());
}
/**
* Execute a unit of work in a managed {@link AccessMode#READ read} transaction with the specified {@link TransactionConfig configuration}.
* <p>
* This transaction will automatically be committed unless an exception is thrown during query execution or by the user code.
* <p>
* Managed transactions should not be explicitly committed (via {@link Transaction#commit()}).
*
* @param work the {@link TransactionWork} to be applied to a new read transaction.
* @param config configuration for all transactions started to execute the unit of work.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
* @deprecated superseded by {@link #executeRead(TransactionCallback, TransactionConfig)}.
*/
@Deprecated
<T> T readTransaction(TransactionWork<T> work, TransactionConfig config);
/**
* Execute a unit of work as a single, managed transaction with {@link AccessMode#READ read} access mode and retry behaviour. The transaction allows for one
* or more statements to be run.
* <p>
* The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work will
* result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver.
* <p>
* The provided unit of work should not return {@link Result} object as it won't be valid outside the scope of the transaction.
*
* @param callback the callback representing the unit of work.
* @param config the transaction configuration for the managed transaction.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
*/
<T> T executeRead(TransactionCallback<T> callback, TransactionConfig config);
/**
* Execute a unit of work in a managed {@link AccessMode#WRITE write} transaction.
* <p>
* This transaction will automatically be committed unless an exception is thrown during query execution or by the user code.
* <p>
* Managed transactions should not be explicitly committed (via {@link Transaction#commit()}).
*
* @param work the {@link TransactionWork} to be applied to a new write transaction.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
* @deprecated superseded by {@link #executeWrite(TransactionCallback)}.
*/
@Deprecated
<T> T writeTransaction(TransactionWork<T> work);
/**
* Execute a unit of work as a single, managed transaction with {@link AccessMode#WRITE write} access mode and retry behaviour. The transaction allows for
* one or more statements to be run.
* <p>
* The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work will
* result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver.
* <p>
* The provided unit of work should not return {@link Result} object as it won't be valid outside the scope of the transaction.
*
* @param callback the callback representing the unit of work.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
*/
default <T> T executeWrite(TransactionCallback<T> callback) {
return executeWrite(callback, TransactionConfig.empty());
}
/**
* Execute a unit of work as a single, managed transaction with {@link AccessMode#WRITE write} access mode and retry behaviour. The transaction allows for one or more statements to be run.
* <p>
* The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work
* will result in a rollback attempt.
* <p>
* This method works equivalently to {@link #executeWrite(TransactionCallback)}, but does not have a return value.
*
* @param contextConsumer the consumer representing the unit of work.
*/
default void executeWriteWithoutResult(Consumer<TransactionContext> contextConsumer) {
executeWrite(tc -> {
contextConsumer.accept(tc);
return null;
});
}
/**
* Execute a unit of work in a managed {@link AccessMode#WRITE write} transaction with the specified {@link TransactionConfig configuration}.
* <p>
* This transaction will automatically be committed unless an exception is thrown during query execution or by the user code.
* <p>
* Managed transactions should not be explicitly committed (via {@link Transaction#commit()}).
*
* @param work the {@link TransactionWork} to be applied to a new write transaction.
* @param config configuration for all transactions started to execute the unit of work.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
* @deprecated superseded by {@link #executeWrite(TransactionCallback, TransactionConfig)}.
*/
@Deprecated
<T> T writeTransaction(TransactionWork<T> work, TransactionConfig config);
/**
* Execute a unit of work as a single, managed transaction with {@link AccessMode#WRITE write} access mode and retry behaviour. The transaction allows for one or more statements to be run.
* <p>
* The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work
* will result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver.
* <p>
* The provided unit of work should not return {@link Result} object as it won't be valid outside the scope of the transaction.
*
* @param callback the callback representing the unit of work.
* @param config the transaction configuration for the managed transaction.
* @param <T> the return type of the given unit of work.
* @return a result as returned by the given unit of work.
*/
<T> T executeWrite(TransactionCallback<T> callback, TransactionConfig config);
/**
* Execute a unit of work as a single, managed transaction with {@link AccessMode#WRITE write} access mode and retry behaviour. The transaction allows for one or more statements to be run.
* <p>
* The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work
* will result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver.
* <p>
* This method works equivalently to {@link #executeWrite(TransactionCallback, TransactionConfig)}, but does not have a return value.
*
* @param contextConsumer the consumer representing the unit of work.
* @param config the transaction configuration for the managed transaction.
*/
default void executeWriteWithoutResult(Consumer<TransactionContext> contextConsumer, TransactionConfig config) {
executeWrite(
tc -> {
contextConsumer.accept(tc);
return null;
},
config);
}
/**
* Run a query in a managed auto-commit transaction with the specified {@link TransactionConfig configuration}, and return a result stream.
*
* @param query text of a Neo4j query.
* @param config configuration for the new transaction.
* @return a stream of result values and associated metadata.
*/
Result run(String query, TransactionConfig config);
/**
* Run a query with parameters in a managed auto-commit transaction with the
* specified {@link TransactionConfig configuration}, and return a result stream.
* <p>
* This method takes a set of parameters that will be injected into the
* query by Neo4j. Using parameters is highly encouraged, it helps avoid
* dangerous cypher injection attacks and improves database performance as
* Neo4j can re-use query plans more often.
* <p>
* This version of run takes a {@link Map} of parameters. The values in the map
* must be values that can be converted to Neo4j types. See {@link Values#parameters(Object...)} for
* a list of allowed types.
* <h4>Example</h4>
* <pre>
* {@code
* Map<String, Object> metadata = new HashMap<>();
* metadata.put("type", "update name");
*
* TransactionConfig config = TransactionConfig.builder()
* .withTimeout(Duration.ofSeconds(3))
* .withMetadata(metadata)
* .build();
*
* Map<String, Object> parameters = new HashMap<>();
* parameters.put("myNameParam", "Bob");
*
* Result result = session.run("MATCH (n) WHERE n.name = $myNameParam RETURN (n)", parameters, config);
* }
* </pre>
*
* @param query text of a Neo4j query.
* @param parameters input data for the query.
* @param config configuration for the new transaction.
* @return a stream of result values and associated metadata.
*/
Result run(String query, Map<String, Object> parameters, TransactionConfig config);
/**
* Run a query in a managed auto-commit transaction with the specified
* {@link TransactionConfig configuration}, and return a result stream.
* <h4>Example</h4>
* <pre>
* {@code
* Map<String, Object> metadata = new HashMap<>();
* metadata.put("type", "update name");
*
* TransactionConfig config = TransactionConfig.builder()
* .withTimeout(Duration.ofSeconds(3))
* .withMetadata(metadata)
* .build();
*
* Query query = new Query("MATCH (n) WHERE n.name = $myNameParam RETURN n.age");
* Result result = session.run(query.withParameters(Values.parameters("myNameParam", "Bob")));
* }
* </pre>
*
* @param query a Neo4j query.
* @param config configuration for the new transaction.
* @return a stream of result values and associated metadata.
*/
Result run(Query query, TransactionConfig config);
/**
* Return the last bookmark of this session.
* <p>
* When no new bookmark is received, the initial bookmarks are returned as a composite {@link Bookmark} containing all initial bookmarks. This may happen
* when no work has been done using the session. If no initial bookmarks have been provided, an empty {@link Bookmark} is returned.
*
* @return the last bookmark.
*/
@Deprecated
Bookmark lastBookmark();
/**
* Return a set of last bookmarks.
* <p>
* When no new bookmark is received, the initial bookmarks are returned. This may happen when no work has been done using the session. Multivalued {@link
* Bookmark} instances will be mapped to distinct {@link Bookmark} instances. If no initial bookmarks have been provided, an empty set is returned.
*
* @return the immutable set of last bookmarks.
*/
Set<Bookmark> lastBookmarks();
/**
* Signal that you are done using this session. In the default driver usage, closing and accessing sessions is very low cost.
*/
@Override
void close();
}