use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class VertxExecuter method simpleUpdate.
public static Future<long[]> simpleUpdate(MycatDataContext context, boolean insert, boolean xa, boolean onlyFirstSum, Iterable<EachSQL> eachSQLs) {
Function<Void, Future<long[]>> function = new Function<Void, Future<long[]>>() {
final long[] sum = new long[] { 0, 0 };
@Override
public Future<long[]> apply(Void unused) {
XaSqlConnection transactionSession = (XaSqlConnection) context.getTransactionSession();
ConcurrentHashMap<String, Future<NewMycatConnection>> map = new ConcurrentHashMap<>();
final AtomicBoolean firstRequest = new AtomicBoolean(true);
for (EachSQL eachSQL : eachSQLs) {
String target = context.resolveDatasourceTargetName(eachSQL.getTarget(), true);
String sql = eachSQL.getSql();
List<Object> params = eachSQL.getParams();
Future<NewMycatConnection> connectionFuture = map.computeIfAbsent(target, s -> transactionSession.getConnection(target));
Future<long[]> future;
if (insert) {
future = VertxExecuter.runInsert(connectionFuture, sql, params);
} else {
future = VertxExecuter.runUpdate(connectionFuture, sql, params);
}
Future<NewMycatConnection> returnConnectionFuture = future.map((Function<long[], Void>) longs2 -> {
if (!onlyFirstSum) {
synchronized (sum) {
sum[0] = sum[0] + longs2[0];
sum[1] = Math.max(sum[1], longs2[1]);
}
} else if (firstRequest.compareAndSet(true, false)) {
sum[0] = longs2[0];
sum[1] = longs2[1];
}
return null;
}).mapEmpty().flatMap(c -> Future.succeededFuture(connectionFuture.result()));
map.put(target, returnConnectionFuture);
}
List<Future<NewMycatConnection>> futures = new ArrayList<>(map.values());
return CompositeFuture.join((List) futures).map(sum);
}
};
if (xa) {
return wrapAsXaTransaction(context, function);
} else {
return Future.succeededFuture().flatMap(o -> function.apply(null));
}
}
use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class SqlResultSetService method loadResultSet.
@SneakyThrows
private Optional<Observable<MysqlPayloadObject>> loadResultSet(SQLSelectStatement sqlSelectStatement) {
return cache.get(sqlSelectStatement.toString(), () -> {
if (!MetaClusterCurrent.exist(DrdsSqlCompiler.class)) {
return Optional.empty();
}
MycatDataContext context = new MycatDataContextImpl();
try {
DrdsSqlWithParams drdsSql = DrdsRunnerHelper.preParse(sqlSelectStatement, context.getDefaultSchema());
Plan plan = DrdsRunnerHelper.getPlan(drdsSql);
XaSqlConnection transactionSession = (XaSqlConnection) context.getTransactionSession();
AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = new AsyncMycatDataContextImpl.SqlMycatDataContextImpl(context, plan.getCodeExecuterContext(), drdsSql);
PrepareExecutor prepare = MetaClusterCurrent.wrapper(ExecutorProvider.class).prepare(plan);
Observable<MysqlPayloadObject> observable = prepare.getMysqlPayloadObjectObservable(prepare.getArrayBindable(), sqlMycatDataContext, plan.getMetaData());
observable = observable.doOnTerminate(new Action() {
@Override
public void run() throws Throwable {
transactionSession.closeStatementState().onComplete(event -> context.close());
}
});
List<MysqlPayloadObject> mysqlPayloadObjects = observable.toList().blockingGet();
observable = Observable.fromIterable(mysqlPayloadObjects);
return Optional.ofNullable(observable);
} catch (Throwable t) {
context.kill();
log.error("", t);
return Optional.empty();
}
});
}
use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class BaseSavepointSuite method baseSavepointRollbackSavepoint.
@Test
public void baseSavepointRollbackSavepoint(VertxTestContext testContext) {
XaSqlConnection baseXaSqlConnection = factory.apply(mySQLManager, xaLog);
Assert.assertTrue(baseXaSqlConnection instanceof SavepointSqlConnection);
SavepointSqlConnection savepointSqlConnection = (SavepointSqlConnection) baseXaSqlConnection;
baseXaSqlConnection.begin().onComplete(new Handler<AsyncResult<Void>>() {
@Override
@SneakyThrows
public void handle(AsyncResult<Void> event) {
Future<Void> sss = savepointSqlConnection.createSavepoint("sss");
sss.toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
savepointSqlConnection.rollbackSavepoint("sss").toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
testContext.completeNow();
}
});
}
use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class BaseSavepointSuite method baseSavepointRollback.
@Test
public void baseSavepointRollback(VertxTestContext testContext) {
XaSqlConnection baseXaSqlConnection = factory.apply(mySQLManager, xaLog);
Assert.assertTrue(baseXaSqlConnection instanceof SavepointSqlConnection);
SavepointSqlConnection savepointSqlConnection = (SavepointSqlConnection) baseXaSqlConnection;
baseXaSqlConnection.begin().onComplete(new Handler<AsyncResult<Void>>() {
@Override
@SneakyThrows
public void handle(AsyncResult<Void> event) {
Future<Void> sss = savepointSqlConnection.createSavepoint("sss");
sss.toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
savepointSqlConnection.rollback();
Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
testContext.completeNow();
}
});
}
use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class BaseSavepointSuite method baseSavepointRollbackInSingleConnection.
@Test
@SneakyThrows
public void baseSavepointRollbackInSingleConnection(VertxTestContext testContext) {
mySQLManager.getConnection("ds1").flatMap(connection -> {
return connection.update("delete FROM `db1`.`travelrecord`").map(u -> connection);
}).flatMap(c -> c.close()).toCompletionStage().toCompletableFuture().get();
XaSqlConnection baseXaSqlConnection = factory.apply(mySQLManager, xaLog);
Assert.assertTrue(baseXaSqlConnection instanceof SavepointSqlConnection);
SavepointSqlConnection savepointSqlConnection = (SavepointSqlConnection) baseXaSqlConnection;
baseXaSqlConnection.begin().onComplete(new Handler<AsyncResult<Void>>() {
@Override
@SneakyThrows
public void handle(AsyncResult<Void> event) {
savepointSqlConnection.createSavepoint("sss").toCompletionStage().toCompletableFuture().get();
NewMycatConnection ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
ds1.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
RowSet objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
savepointSqlConnection.rollback().toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() == 0);
savepointSqlConnection.close();
testContext.completeNow();
}
});
}
Aggregations