Java源码示例:io.vertx.sqlclient.Transaction

示例1
protected void initConnector() {
  connector = handler -> {
    Pool pool = getPool();
    pool.getConnection(ar1 -> {
      if (ar1.succeeded()) {
        SqlConnection conn = ar1.result();
        conn.begin(ar2 -> {
          if (ar2.succeeded()) {
            Transaction tx = ar2.result();
            tx.completion().onComplete(ar3 -> {
              conn.close();
            });
            handler.handle(Future.succeededFuture(new Result(conn, tx)));
          } else {
            conn.close();
          }
        });
      } else {
        handler.handle(ar1.mapEmpty());
      }
    });
  };
}
 
示例2
void selectStream(AsyncResult<SQLConnection> conn, String sql, Tuple params, int chunkSize,
    Handler<AsyncResult<RowStream<Row>>> replyHandler) {
  try {
    if (conn.failed()) {
      replyHandler.handle(Future.failedFuture(conn.cause()));
      return;
    }
    final Transaction tx = conn.result().tx;
    tx.prepare(sql, res -> {
      if (res.failed()) {
        log.error(res.cause().getMessage(), res.cause());
        replyHandler.handle(Future.failedFuture(res.cause()));
        return;
      }
      PreparedStatement pq = res.result();
      RowStream<Row> rowStream = pq.createStream(chunkSize, params);
      replyHandler.handle(Future.succeededFuture(rowStream));
    });
  } catch (Exception e) {
    log.error("select stream sql: " + e.getMessage() + " - " + sql, e);
    replyHandler.handle(Future.failedFuture(e));
  }
}
 
示例3
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Cursors require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Create a cursor
          Cursor cursor = pq.cursor(Tuple.of("julien"));

          // Read 50 rows
          cursor.read(50, ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();

              // Check for more ?
              if (cursor.hasMore()) {
                // Repeat the process...
              } else {
                // No more rows - commit the transaction
                tx.commit();
              }
            }
          });
        }
      });
    }
  });
}
 
示例4
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Streams require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Fetch 50 rows at a time
          RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

          // Use the stream
          stream.exceptionHandler(err -> {
            System.out.println("Error: " + err.getMessage());
          });
          stream.endHandler(v -> {
            tx.commit();
            System.out.println("End of stream");
          });
          stream.handler(row -> {
            System.out.println("User: " + row.getString("last_name"));
          });
        }
      });
    }
  });
}
 
示例5
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Cursors require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Create a cursor
          Cursor cursor = pq.cursor(Tuple.of("julien"));

          // Read 50 rows
          cursor.read(50, ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();

              // Check for more ?
              if (cursor.hasMore()) {
                // Repeat the process...
              } else {
                // No more rows - commit the transaction
                tx.commit();
              }
            }
          });
        }
      });
    }
  });
}
 
示例6
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Streams require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Fetch 50 rows at a time
          RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

          // Use the stream
          stream.exceptionHandler(err -> {
            System.out.println("Error: " + err.getMessage());
          });
          stream.endHandler(v -> {
            tx.commit();
            System.out.println("End of stream");
          });
          stream.handler(row -> {
            System.out.println("User: " + row.getString("last_name"));
          });
        }
      });
    }
  });
}
 
示例7
public void usingCursors01(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Cursors require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Create a cursor
          Cursor cursor = pq.cursor(Tuple.of("julien"));

          // Read 50 rows
          cursor.read(50, ar2 -> {
            if (ar2.succeeded()) {
              RowSet<Row> rows = ar2.result();

              // Check for more ?
              if (cursor.hasMore()) {
                // Repeat the process...
              } else {
                // No more rows - commit the transaction
                tx.commit();
              }
            }
          });
        }
      });
    }
  });
}
 
示例8
public void usingCursors03(SqlConnection connection) {
  connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar0 -> {
    if (ar0.succeeded()) {
      PreparedStatement pq = ar0.result();

      // Streams require to run within a transaction
      connection.begin(ar1 -> {
        if (ar1.succeeded()) {
          Transaction tx = ar1.result();

          // Fetch 50 rows at a time
          RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

          // Use the stream
          stream.exceptionHandler(err -> {
            System.out.println("Error: " + err.getMessage());
          });
          stream.endHandler(v -> {
            tx.commit();
            System.out.println("End of stream");
          });
          stream.handler(row -> {
            System.out.println("User: " + row.getString("last_name"));
          });
        }
      });
    }
  });
}
 
示例9
private synchronized void afterBegin(AsyncResult<Transaction> ar) {
  if (ar.succeeded()) {
    status = ST_PENDING;
  } else {
    status = ST_COMPLETED;
  }
  checkPending();
}
 
示例10
@Override
public Future<Transaction> begin() {
  if (tx != null) {
    throw new IllegalStateException();
  }
  tx = new TransactionImpl(context, conn);
  tx.completion().onComplete(ar -> {
    tx = null;
  });
  return tx.begin();
}
 
示例11
/**
 * @return an instance of a <code>ReactiveClassicGenericQueryExecutor</code> that performs all CRUD
 * functions in the scope of a transaction. The transaction has to be committed/rolled back by calling <code>commit</code>
 * or <code>rollback</code> on the QueryExecutor returned.
 */
public Future<? extends ReactiveClassicGenericQueryExecutor> beginTransaction(){
    if(delegate instanceof Transaction){
        throw new IllegalStateException("Already in transaction");
    }
    Promise<Transaction> transactionPromise = Promise.promise();
    ((Pool) delegate).begin(transactionPromise);
    return transactionPromise.future().map(newInstance());
}
 
示例12
/**
 * Rolls a transaction back.
 * @return a <code>Future</code> that completes when the transaction has been rolled back.
 * @throws IllegalStateException if not called <code>beginTransaction</code> before.
 */
public Future<Void> rollback(){
    if(!(delegate instanceof Transaction)){
        throw new IllegalStateException("Not in transaction");
    }
    Promise<Void> commit = Promise.promise();
    ((Transaction) delegate).rollback(commit);
    return commit.future();
}
 
示例13
/**
 * @return an instance of a <code>ReactiveCompletableFutureGenericQueryExecutor</code> that performs all CRUD
 * functions in the scope of a transaction. The transaction has to be committed/rolled back by calling <code>commit</code>
 * or <code>rollback</code> on the QueryExecutor returned.
 */
public CompletableFuture<? extends ReactiveCompletableFutureGenericQueryExecutor> beginTransaction(){
    if(delegate instanceof Transaction){
        throw new IllegalStateException("Already in transaction");
    }
    CompletableFuture<Transaction> transactionFuture = new VertxCompletableFuture<>(vertx);
    ((Pool) delegate).begin(createCompletionHandler(transactionFuture));
    return transactionFuture.thenApply(newInstance());
}
 
示例14
/**
 * Rolls a transaction back.
 * @return a <code>CompletableFuture</code> that completes when the transaction has been rolled back.
 * @throws IllegalStateException if not called <code>beginTransaction</code> before.
 */
public CompletableFuture<Void> rollback(){
    if(!(delegate instanceof Transaction)){
        throw new IllegalStateException("Not in transaction");
    }
    CompletableFuture<Void> commit = new VertxCompletableFuture<>(vertx);
    ((Transaction) delegate).rollback(createCompletionHandler(commit));
    return commit;
}
 
示例15
@Override
public void begin(Handler<AsyncResult<Transaction>> handler) {
    pool().begin(handler);
}
 
示例16
public void transaction01(Pool pool) {
  pool.getConnection(res -> {
    if (res.succeeded()) {

      // Transaction must use a connection
      SqlConnection conn = res.result();

      // Begin the transaction
      conn.begin(ar0 -> {
        if (ar0.succeeded()) {
          Transaction tx = ar0.result();

          // Various statements
          conn
            .query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')")
            .execute(ar1 -> {
              if (ar1.succeeded()) {
                conn
                  .query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')")
                  .execute(ar2 -> {
                    if (ar2.succeeded()) {
                      // Commit the transaction
                      tx.commit(ar3 -> {
                        if (ar3.succeeded()) {
                          System.out.println("Transaction succeeded");
                        } else {
                          System.out.println("Transaction failed " + ar3.cause().getMessage());
                        }
                        // Return the connection to the pool
                        conn.close();
                      });
                    } else {
                      // Return the connection to the pool
                      conn.close();
                    }
                  });
              } else {
                // Return the connection to the pool
                conn.close();
              }
            });
        } else {
          // Return the connection to the pool
          conn.close();
        }
      });
    }
  });
}
 
示例17
public void transaction02(Transaction tx) {
  tx.completion().onFailure(err -> {
    System.out.println("Transaction failed => rollbacked");
  });
}
 
示例18
public void transaction01(Pool pool) {
  pool.getConnection(res -> {
    if (res.succeeded()) {

      // Transaction must use a connection
      SqlConnection conn = res.result();

      // Begin the transaction
      conn.begin(ar0 -> {
        if (ar0.succeeded()) {
          Transaction tx = ar0.result();
          // Various statements
          conn
            .query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')")
            .execute(ar1 -> {
              if (ar1.succeeded()) {
                conn
                  .query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')")
                  .execute(ar2 -> {
                    if (ar2.succeeded()) {
                      // Commit the transaction
                      tx.commit(ar3 -> {
                        if (ar3.succeeded()) {
                          System.out.println("Transaction succeeded");
                        } else {
                          System.out.println("Transaction failed " + ar3.cause().getMessage());
                        }
                        // Return the connection to the pool
                        conn.close();
                      });
                    } else {
                      // Return the connection to the pool
                      conn.close();
                    }
                  });
              } else {
                // Return the connection to the pool
                conn.close();
              }
            });
        } else {
          // Return the connection to the pool
          conn.close();
        }
      });
    }
  });
}
 
示例19
public void transaction02(Transaction tx) {
  tx.completion().onFailure(err -> {
    System.out.println("Transaction failed => rollbacked");
  });
}
 
示例20
public void transaction01(Pool pool) {
  pool.getConnection(res -> {
    if (res.succeeded()) {

      // Transaction must use a connection
      SqlConnection conn = res.result();

      // Begin the transaction
      conn.begin(ar0 -> {
        if (ar0.succeeded()) {
          Transaction tx = ar0.result();

          // Various statements
          conn
            .query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')")
            .execute(ar1 -> {
              if (ar1.succeeded()) {
                conn
                  .query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')")
                  .execute(ar2 -> {
                    if (ar2.succeeded()) {
                      // Commit the transaction
                      tx.commit(ar3 -> {
                        if (ar3.succeeded()) {
                          System.out.println("Transaction succeeded");
                        } else {
                          System.out.println("Transaction failed " + ar3.cause().getMessage());
                        }
                        // Return the connection to the pool
                        conn.close();
                      });
                    } else {
                      // Return the connection to the pool
                      conn.close();
                    }
                  });
              } else {
                // Return the connection to the pool
                conn.close();
              }
            });
        } else {
          // Return the connection to the pool
          conn.close();
        }
      });
    }
  });
}
 
示例21
public void transaction02(Transaction tx) {
  tx.completion().onFailure(err -> {
    System.out.println("Transaction failed => rollbacked");
  });
}
 
示例22
Future<Transaction> begin() {
  PromiseInternal<Transaction> promise = context.promise(this::afterBegin);
  ScheduledCommand<Transaction> b = doQuery(new TxCommand<>(TxCommand.Kind.BEGIN, this), promise);
  doSchedule(b.cmd, b.handler);
  return promise.future();
}
 
示例23
@Override
public void begin(Handler<AsyncResult<Transaction>> handler) {
  Future<Transaction> fut = begin();
  fut.onComplete(handler);
}
 
示例24
public Result(SqlClient client, Transaction tx) {
  this.client = client;
  this.tx = tx;
}
 
示例25
public void transaction01(Pool pool) {
  pool.getConnection(res -> {
    if (res.succeeded()) {

      // Transaction must use a connection
      SqlConnection conn = res.result();

      // Begin the transaction
      conn.begin(ar0 -> {
        if (ar0.succeeded()) {
          Transaction tx = ar0.result();
          // Various statements
          conn
            .query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')")
            .execute(ar1 -> {
              if (ar1.succeeded()) {
                conn
                  .query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')")
                  .execute(ar2 -> {
                    if (ar2.succeeded()) {
                      // Commit the transaction
                      tx.commit(ar3 -> {
                        if (ar3.succeeded()) {
                          System.out.println("Transaction succeeded");
                        } else {
                          System.out.println("Transaction failed " + ar3.cause().getMessage());
                        }
                        // Return the connection to the pool
                        conn.close();
                      });
                    } else {
                      // Return the connection to the pool
                      conn.close();
                    }
                  });
              } else {
                // Return the connection to the pool
                conn.close();
              }
            });
        } else {
          // Return the connection to the pool
          conn.close();
        }
      });
    }
  });
}
 
示例26
public void transaction02(Transaction tx) {
  tx.completion().onFailure(err -> {
    System.out.println("Transaction failed => rollbacked");
  });
}
 
示例27
@Override
public Transaction begin() {
  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
 
示例28
public SQLConnection(PgConnection conn, Transaction tx, Long timerId) {
  this.conn = conn;
  this.tx = tx;
  this.timerId = timerId;
}
 
示例29
@Override
public Transaction begin() {
  return null;
}
 
示例30
@Override
protected Function<Transaction, ReactiveClassicQueryExecutor<R,P,T>> newInstance() {
    return pgTransaction -> new ReactiveClassicQueryExecutor<>(configuration(), pgTransaction, pojoMapper);
}