Search in sources :

Example 31 with XaSqlConnection

use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.

the class BaseSavepointSuite method baseSavepointReleaseSavepointInTwoConnection.

@Test
@SneakyThrows
public void baseSavepointReleaseSavepointInTwoConnection(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);
            Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
            savepointSqlConnection.releaseSavepoint("sss").toCompletionStage().toCompletableFuture().get();
            objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
            Assert.assertTrue(objects.size() > 0);
            objects2 = ds2.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
            Assert.assertTrue(objects2.size() > 0);
            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();
        }
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) NewMycatConnection(io.mycat.newquery.NewMycatConnection) SneakyThrows(lombok.SneakyThrows) RowSet(io.mycat.newquery.RowSet) BiFunction(java.util.function.BiFunction) XaLog(cn.mycat.vertx.xa.XaLog) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) AsyncResult(io.vertx.core.AsyncResult) MySQLManager(cn.mycat.vertx.xa.MySQLManager) SavepointSqlConnection(cn.mycat.vertx.xa.SavepointSqlConnection) Handler(io.vertx.core.Handler) Assert(org.junit.Assert) NewMycatConnection(io.mycat.newquery.NewMycatConnection) SavepointSqlConnection(cn.mycat.vertx.xa.SavepointSqlConnection) RowSet(io.mycat.newquery.RowSet) SneakyThrows(lombok.SneakyThrows) AsyncResult(io.vertx.core.AsyncResult) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) Test(org.junit.jupiter.api.Test) SneakyThrows(lombok.SneakyThrows)

Example 32 with XaSqlConnection

use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.

the class BaseSavepointSuite method baseSavepointReleaseSavepointInSingleConnection.

@Test
@SneakyThrows
public void baseSavepointReleaseSavepointInSingleConnection(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) {
            NewMycatConnection ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
            ds1.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);
            Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
            savepointSqlConnection.releaseSavepoint("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);
            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);
            savepointSqlConnection.close();
            testContext.completeNow();
        }
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) NewMycatConnection(io.mycat.newquery.NewMycatConnection) SneakyThrows(lombok.SneakyThrows) RowSet(io.mycat.newquery.RowSet) BiFunction(java.util.function.BiFunction) XaLog(cn.mycat.vertx.xa.XaLog) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) AsyncResult(io.vertx.core.AsyncResult) MySQLManager(cn.mycat.vertx.xa.MySQLManager) SavepointSqlConnection(cn.mycat.vertx.xa.SavepointSqlConnection) Handler(io.vertx.core.Handler) Assert(org.junit.Assert) NewMycatConnection(io.mycat.newquery.NewMycatConnection) SavepointSqlConnection(cn.mycat.vertx.xa.SavepointSqlConnection) RowSet(io.mycat.newquery.RowSet) SneakyThrows(lombok.SneakyThrows) AsyncResult(io.vertx.core.AsyncResult) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) Test(org.junit.jupiter.api.Test) SneakyThrows(lombok.SneakyThrows)

Example 33 with XaSqlConnection

use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.

the class BaseSavepointSuite method baseSavepointRelease.

@Test
public void baseSavepointRelease(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.releaseSavepoint("sss").toCompletionStage().toCompletableFuture().get();
            Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
            testContext.completeNow();
        }
    });
}
Also used : SavepointSqlConnection(cn.mycat.vertx.xa.SavepointSqlConnection) SneakyThrows(lombok.SneakyThrows) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) Test(org.junit.jupiter.api.Test)

Example 34 with XaSqlConnection

use of cn.mycat.vertx.xa.XaSqlConnection in project Mycat2 by MyCATApache.

the class MycatDataContextImpl method switchTransaction.

@Override
public void switchTransaction(TransactionType transactionSessionType) {
    Objects.requireNonNull(transactionSessionType);
    TransactionSession transactionSession = null;
    XaSqlConnection connection;
    switch(transactionSessionType) {
        case PROXY_TRANSACTION_TYPE:
            connection = new LocalSqlConnection(() -> MetaClusterCurrent.wrapper(MySQLManager.class), MetaClusterCurrent.wrapper(XaLog.class));
            break;
        case JDBC_TRANSACTION_TYPE:
            connection = new LocalXaSqlConnection(() -> MetaClusterCurrent.wrapper(MySQLManager.class), MetaClusterCurrent.wrapper(XaLog.class));
            break;
        default:
            throw new IllegalStateException("Unexpected transaction type: " + transactionSessionType);
    }
    connection = new SavepointSqlConnection(connection);
    transactionSession = new MycatXaTranscation(connection, transactionSessionType);
    if (this.transactionSession != null) {
        this.transactionSession.deliverTo(transactionSession);
    }
    this.transactionSession = transactionSession;
}
Also used : LocalXaSqlConnection(cn.mycat.vertx.xa.impl.LocalXaSqlConnection) SavepointSqlConnection(cn.mycat.vertx.xa.SavepointSqlConnection) LocalSqlConnection(cn.mycat.vertx.xa.impl.LocalSqlConnection) LocalXaSqlConnection(cn.mycat.vertx.xa.impl.LocalXaSqlConnection) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection)

Aggregations

XaSqlConnection (cn.mycat.vertx.xa.XaSqlConnection)34 AsyncResult (io.vertx.core.AsyncResult)26 Test (org.junit.jupiter.api.Test)26 Future (io.vertx.core.Future)21 NewMycatConnection (io.mycat.newquery.NewMycatConnection)20 Handler (io.vertx.core.Handler)18 MySQLManager (cn.mycat.vertx.xa.MySQLManager)17 RowSet (io.mycat.newquery.RowSet)16 XaLog (cn.mycat.vertx.xa.XaLog)15 VertxTestContext (io.vertx.junit5.VertxTestContext)15 BiFunction (java.util.function.BiFunction)15 SavepointSqlConnection (cn.mycat.vertx.xa.SavepointSqlConnection)14 SneakyThrows (lombok.SneakyThrows)14 Assert (org.junit.Assert)9 CompositeFuture (io.vertx.core.CompositeFuture)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 Function (java.util.function.Function)8 SqlResult (io.mycat.newquery.SqlResult)7 Row (io.vertx.sqlclient.Row)7 SqlConnection (io.vertx.sqlclient.SqlConnection)7