예제 #1
0
 @Override
 public <T> ComposableFuture<T> withConnection(final TransactionHandler<T> handler) {
   final ComposableFuture<MySqlAsyncConnection> futureConn = take();
   return futureConn.continueOnSuccess(
       new FutureSuccessHandler<MySqlAsyncConnection, T>() {
         @Override
         public ComposableFuture<T> handle(final MySqlAsyncConnection conn) {
           return handler
               .handle(conn)
               .continueWith(
                   new FutureResultHandler<T, T>() {
                     @Override
                     public ComposableFuture<T> handle(final Try<T> result) {
                       return giveBack(conn)
                           .continueWith(
                               new FutureResultHandler<Boolean, T>() {
                                 @Override
                                 public ComposableFuture<T> handle(
                                     final Try<Boolean> giveBackResult) {
                                   return ComposableFutures.fromTry(result);
                                 }
                               });
                     }
                   });
         }
       });
 }
예제 #2
0
  private ComposableFuture<MySqlAsyncConnection> take() {
    final ComposableFuture<MySQLConnection> connFuture =
        ScalaFutureHelper.from(
            new ScalaFutureHelper.FutureProvider<MySQLConnection>() {
              @Override
              public Future<MySQLConnection> provide() {
                return _pool.take();
              }
            });

    return connFuture.continueOnSuccess(
        new SuccessHandler<MySQLConnection, MySqlAsyncConnection>() {
          @Override
          public MySqlAsyncConnection handle(final MySQLConnection result) {
            return new MySqlAsyncConnection(result);
          }
        });
  }
예제 #3
0
  @Override
  public <T> ComposableFuture<T> withTransaction(final TransactionHandler<T> handler) {
    final ComposableFuture<MySqlAsyncConnection> futureConn = take();
    return futureConn.continueOnSuccess(
        new FutureSuccessHandler<MySqlAsyncConnection, T>() {
          @Override
          public ComposableFuture<T> handle(final MySqlAsyncConnection conn) {
            return conn.startTx()
                .continueOnSuccess(
                    new FutureSuccessHandler<MySqlAsyncConnection, T>() {
                      @Override
                      public ComposableFuture<T> handle(final MySqlAsyncConnection result) {
                        return handler.handle(conn);
                      }
                    })
                .continueOnSuccess(
                    new FutureSuccessHandler<T, T>() {
                      @Override
                      public ComposableFuture<T> handle(final T result) {
                        return conn.commit()
                            .continueWith(
                                new FutureResultHandler<QueryResult, T>() {
                                  @Override
                                  public ComposableFuture<T> handle(
                                      final Try<QueryResult> commitResult) {
                                    return giveBack(conn)
                                        .continueWith(
                                            new FutureResultHandler<Boolean, T>() {
                                              @Override
                                              public ComposableFuture<T> handle(
                                                  final Try<Boolean> giveBackResult) {
                                                if (!giveBackResult.isSuccess()) {
                                                  logger.warn(
                                                      "can't return connection back to pool",
                                                      giveBackResult.getError());
                                                }

                                                if (commitResult.isSuccess()) {
                                                  return fromValue(result);
                                                } else {
                                                  return fromError(commitResult.getError());
                                                }
                                              }
                                            });
                                  }
                                });
                      }
                    })
                .continueOnError(
                    new FutureErrorHandler<T>() {
                      @Override
                      public ComposableFuture<T> handle(final Throwable error) {
                        return conn.rollBack()
                            .continueWith(
                                new FutureResultHandler<QueryResult, T>() {
                                  @Override
                                  public ComposableFuture<T> handle(
                                      final Try<QueryResult> rollBackResult) {
                                    return giveBack(conn)
                                        .continueWith(
                                            new FutureResultHandler<Boolean, T>() {
                                              @Override
                                              public ComposableFuture<T> handle(
                                                  final Try<Boolean> result) {
                                                if (!result.isSuccess()) {
                                                  logger.warn(
                                                      "can't return connection back to pool",
                                                      error);
                                                }
                                                return fromError(error);
                                              }
                                            });
                                  }
                                });
                      }
                    });
          }
        });
  }