use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class BaseSavepointSuite method baseSavepointCommitSavepointInTwoConnection.
@Test
@SneakyThrows
public void baseSavepointCommitSavepointInTwoConnection(VertxTestContext testContext) {
mySQLManager.getConnection("ds1").flatMap(connection -> {
return connection.update("delete FROM `db1`.`travelrecord`").map(u -> connection);
}).flatMap(c -> c.close()).toCompletionStage().toCompletableFuture().get();
mySQLManager.getConnection("ds2").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) {
NewMycatConnection ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
ds1.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
NewMycatConnection ds2 = savepointSqlConnection.getConnection("ds2").toCompletionStage().toCompletableFuture().get();
ds2.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
savepointSqlConnection.createSavepoint("sss").toCompletionStage().toCompletableFuture().get();
RowSet objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
RowSet objects2 = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects2.size() > 0);
ds1.update("delete FROM `db1`.`travelrecord`").toCompletionStage().toCompletableFuture().get();
ds2.update("delete FROM `db1`.`travelrecord`").toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
savepointSqlConnection.commit().toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
Assert.assertFalse(savepointSqlConnection.isInTransaction());
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);
ds2 = savepointSqlConnection.getConnection("ds2").toCompletionStage().toCompletableFuture().get();
objects = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() == 0);
savepointSqlConnection.close();
testContext.completeNow();
}
});
}
use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class BaseSavepointSuite method baseSavepointRollbackSavepointInTwoConnection.
@Test
@SneakyThrows
public void baseSavepointRollbackSavepointInTwoConnection(VertxTestContext testContext) {
mySQLManager.getConnection("ds1").flatMap(connection -> {
return connection.update("delete FROM `db1`.`travelrecord`").map(u -> connection);
}).flatMap(c -> c.close()).toCompletionStage().toCompletableFuture().get();
mySQLManager.getConnection("ds2").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) {
NewMycatConnection ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
ds1.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
NewMycatConnection ds2 = savepointSqlConnection.getConnection("ds2").toCompletionStage().toCompletableFuture().get();
ds2.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
savepointSqlConnection.createSavepoint("sss").toCompletionStage().toCompletableFuture().get();
RowSet objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
RowSet objects2 = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects2.size() > 0);
ds1.update("delete FROM `db1`.`travelrecord`").toCompletionStage().toCompletableFuture().get();
ds2.update("delete FROM `db1`.`travelrecord`").toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
savepointSqlConnection.rollbackSavepoint("sss").toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
Assert.assertTrue(savepointSqlConnection.isInTransaction());
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);
ds2 = savepointSqlConnection.getConnection("ds2").toCompletionStage().toCompletableFuture().get();
objects = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
savepointSqlConnection.commit().toCompletionStage().toCompletableFuture().get();
Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
Assert.assertFalse(savepointSqlConnection.isInTransaction());
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);
ds2 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
objects = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
Assert.assertTrue(objects.size() > 0);
savepointSqlConnection.close();
testContext.completeNow();
}
});
}
use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class XaTestSuite method beginDoubleTargetInsertButCommitFail.
@Test
public void beginDoubleTargetInsertButCommitFail(VertxTestContext testContext) throws Exception {
clearData();
XaSqlConnection baseXaSqlConnection = factory.apply(mySQLManager, xaLog);
baseXaSqlConnection.begin().onComplete(event -> {
Assertions.assertTrue(event.succeeded());
Future<NewMycatConnection> ds1 = baseXaSqlConnection.getConnection("ds1");
Future<NewMycatConnection> ds2 = baseXaSqlConnection.getConnection("ds2");
CompositeFuture all = CompositeFuture.join(ds1.compose(connection -> {
Future<SqlResult> future = connection.update("INSERT INTO db1.travelrecord (id)\n" + " VALUES\n" + " (1);");
return future.compose(rowSet -> {
Assertions.assertEquals(1, rowSet.getAffectRows());
return Future.succeededFuture(connection);
});
}), ds2.compose(connection -> {
Future<SqlResult> future = connection.insert("INSERT INTO db1.travelrecord (id)\n" + " VALUES\n" + " (2);");
return future.compose(rowSet -> {
Assertions.assertEquals(1, rowSet.getAffectRows());
return Future.succeededFuture(connection);
});
}));
all.onComplete(event13 -> {
Assertions.assertTrue(event13.succeeded());
Future<Void> future = baseXaSqlConnection.commitXa((log) -> Future.failedFuture("commit fail"));
future.onComplete(new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> event) {
Assertions.assertTrue(event.failed());
baseXaSqlConnection.commit().onComplete(new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> event) {
Assertions.assertTrue(event.succeeded());
Assertions.assertFalse(baseXaSqlConnection.isInTransaction());
Future<NewMycatConnection> connectionFuture = baseXaSqlConnection.getConnection("ds1");
connectionFuture.compose(sqlConnection -> sqlConnection.query("select id from db1.travelrecord")).onComplete(event1 -> {
Assertions.assertTrue(event1.succeeded());
Assertions.assertEquals(1, event1.result().size());
testContext.completeNow();
});
}
});
}
});
});
});
}
use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class AsyncMycatDataContextImpl method recycleConnection.
public synchronized void recycleConnection(String key, Future<NewMycatConnection> connectionFuture) {
XaSqlConnection transactionSession = (XaSqlConnection) context.getTransactionSession();
if (context.isInTransaction()) {
transactionConnnectionMap.put(key, connectionFuture);
return;
}
connectionFuture = connectionFuture.flatMap(c -> c.close().mapEmpty());
transactionSession.addCloseFuture(connectionFuture.mapEmpty());
connnectionFutureCollection.add(Objects.requireNonNull(connectionFuture));
}
use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.
the class SequenceSchedulePolicy method getConnetion.
@Override
public Future<NewMycatConnection> getConnetion(MycatDataContext dataContext, int order, int refCount, String targetArg, long deadline, Future<NewMycatConnection> recycleConnectionFuture) {
synchronized (futures) {
String target = dataContext.resolveDatasourceTargetName(targetArg, true);
Future<NewMycatConnection> sqlConnectionFuture = futures.get(target);
if (sqlConnectionFuture == null) {
futures.put(target, recycleConnectionFuture);
XaSqlConnection transactionSession = (XaSqlConnection) dataContext.getTransactionSession();
return transactionSession.getConnection(target);
} else {
Promise<NewMycatConnection> promise = Promise.promise();
futures.put(target, recycleConnectionFuture);
sqlConnectionFuture.onComplete(promise);
return promise.future();
}
}
}
Aggregations