Search in sources :

Example 6 with XaSqlConnection

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

the class XaTestSuite method beginSingleTargetInsertCommit.

@Test
public void beginSingleTargetInsertCommit(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");
        ds1.compose(connection -> {
            Future<SqlResult> future = connection.insert("INSERT INTO db1.travelrecord (id)\n" + "                       VALUES\n" + "                       (1);");
            return future.compose(rowSet -> {
                Assertions.assertEquals(1, rowSet.getAffectRows());
                return Future.succeededFuture(connection);
            });
        }).compose(connection -> {
            return connection.query("select id from db1.travelrecord").compose(rows -> {
                Assertions.assertEquals(1, rows.size());
                return Future.succeededFuture(connection);
            });
        }).onComplete(event13 -> {
            Assertions.assertTrue(event13.succeeded());
            baseXaSqlConnection.commit().onComplete(event12 -> {
                Assertions.assertTrue(event12.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();
                });
            });
        });
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Connection(java.sql.Connection) RowSet(io.mycat.newquery.RowSet) BiFunction(java.util.function.BiFunction) XaLog(cn.mycat.vertx.xa.XaLog) Disabled(org.junit.jupiter.api.Disabled) Function(java.util.function.Function) CompositeFuture(io.vertx.core.CompositeFuture) SimpleConfig(cn.mycat.vertx.xa.SimpleConfig) SQLException(java.sql.SQLException) SqlConnection(io.vertx.sqlclient.SqlConnection) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) MySQLManager(cn.mycat.vertx.xa.MySQLManager) JsonUtil(io.mycat.util.JsonUtil) NewMycatConnection(io.mycat.newquery.NewMycatConnection) XaLogImpl(cn.mycat.vertx.xa.impl.XaLogImpl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VertxExtension(io.vertx.junit5.VertxExtension) SqlResult(io.mycat.newquery.SqlResult) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcUtils(com.alibaba.druid.util.JdbcUtils) Row(io.vertx.sqlclient.Row) Assertions(org.junit.jupiter.api.Assertions) Handler(io.vertx.core.Handler) Collections(java.util.Collections) NewMycatConnection(io.mycat.newquery.NewMycatConnection) SqlResult(io.mycat.newquery.SqlResult) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) Test(org.junit.jupiter.api.Test)

Example 7 with XaSqlConnection

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

the class BaseSavepointSuite method baseSavepointSavepointCommit.

@Test
@SneakyThrows
public void baseSavepointSavepointCommit(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) {
            Future<Void> sss = savepointSqlConnection.createSavepoint("sss");
            sss.toCompletionStage().toCompletableFuture().get();
            Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
            NewMycatConnection ds1 = savepointSqlConnection.getConnection("ds1").toCompletionStage().toCompletableFuture().get();
            ds1.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
            sss = savepointSqlConnection.createSavepoint("sss");
            sss.toCompletionStage().toCompletableFuture().get();
            RowSet objects = ds1.query("select * from `db1`.`travelrecord` where id = 2").toCompletionStage().toCompletableFuture().get();
            Assert.assertTrue(objects.size() > 0);
            savepointSqlConnection.commit().toCompletionStage().toCompletableFuture().get();
            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);
            Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
            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) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) Test(org.junit.jupiter.api.Test) SneakyThrows(lombok.SneakyThrows)

Example 8 with XaSqlConnection

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

the class BaseSavepointSuite method baseSavepointCommitInSingleConnection.

@Test
@SneakyThrows
public void baseSavepointCommitInSingleConnection(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.commit().toCompletionStage().toCompletableFuture();
            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();
        }
    });
}
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 9 with XaSqlConnection

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

the class BaseSavepointSuite method baseSavepointRollbackSavepointInSingleConnection.

@Test
@SneakyThrows
public void baseSavepointRollbackSavepointInSingleConnection(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.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);
            ds1.insert("insert into `db1`.`travelrecord` (`id`) values ('2')").toCompletionStage().toCompletableFuture().get();
            savepointSqlConnection.commit().toCompletionStage().toCompletableFuture().get();
            Assert.assertEquals("[]", savepointSqlConnection.getExistedSavepoints().toString());
            Assert.assertFalse(savepointSqlConnection.isInTransaction());
            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 10 with XaSqlConnection

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

the class BaseSavepointSuite method baseSavepointRollbackInTwoConnection.

@Test
@SneakyThrows
public void baseSavepointRollbackInTwoConnection(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);
            ds2.insert("insert into `db1`.`travelrecord` (`id`) values ('3')").toCompletionStage().toCompletableFuture().get();
            Assert.assertEquals("[sss]", savepointSqlConnection.getExistedSavepoints().toString());
            savepointSqlConnection.rollback().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();
        }
    });
}
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)

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