|
26 | 26 | import java.util.concurrent.locks.Lock;
|
27 | 27 | import java.util.concurrent.locks.ReentrantLock;
|
28 | 28 | import java.util.function.BiFunction;
|
| 29 | +import java.util.function.Function; |
29 | 30 |
|
30 | 31 | import org.neo4j.driver.Bookmark;
|
31 | 32 | import org.neo4j.driver.Query;
|
|
40 | 41 | import org.neo4j.driver.internal.cursor.RxResultCursor;
|
41 | 42 | import org.neo4j.driver.internal.messaging.BoltProtocol;
|
42 | 43 | import org.neo4j.driver.internal.spi.Connection;
|
43 |
| -import org.neo4j.driver.internal.util.Futures; |
44 | 44 |
|
| 45 | +import static org.neo4j.driver.internal.util.Futures.asCompletionException; |
| 46 | +import static org.neo4j.driver.internal.util.Futures.combineErrors; |
45 | 47 | import static org.neo4j.driver.internal.util.Futures.completedWithNull;
|
46 | 48 | import static org.neo4j.driver.internal.util.Futures.failedFuture;
|
| 49 | +import static org.neo4j.driver.internal.util.Futures.futureCompletingConsumer; |
47 | 50 | import static org.neo4j.driver.internal.util.LockUtil.executeWithLock;
|
48 | 51 |
|
49 | 52 | public class UnmanagedTransaction
|
@@ -123,164 +126,25 @@ else if ( beginError instanceof ConnectionReadTimeoutException )
|
123 | 126 | {
|
124 | 127 | connection.release();
|
125 | 128 | }
|
126 |
| - throw Futures.asCompletionException( beginError ); |
| 129 | + throw asCompletionException( beginError ); |
127 | 130 | }
|
128 | 131 | return this;
|
129 | 132 | } );
|
130 | 133 | }
|
131 | 134 |
|
132 | 135 | public CompletionStage<Void> closeAsync()
|
133 | 136 | {
|
134 |
| - CompletionStage<Void> stage = executeWithLock( lock, () -> |
135 |
| - { |
136 |
| - CompletionStage<Void> resultStage = null; |
137 |
| - if ( !isOpen() ) |
138 |
| - { |
139 |
| - resultStage = completedWithNull(); |
140 |
| - } |
141 |
| - else if ( state == State.COMMITTED ) |
142 |
| - { |
143 |
| - resultStage = failedFuture( new ClientException( CANT_ROLLBACK_COMMITTED_MSG ) ); |
144 |
| - } |
145 |
| - else if ( state == State.ROLLED_BACK ) |
146 |
| - { |
147 |
| - resultStage = failedFuture( new ClientException( CANT_ROLLBACK_ROLLED_BACK_MSG ) ); |
148 |
| - } |
149 |
| - else if ( commitFuture != null ) |
150 |
| - { |
151 |
| - resultStage = failedFuture( new ClientException( CANT_ROLLBACK_COMMITTING_MSG ) ); |
152 |
| - } |
153 |
| - else if ( rollbackFuture != null ) |
154 |
| - { |
155 |
| - resultStage = rollbackFuture; |
156 |
| - } |
157 |
| - else |
158 |
| - { |
159 |
| - rollbackFuture = new CompletableFuture<>(); |
160 |
| - } |
161 |
| - return resultStage; |
162 |
| - } ); |
163 |
| - |
164 |
| - if ( stage == null ) |
165 |
| - { |
166 |
| - stage = resultCursors |
167 |
| - .retrieveNotConsumedError() |
168 |
| - .thenCompose( error -> doRollbackAsync().handle( handleCommitOrRollback( error ) ) ) |
169 |
| - .whenComplete( ( ignore, error ) -> handleTransactionCompletion( false, error ) ); |
170 |
| - stage.whenComplete( ( result, error ) -> |
171 |
| - { |
172 |
| - if ( error != null ) |
173 |
| - { |
174 |
| - rollbackFuture.completeExceptionally( error ); |
175 |
| - } |
176 |
| - else |
177 |
| - { |
178 |
| - rollbackFuture.complete( result ); |
179 |
| - } |
180 |
| - } ); |
181 |
| - } |
182 |
| - |
183 |
| - return stage; |
| 137 | + return closeAsync( false, true ); |
184 | 138 | }
|
185 | 139 |
|
186 | 140 | public CompletionStage<Void> commitAsync()
|
187 | 141 | {
|
188 |
| - CompletionStage<Void> stage = executeWithLock( lock, () -> |
189 |
| - { |
190 |
| - CompletionStage<Void> resultStage = null; |
191 |
| - if ( state == State.COMMITTED ) |
192 |
| - { |
193 |
| - resultStage = failedFuture( new ClientException( CANT_COMMIT_COMMITTED_MSG ) ); |
194 |
| - } |
195 |
| - else if ( state == State.ROLLED_BACK ) |
196 |
| - { |
197 |
| - resultStage = failedFuture( new ClientException( CANT_COMMIT_ROLLED_BACK_MSG ) ); |
198 |
| - } |
199 |
| - else if ( rollbackFuture != null ) |
200 |
| - { |
201 |
| - resultStage = failedFuture( new ClientException( CANT_COMMIT_ROLLING_BACK_MSG ) ); |
202 |
| - } |
203 |
| - else if ( commitFuture != null ) |
204 |
| - { |
205 |
| - resultStage = commitFuture; |
206 |
| - } |
207 |
| - else |
208 |
| - { |
209 |
| - commitFuture = new CompletableFuture<>(); |
210 |
| - } |
211 |
| - return resultStage; |
212 |
| - } ); |
213 |
| - |
214 |
| - if ( stage == null ) |
215 |
| - { |
216 |
| - stage = resultCursors |
217 |
| - .retrieveNotConsumedError() |
218 |
| - .thenCompose( error -> doCommitAsync( error ).handle( handleCommitOrRollback( error ) ) ) |
219 |
| - .whenComplete( ( ignore, error ) -> handleTransactionCompletion( true, error ) ); |
220 |
| - stage.whenComplete( ( result, error ) -> |
221 |
| - { |
222 |
| - if ( error != null ) |
223 |
| - { |
224 |
| - commitFuture.completeExceptionally( error ); |
225 |
| - } |
226 |
| - else |
227 |
| - { |
228 |
| - commitFuture.complete( result ); |
229 |
| - } |
230 |
| - } ); |
231 |
| - } |
232 |
| - |
233 |
| - return stage; |
| 142 | + return closeAsync( true, false ); |
234 | 143 | }
|
235 | 144 |
|
236 | 145 | public CompletionStage<Void> rollbackAsync()
|
237 | 146 | {
|
238 |
| - CompletionStage<Void> stage = executeWithLock( lock, () -> |
239 |
| - { |
240 |
| - CompletionStage<Void> resultStage = null; |
241 |
| - if ( state == State.COMMITTED ) |
242 |
| - { |
243 |
| - resultStage = failedFuture( new ClientException( CANT_ROLLBACK_COMMITTED_MSG ) ); |
244 |
| - } |
245 |
| - else if ( state == State.ROLLED_BACK ) |
246 |
| - { |
247 |
| - resultStage = failedFuture( new ClientException( CANT_ROLLBACK_ROLLED_BACK_MSG ) ); |
248 |
| - } |
249 |
| - else if ( commitFuture != null ) |
250 |
| - { |
251 |
| - resultStage = failedFuture( new ClientException( CANT_ROLLBACK_COMMITTING_MSG ) ); |
252 |
| - } |
253 |
| - else if ( rollbackFuture != null ) |
254 |
| - { |
255 |
| - resultStage = rollbackFuture; |
256 |
| - } |
257 |
| - else |
258 |
| - { |
259 |
| - rollbackFuture = new CompletableFuture<>(); |
260 |
| - } |
261 |
| - return resultStage; |
262 |
| - } ); |
263 |
| - |
264 |
| - if ( stage == null ) |
265 |
| - { |
266 |
| - stage = resultCursors |
267 |
| - .retrieveNotConsumedError() |
268 |
| - .thenCompose( error -> doRollbackAsync().handle( handleCommitOrRollback( error ) ) ) |
269 |
| - .whenComplete( ( ignore, error ) -> handleTransactionCompletion( false, error ) ); |
270 |
| - stage.whenComplete( ( result, error ) -> |
271 |
| - { |
272 |
| - if ( error != null ) |
273 |
| - { |
274 |
| - rollbackFuture.completeExceptionally( error ); |
275 |
| - } |
276 |
| - else |
277 |
| - { |
278 |
| - rollbackFuture.complete( result ); |
279 |
| - } |
280 |
| - } ); |
281 |
| - } |
282 |
| - |
283 |
| - return stage; |
| 147 | + return closeAsync( false, false ); |
284 | 148 | }
|
285 | 149 |
|
286 | 150 | public CompletionStage<ResultCursor> runAsync( Query query )
|
@@ -383,7 +247,7 @@ private static BiFunction<Void,Throwable,Void> handleCommitOrRollback( Throwable
|
383 | 247 | {
|
384 | 248 | return ( ignore, commitOrRollbackError ) ->
|
385 | 249 | {
|
386 |
| - CompletionException combinedError = Futures.combineErrors( cursorFailure, commitOrRollbackError ); |
| 250 | + CompletionException combinedError = combineErrors( cursorFailure, commitOrRollbackError ); |
387 | 251 | if ( combinedError != null )
|
388 | 252 | {
|
389 | 253 | throw combinedError;
|
@@ -418,4 +282,81 @@ else if ( throwable instanceof ConnectionReadTimeoutException )
|
418 | 282 | connection.release(); // release in background
|
419 | 283 | }
|
420 | 284 | }
|
| 285 | + |
| 286 | + private CompletionStage<Void> closeAsync( boolean commit, boolean completeWithNullIfNotOpen ) |
| 287 | + { |
| 288 | + CompletionStage<Void> stage = executeWithLock( lock, () -> |
| 289 | + { |
| 290 | + CompletionStage<Void> resultStage = null; |
| 291 | + if ( completeWithNullIfNotOpen && !isOpen() ) |
| 292 | + { |
| 293 | + resultStage = completedWithNull(); |
| 294 | + } |
| 295 | + else if ( state == State.COMMITTED ) |
| 296 | + { |
| 297 | + resultStage = failedFuture( new ClientException( commit ? CANT_COMMIT_COMMITTED_MSG : CANT_ROLLBACK_COMMITTED_MSG ) ); |
| 298 | + } |
| 299 | + else if ( state == State.ROLLED_BACK ) |
| 300 | + { |
| 301 | + resultStage = failedFuture( new ClientException( commit ? CANT_COMMIT_ROLLED_BACK_MSG : CANT_ROLLBACK_ROLLED_BACK_MSG ) ); |
| 302 | + } |
| 303 | + else |
| 304 | + { |
| 305 | + if ( commit ) |
| 306 | + { |
| 307 | + if ( rollbackFuture != null ) |
| 308 | + { |
| 309 | + resultStage = failedFuture( new ClientException( CANT_COMMIT_ROLLING_BACK_MSG ) ); |
| 310 | + } |
| 311 | + else if ( commitFuture != null ) |
| 312 | + { |
| 313 | + resultStage = commitFuture; |
| 314 | + } |
| 315 | + else |
| 316 | + { |
| 317 | + commitFuture = new CompletableFuture<>(); |
| 318 | + } |
| 319 | + } |
| 320 | + else |
| 321 | + { |
| 322 | + if ( commitFuture != null ) |
| 323 | + { |
| 324 | + resultStage = failedFuture( new ClientException( CANT_ROLLBACK_COMMITTING_MSG ) ); |
| 325 | + } |
| 326 | + else if ( rollbackFuture != null ) |
| 327 | + { |
| 328 | + resultStage = rollbackFuture; |
| 329 | + } |
| 330 | + else |
| 331 | + { |
| 332 | + rollbackFuture = new CompletableFuture<>(); |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + return resultStage; |
| 337 | + } ); |
| 338 | + |
| 339 | + if ( stage == null ) |
| 340 | + { |
| 341 | + CompletableFuture<Void> targetFuture; |
| 342 | + Function<Throwable,CompletionStage<Void>> targetAction; |
| 343 | + if ( commit ) |
| 344 | + { |
| 345 | + targetFuture = commitFuture; |
| 346 | + targetAction = throwable -> doCommitAsync( throwable ).handle( handleCommitOrRollback( throwable ) ); |
| 347 | + } |
| 348 | + else |
| 349 | + { |
| 350 | + targetFuture = rollbackFuture; |
| 351 | + targetAction = throwable -> doRollbackAsync().handle( handleCommitOrRollback( throwable ) ); |
| 352 | + } |
| 353 | + resultCursors.retrieveNotConsumedError() |
| 354 | + .thenCompose( targetAction ) |
| 355 | + .whenComplete( ( ignored, throwable ) -> handleTransactionCompletion( commit, throwable ) ) |
| 356 | + .whenComplete( futureCompletingConsumer( targetFuture ) ); |
| 357 | + stage = targetFuture; |
| 358 | + } |
| 359 | + |
| 360 | + return stage; |
| 361 | + } |
421 | 362 | }
|
0 commit comments