20
20
21
21
import java .util .Collections ;
22
22
import java .util .Map ;
23
+ import java .util .concurrent .CompletableFuture ;
23
24
import java .util .concurrent .CompletionStage ;
25
+ import java .util .function .BiConsumer ;
24
26
25
27
import org .neo4j .driver .ResultResourcesHandler ;
26
28
import org .neo4j .driver .internal .async .AsyncConnection ;
27
- import org .neo4j .driver .internal .async .InternalFuture ;
28
- import org .neo4j .driver .internal .async .InternalPromise ;
29
29
import org .neo4j .driver .internal .async .QueryRunner ;
30
30
import org .neo4j .driver .internal .handlers .BeginTxResponseHandler ;
31
31
import org .neo4j .driver .internal .handlers .BookmarkResponseHandler ;
34
34
import org .neo4j .driver .internal .handlers .RollbackTxResponseHandler ;
35
35
import org .neo4j .driver .internal .spi .Connection ;
36
36
import org .neo4j .driver .internal .types .InternalTypeSystem ;
37
- import org .neo4j .driver .internal .util .BiConsumer ;
38
37
import org .neo4j .driver .v1 .Record ;
39
38
import org .neo4j .driver .v1 .Statement ;
40
39
import org .neo4j .driver .v1 .StatementResult ;
45
44
import org .neo4j .driver .v1 .exceptions .ClientException ;
46
45
import org .neo4j .driver .v1 .exceptions .Neo4jException ;
47
46
import org .neo4j .driver .v1 .types .TypeSystem ;
48
- import org .neo4j .driver .v1 .util .Function ;
49
47
50
- import static org .neo4j .driver .internal .async .Futures .asCompletionStage ;
48
+ import static java .util .concurrent .CompletableFuture .completedFuture ;
49
+ import static org .neo4j .driver .internal .async .Futures .failedFuture ;
51
50
import static org .neo4j .driver .internal .util .ErrorUtil .isRecoverable ;
52
51
import static org .neo4j .driver .v1 .Values .ofValue ;
53
52
import static org .neo4j .driver .v1 .Values .value ;
@@ -119,25 +118,23 @@ public void begin( Bookmark initialBookmark )
119
118
}
120
119
}
121
120
122
- public InternalFuture <ExplicitTransaction > beginAsync ( Bookmark initialBookmark )
121
+ public CompletionStage <ExplicitTransaction > beginAsync ( Bookmark initialBookmark )
123
122
{
124
- InternalPromise <ExplicitTransaction > beginTxPromise = asyncConnection .newPromise ();
125
-
126
123
Map <String ,Value > parameters = initialBookmark .asBeginTransactionParameters ();
127
124
asyncConnection .run ( BEGIN_QUERY , parameters , NoOpResponseHandler .INSTANCE );
128
125
129
126
if ( initialBookmark .isEmpty () )
130
127
{
131
128
asyncConnection .pullAll ( NoOpResponseHandler .INSTANCE );
132
- beginTxPromise . setSuccess ( this );
129
+ return completedFuture ( this );
133
130
}
134
131
else
135
132
{
136
- asyncConnection .pullAll ( new BeginTxResponseHandler <>( beginTxPromise , this ) );
133
+ CompletableFuture <ExplicitTransaction > beginFuture = new CompletableFuture <>();
134
+ asyncConnection .pullAll ( new BeginTxResponseHandler <>( beginFuture , this ) );
137
135
asyncConnection .flush ();
136
+ return beginFuture ;
138
137
}
139
-
140
- return beginTxPromise ;
141
138
}
142
139
143
140
@ Override
@@ -217,20 +214,14 @@ private void rollbackTx()
217
214
218
215
@ Override
219
216
public CompletionStage <Void > commitAsync ()
220
- {
221
- return asCompletionStage ( internalCommitAsync () );
222
- }
223
-
224
- InternalFuture <Void > internalCommitAsync ()
225
217
{
226
218
if ( state == State .COMMITTED )
227
219
{
228
- return asyncConnection .< Void > newPromise (). setSuccess ( null );
220
+ return completedFuture ( null );
229
221
}
230
222
else if ( state == State .ROLLED_BACK )
231
223
{
232
- return asyncConnection .<Void >newPromise ().setFailure (
233
- new ClientException ( "Can't commit, transaction has already been rolled back" ) );
224
+ return failedFuture ( new ClientException ( "Can't commit, transaction has already been rolled back" ) );
234
225
}
235
226
else
236
227
{
@@ -240,20 +231,14 @@ else if ( state == State.ROLLED_BACK )
240
231
241
232
@ Override
242
233
public CompletionStage <Void > rollbackAsync ()
243
- {
244
- return asCompletionStage ( internalRollbackAsync () );
245
- }
246
-
247
- InternalFuture <Void > internalRollbackAsync ()
248
234
{
249
235
if ( state == State .COMMITTED )
250
236
{
251
- return asyncConnection .<Void >newPromise ()
252
- .setFailure ( new ClientException ( "Can't rollback, transaction has already been committed" ) );
237
+ return failedFuture ( new ClientException ( "Can't rollback, transaction has already been committed" ) );
253
238
}
254
239
else if ( state == State .ROLLED_BACK )
255
240
{
256
- return asyncConnection .< Void > newPromise (). setSuccess ( null );
241
+ return completedFuture ( null );
257
242
}
258
243
else
259
244
{
@@ -263,51 +248,39 @@ else if ( state == State.ROLLED_BACK )
263
248
264
249
private BiConsumer <Void ,Throwable > releaseConnectionAndNotifySession ()
265
250
{
266
- return new BiConsumer < Void , Throwable >()
251
+ return ( ignore , error ) ->
267
252
{
268
- @ Override
269
- public void accept ( Void result , Throwable error )
270
- {
271
- asyncConnection .release ();
272
- session .asyncTransactionClosed ( ExplicitTransaction .this );
273
- }
253
+ asyncConnection .release ();
254
+ session .asyncTransactionClosed ( ExplicitTransaction .this );
274
255
};
275
256
}
276
257
277
- private InternalFuture <Void > doCommitAsync ()
258
+ private CompletionStage <Void > doCommitAsync ()
278
259
{
279
- InternalPromise <Void > commitTxPromise = asyncConnection . newPromise ();
260
+ CompletableFuture <Void > commitFuture = new CompletableFuture <> ();
280
261
281
- asyncConnection .run ( COMMIT_QUERY , Collections .< String , Value > emptyMap (), NoOpResponseHandler .INSTANCE );
282
- asyncConnection .pullAll ( new CommitTxResponseHandler ( commitTxPromise , this ) );
262
+ asyncConnection .run ( COMMIT_QUERY , Collections .emptyMap (), NoOpResponseHandler .INSTANCE );
263
+ asyncConnection .pullAll ( new CommitTxResponseHandler ( commitFuture , this ) );
283
264
asyncConnection .flush ();
284
265
285
- return commitTxPromise .thenApply ( new Function < Void , Void >()
266
+ return commitFuture .thenApply ( ignore ->
286
267
{
287
- @ Override
288
- public Void apply ( Void ignore )
289
- {
290
- ExplicitTransaction .this .state = State .COMMITTED ;
291
- return null ;
292
- }
268
+ ExplicitTransaction .this .state = State .COMMITTED ;
269
+ return null ;
293
270
} );
294
271
}
295
272
296
- private InternalFuture <Void > doRollbackAsync ()
273
+ private CompletionStage <Void > doRollbackAsync ()
297
274
{
298
- InternalPromise <Void > rollbackTxPromise = asyncConnection . newPromise ();
299
- asyncConnection .run ( ROLLBACK_QUERY , Collections .< String , Value > emptyMap (), NoOpResponseHandler .INSTANCE );
300
- asyncConnection .pullAll ( new RollbackTxResponseHandler ( rollbackTxPromise ) );
275
+ CompletableFuture <Void > rollbackFuture = new CompletableFuture <> ();
276
+ asyncConnection .run ( ROLLBACK_QUERY , Collections .emptyMap (), NoOpResponseHandler .INSTANCE );
277
+ asyncConnection .pullAll ( new RollbackTxResponseHandler ( rollbackFuture ) );
301
278
asyncConnection .flush ();
302
279
303
- return rollbackTxPromise .thenApply ( new Function < Void , Void >()
280
+ return rollbackFuture .thenApply ( ignore ->
304
281
{
305
- @ Override
306
- public Void apply ( Void ignore )
307
- {
308
- ExplicitTransaction .this .state = State .ROLLED_BACK ;
309
- return null ;
310
- }
282
+ ExplicitTransaction .this .state = State .ROLLED_BACK ;
283
+ return null ;
311
284
} );
312
285
}
313
286
@@ -393,7 +366,7 @@ public StatementResult run( Statement statement )
393
366
public CompletionStage <StatementResultCursor > runAsync ( Statement statement )
394
367
{
395
368
ensureNotFailed ();
396
- return asCompletionStage ( QueryRunner .runAsync ( asyncConnection , statement , this ) );
369
+ return QueryRunner .runAsync ( asyncConnection , statement , this );
397
370
}
398
371
399
372
@ Override
0 commit comments