18
18
*/
19
19
package org .neo4j .driver ;
20
20
21
+ import java .util .Collections ;
22
+ import java .util .Map ;
21
23
import java .util .concurrent .CompletionStage ;
24
+ import java .util .function .Consumer ;
22
25
import org .neo4j .driver .async .AsyncSession ;
23
26
import org .neo4j .driver .exceptions .ClientException ;
24
27
import org .neo4j .driver .reactive .ReactiveSession ;
63
66
* @since 1.0 (Modified and Added {@link AsyncSession} and {@link RxSession} since 2.0)
64
67
*/
65
68
public interface Driver extends AutoCloseable {
69
+ /**
70
+ * Execute an idempotent query in a managed transaction with automatic retries on retryable errors.
71
+ * <p>
72
+ * This is a basic high-level API for executing idempotent queries. {@link #executeQuery(Query, QueryConfig)}
73
+ * documentation provides more details.
74
+ *
75
+ * @param query a query value to execute
76
+ * @return a query execution result value
77
+ */
78
+ default EagerResult executeQuery (Query query ) {
79
+ var defaultConfig = QueryConfig .builder ().build ();
80
+ return executeQuery (query , defaultConfig );
81
+ }
82
+
83
+ /**
84
+ * Execute an idempotent query in a managed transaction with automatic retries on retryable errors.
85
+ * <p>
86
+ * This is a basic high-level API for executing idempotent queries. There are more advanced APIs available.
87
+ * For instance, {@link Session}, {@link Transaction} and transaction functions that are accessible via
88
+ * methods like {@link Session#executeWrite(TransactionCallback)}, {@link Session#executeWriteWithoutResult(Consumer)}
89
+ * and {@link Session#executeRead(TransactionCallback)} (there are also overloaded options available).
90
+ * <p>
91
+ * Causal consistency is managed via driver's {@link BookmarkManager} that is enabled by default and may
92
+ * be replaced using {@link Config.ConfigBuilder#withQueryBookmarkManager(BookmarkManager)}. It is also possible
93
+ * to use a different {@link BookmarkManager} or disable it via
94
+ * {@link QueryConfig.Builder#withBookmarkManager(BookmarkManager)} on individual basis.
95
+ * <p>
96
+ * Sample usage:
97
+ * <pre>
98
+ * {@code
99
+ * var query = new Query("CREATE (n{field: $value}) RETURN n", Map.of("value", "value"));
100
+ * var eagerResult = driver.executeQuery(query, QueryConfig.defaultConfig());
101
+ * }
102
+ * </pre>
103
+ * The above sample is functionally similar to the following use of the more advanced APIs:
104
+ * <pre>
105
+ * {@code
106
+ * var query = new Query("CREATE (n{field: $value}) RETURN n", Map.of("value", "value"));
107
+ * ResultTransformer<EagerResult> resultTransformer = ResultTransformer implementation;
108
+ * var sessionConfig = SessionConfig.builder()
109
+ * .withBookmarkManager(driverConfig.queryBookmarkManager())
110
+ * .build();
111
+ * try (var session = driver.session(sessionConfig)) {
112
+ * var eagerResult = session.executeWrite(tx -> {
113
+ * var result = tx.run(query);
114
+ * return resultTransformer.transform(result);
115
+ * });
116
+ * }
117
+ * }
118
+ * </pre>
119
+ *
120
+ * @param query a query value to execute
121
+ * @param config a query execution config value
122
+ * @param <T> a type managed by {@link QueryConfig#resultTransformer()}
123
+ * @return a query execution result value of a type managed by {@link QueryConfig#resultTransformer()}
124
+ */
125
+ <T > T executeQuery (Query query , QueryConfig <T > config );
126
+
127
+ /**
128
+ * Execute an idempotent query in a managed transaction with automatic retries on retryable errors.
129
+ * <p>
130
+ * This is a basic high-level API for executing idempotent queries. {@link #executeQuery(Query, QueryConfig)}
131
+ * documentation provides more details.
132
+ *
133
+ * @param query a query string to execute
134
+ * @return a query execution result value
135
+ */
136
+ default EagerResult executeQuery (String query ) {
137
+ return executeQuery (query , Collections .emptyMap ());
138
+ }
139
+
140
+ /**
141
+ * Execute an idempotent query in a managed transaction with automatic retries on retryable errors.
142
+ * <p>
143
+ * This is a basic high-level API for executing idempotent queries. {@link #executeQuery(Query, QueryConfig)}
144
+ * documentation provides more details.
145
+ *
146
+ * @param query a query string to execute
147
+ * @param parameters a query parameters
148
+ * @return a query execution result value
149
+ */
150
+ default EagerResult executeQuery (String query , Map <String , Object > parameters ) {
151
+ return executeQuery (new Query (query , parameters ));
152
+ }
153
+
154
+ /**
155
+ * Execute an idempotent query in a managed transaction with automatic retries on retryable errors.
156
+ * <p>
157
+ * This is a basic high-level API for executing idempotent queries. {@link #executeQuery(Query, QueryConfig)}
158
+ * documentation provides more details.
159
+ *
160
+ * @param query a query string to execute
161
+ * @param parameters a query parameters
162
+ * @param config a query execution config value
163
+ * @param <T> a type managed by {@link QueryConfig#resultTransformer()}
164
+ * @return a query execution result value of a type managed by {@link QueryConfig#resultTransformer()}
165
+ */
166
+ default <T > T executeQuery (String query , Map <String , Object > parameters , QueryConfig <T > config ) {
167
+ return executeQuery (new Query (query , parameters ), config );
168
+ }
169
+
170
+ /**
171
+ * Returns an instance of {@link BookmarkManager} used by {@link #executeQuery(Query, QueryConfig)} method and its variants by default.
172
+ *
173
+ * @return bookmark manager, must not be {@code null}
174
+ */
175
+ BookmarkManager queryBookmarkManager ();
176
+
66
177
/**
67
178
* Return a flag to indicate whether or not encryption is used for this driver.
68
179
*
@@ -84,6 +195,7 @@ default Session session() {
84
195
/**
85
196
* Instantiate a new {@link Session} with a specified {@link SessionConfig session configuration}.
86
197
* Use {@link SessionConfig#forDatabase(String)} to obtain a general purpose session configuration for the specified database.
198
+ *
87
199
* @param sessionConfig specifies session configurations for this session.
88
200
* @return a new {@link Session} object.
89
201
* @see SessionConfig
@@ -257,6 +369,7 @@ default AsyncSession asyncSession(SessionConfig sessionConfig) {
257
369
/**
258
370
* Returns the driver metrics if metrics reporting is enabled via {@link Config.ConfigBuilder#withDriverMetrics()}.
259
371
* Otherwise, a {@link ClientException} will be thrown.
372
+ *
260
373
* @return the driver metrics if enabled.
261
374
* @throws ClientException if the driver metrics reporting is not enabled.
262
375
*/
@@ -281,7 +394,7 @@ default AsyncSession asyncSession(SessionConfig sessionConfig) {
281
394
/**
282
395
* This verifies if the driver can connect to a remote server or a cluster
283
396
* by establishing a network connection with the remote and possibly exchanging a few data before closing the connection.
284
- *
397
+ * <p>
285
398
* It throws exception if fails to connect. Use the exception to further understand the cause of the connectivity problem.
286
399
* Note: Even if this method throws an exception, the driver still need to be closed via {@link #close()} to free up all resources.
287
400
*/
@@ -290,7 +403,7 @@ default AsyncSession asyncSession(SessionConfig sessionConfig) {
290
403
/**
291
404
* This verifies if the driver can connect to a remote server or cluster
292
405
* by establishing a network connection with the remote and possibly exchanging a few data before closing the connection.
293
- *
406
+ * <p>
294
407
* This operation is asynchronous and returns a {@link CompletionStage}. This stage is completed with
295
408
* {@code null} when the driver connects to the remote server or cluster successfully.
296
409
* It is completed exceptionally if the driver failed to connect the remote server or cluster.
@@ -303,12 +416,14 @@ default AsyncSession asyncSession(SessionConfig sessionConfig) {
303
416
304
417
/**
305
418
* Returns true if the server or cluster the driver connects to supports multi-databases, otherwise false.
419
+ *
306
420
* @return true if the server or cluster the driver connects to supports multi-databases, otherwise false.
307
421
*/
308
422
boolean supportsMultiDb ();
309
423
310
424
/**
311
425
* Asynchronous check if the server or cluster the driver connects to supports multi-databases.
426
+ *
312
427
* @return a {@link CompletionStage completion stage} that returns true if the server or cluster
313
428
* the driver connects to supports multi-databases, otherwise false.
314
429
*/
0 commit comments