Search in sources :

Example 11 with XaSqlConnection

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();
        }
    });
}
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 12 with XaSqlConnection

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();
        }
    });
}
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 13 with XaSqlConnection

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();
                            });
                        }
                    });
                }
            });
        });
    });
}
Also used : CompositeFuture(io.vertx.core.CompositeFuture) 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) Handler(io.vertx.core.Handler) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult) Test(org.junit.jupiter.api.Test)

Example 14 with XaSqlConnection

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));
}
Also used : ShardingTable(io.mycat.calcite.table.ShardingTable) VertxExecuter(io.mycat.vertx.VertxExecuter) Distribution(io.mycat.calcite.rewriter.Distribution) java.util(java.util) CustomRuleFunction(io.mycat.router.CustomRuleFunction) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) Getter(lombok.Getter) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentMap(java.util.concurrent.ConcurrentMap) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) CompositeFuture(io.vertx.core.CompositeFuture) ParamHolder(io.mycat.calcite.spm.ParamHolder) ImmutableList(com.google.common.collect.ImmutableList) MycatTransientSQLTableScan(io.mycat.calcite.table.MycatTransientSQLTableScan) RexNode(org.apache.calcite.rex.RexNode) Observable(io.reactivex.rxjava3.core.Observable) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MySQLManager(cn.mycat.vertx.xa.MySQLManager) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) GlobalTable(io.mycat.calcite.table.GlobalTable) ValuePredicateAnalyzer(io.mycat.calcite.rewriter.ValuePredicateAnalyzer) Function1(org.apache.calcite.linq4j.function.Function1) Flowable(io.reactivex.rxjava3.core.Flowable) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) Logger(org.slf4j.Logger) NewMycatConnection(io.mycat.newquery.NewMycatConnection) io.mycat.calcite(io.mycat.calcite) RexBuilder(org.apache.calcite.rex.RexBuilder) MycatView(io.mycat.calcite.logical.MycatView) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Flowables(hu.akarnokd.rxjava3.operators.Flowables) VertxUtil(io.mycat.util.VertxUtil) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) VisualTableHandler(io.mycat.prototypeserver.mysql.VisualTableHandler) NormalTable(io.mycat.calcite.table.NormalTable) ValueIndexCondition(io.mycat.calcite.rewriter.ValueIndexCondition) QueryType(io.mycat.querycondition.QueryType) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) BackpressureStrategy(io.reactivex.rxjava3.core.BackpressureStrategy) MycatPreparedStatementUtil(io.mycat.calcite.executor.MycatPreparedStatementUtil) SqlString(org.apache.calcite.sql.util.SqlString) NotNull(org.jetbrains.annotations.NotNull) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection)

Example 15 with XaSqlConnection

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();
        }
    }
}
Also used : NewMycatConnection(io.mycat.newquery.NewMycatConnection) 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