Search in sources :

Example 1 with SqlResult

use of io.mycat.newquery.SqlResult in project Mycat2 by MyCATApache.

the class XaTestSuite method beginDoubleTargetInsertButStatementFail.

@Test
public void beginDoubleTargetInsertButStatementFail(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.insert("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  (id)\n" + "                       VALUES\n" + "                       (2/0);");
            return future.compose(rowSet -> {
                Assertions.assertEquals(1, rowSet.getAffectRows());
                return Future.succeededFuture(connection);
            });
        }));
        all.onComplete(event13 -> {
            Assertions.assertTrue(event13.failed());
            baseXaSqlConnection.rollback().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(0, 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) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) Test(org.junit.jupiter.api.Test)

Example 2 with SqlResult

use of io.mycat.newquery.SqlResult 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 3 with SqlResult

use of io.mycat.newquery.SqlResult 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 4 with SqlResult

use of io.mycat.newquery.SqlResult in project Mycat2 by MyCATApache.

the class VertxConnection method update.

public Future<SqlResult> update(String sql) {
    synchronized (VertxConnection.this) {
        Future<SqlResult> sqlResultFuture = future.transform(unused -> Future.future(promise -> {
            checkException();
            OkCommand queryCommand = new OkCommand(sql, netSocket, promise);
            netSocket.handler(queryCommand);
            queryCommand.write();
            promise.future().onSuccess(event -> VertxConnection.this.serverstatus = queryCommand.serverstatus);
        }));
        future = sqlResultFuture.mapEmpty();
        return sqlResultFuture;
    }
}
Also used : Logger(org.slf4j.Logger) Getter(lombok.Getter) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) NewMycatConnectionConfig(io.mycat.newquery.NewMycatConnectionConfig) ByteArrayDecoder(io.mycat.mysqlclient.decoder.ByteArrayDecoder) SqlResult(io.mycat.newquery.SqlResult) Future(io.vertx.core.Future) Function(java.util.function.Function) QueryCommand(io.mycat.mysqlclient.command.QueryCommand) Buffer(io.vertx.core.buffer.Buffer) MySQLServerStatusFlags(io.mycat.beans.mysql.MySQLServerStatusFlags) Observable(io.reactivex.rxjava3.core.Observable) OkCommand(io.mycat.mysqlclient.command.OkCommand) ResponseBufferCommand(io.mycat.mysqlclient.command.ResponseBufferCommand) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) StringArrayDecoder(io.mycat.mysqlclient.decoder.StringArrayDecoder) NetSocket(io.vertx.core.net.NetSocket) SqlResult(io.mycat.newquery.SqlResult) OkCommand(io.mycat.mysqlclient.command.OkCommand)

Example 5 with SqlResult

use of io.mycat.newquery.SqlResult in project Mycat2 by MyCATApache.

the class XaTestSuite method beginDoubleTargetInsertButPrepareFail.

@Test
public void beginDoubleTargetInsertButPrepareFail(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.insert("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((c) -> Future.failedFuture("prepare fail"));
            future.onComplete(new Handler<AsyncResult<Void>>() {

                @Override
                public void handle(AsyncResult<Void> event) {
                    Assertions.assertTrue(event.failed());
                    baseXaSqlConnection.rollback().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(0, 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)

Aggregations

SqlResult (io.mycat.newquery.SqlResult)7 Function (java.util.function.Function)7 XaSqlConnection (cn.mycat.vertx.xa.XaSqlConnection)6 NewMycatConnection (io.mycat.newquery.NewMycatConnection)6 RowSet (io.mycat.newquery.RowSet)6 AsyncResult (io.vertx.core.AsyncResult)6 Future (io.vertx.core.Future)6 Handler (io.vertx.core.Handler)6 MySQLManager (cn.mycat.vertx.xa.MySQLManager)5 SimpleConfig (cn.mycat.vertx.xa.SimpleConfig)5 XaLog (cn.mycat.vertx.xa.XaLog)5 XaLogImpl (cn.mycat.vertx.xa.impl.XaLogImpl)5 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)5 JdbcUtils (com.alibaba.druid.util.JdbcUtils)5 JsonUtil (io.mycat.util.JsonUtil)5 CompositeFuture (io.vertx.core.CompositeFuture)5 VertxExtension (io.vertx.junit5.VertxExtension)5 VertxTestContext (io.vertx.junit5.VertxTestContext)5 Row (io.vertx.sqlclient.Row)5 SqlConnection (io.vertx.sqlclient.SqlConnection)5