Search in sources :

Example 1 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class BaseTransactionalExecutorTest method testExecuteWithGlobalLockSet.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testExecuteWithGlobalLockSet() throws Throwable {
    // initial objects
    ConnectionProxy connectionProxy = new ConnectionProxy(null, null);
    StatementProxy statementProxy = new StatementProxy<>(connectionProxy, null);
    BaseTransactionalExecutor<Object, Statement> baseTransactionalExecutor = new BaseTransactionalExecutor<Object, Statement>(statementProxy, null, (SQLRecognizer) null) {

        @Override
        protected Object doExecute(Object... args) {
            return null;
        }
    };
    GlobalLockTemplate template = new GlobalLockTemplate();
    // not in global lock context
    try {
        baseTransactionalExecutor.execute(new Object());
        Assertions.assertFalse(connectionProxy.isGlobalLockRequire(), "connection context set!");
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
    // in global lock context
    template.execute(new GlobalLockExecutor() {

        @Override
        public Object execute() throws Throwable {
            baseTransactionalExecutor.execute(new Object());
            Assertions.assertTrue(connectionProxy.isGlobalLockRequire(), "connection context not set!");
            return null;
        }

        @Override
        public GlobalLockConfig getGlobalLockConfig() {
            return null;
        }
    });
}
Also used : GlobalLockConfig(io.seata.core.model.GlobalLockConfig) GlobalLockTemplate(io.seata.rm.GlobalLockTemplate) GlobalLockExecutor(io.seata.rm.GlobalLockExecutor) Statement(java.sql.Statement) StatementProxy(io.seata.rm.datasource.StatementProxy) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) Test(org.junit.jupiter.api.Test)

Example 2 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class MultiExecutorTest method testBeforeImageAndAfterImages.

@Test
public void testBeforeImageAndAfterImages() throws SQLException {
    // same table and same type
    String sql = "update table_update_executor_test set name = 'WILL' where id = 1;" + "update table_update_executor_test set name = 'WILL2' where id = 2";
    List<SQLRecognizer> multi = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
    executor = new MultiExecutor(statementProxy, (statement, args) -> {
        return null;
    }, multi);
    TableRecords beforeImage = executor.beforeImage();
    Map multiSqlGroup = executor.getMultiSqlGroup();
    Map beforeImagesMap = executor.getBeforeImagesMap();
    Assertions.assertEquals(multiSqlGroup.size(), 1);
    Assertions.assertEquals(beforeImagesMap.size(), 1);
    TableRecords afterImage = executor.afterImage(beforeImage);
    Assertions.assertEquals(executor.getAfterImagesMap().size(), 1);
    executor.prepareUndoLog(beforeImage, afterImage);
    List<SQLUndoLog> items = connectionProxy.getContext().getUndoItems();
    Assertions.assertTrue(items.stream().allMatch(t -> Objects.equals(t.getSqlType(), SQLType.UPDATE) && Objects.equals(t.getTableName(), "table_update_executor_test")));
    Assertions.assertEquals(items.size(), 1);
    connectionProxy.getContext().reset();
    // same table delete
    sql = "delete from table_update_executor_test where id = 2;" + "delete from table_update_executor_test where id = 3";
    multi = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
    executor = new MultiExecutor(statementProxy, (statement, args) -> {
        return null;
    }, multi);
    beforeImage = executor.beforeImage();
    multiSqlGroup = executor.getMultiSqlGroup();
    beforeImagesMap = executor.getBeforeImagesMap();
    Assertions.assertEquals(multiSqlGroup.size(), 1);
    Assertions.assertEquals(beforeImagesMap.size(), 1);
    afterImage = executor.afterImage(beforeImage);
    Assertions.assertEquals(executor.getAfterImagesMap().size(), 1);
    executor.prepareUndoLog(beforeImage, afterImage);
    items = connectionProxy.getContext().getUndoItems();
    Set<String> itemSet = items.stream().map(t -> t.getTableName()).collect(Collectors.toSet());
    Assertions.assertTrue(itemSet.contains("table_update_executor_test"));
    Assertions.assertEquals(items.size(), 1);
    connectionProxy.getContext().reset();
    // multi table update
    sql = "update table_update_executor_test set name = 'WILL' where id = 1;update table_update_executor_test2 set name = 'WILL' where id = 1;update table_update_executor_test2 set name = 'WILL' where id = 3;";
    multi = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
    executor = new MultiExecutor(statementProxy, (statement, args) -> {
        return null;
    }, multi);
    beforeImage = executor.beforeImage();
    multiSqlGroup = executor.getMultiSqlGroup();
    beforeImagesMap = executor.getBeforeImagesMap();
    Assertions.assertEquals(multiSqlGroup.size(), 2);
    Assertions.assertEquals(beforeImagesMap.size(), 2);
    afterImage = executor.afterImage(beforeImage);
    Assertions.assertEquals(executor.getAfterImagesMap().size(), 2);
    executor.prepareUndoLog(beforeImage, afterImage);
    items = connectionProxy.getContext().getUndoItems();
    itemSet = items.stream().map(t -> t.getTableName()).collect(Collectors.toSet());
    Assertions.assertTrue(itemSet.contains("table_update_executor_test"));
    Assertions.assertTrue(itemSet.contains("table_update_executor_test2"));
    Assertions.assertEquals(items.size(), 2);
    connectionProxy.getContext().reset();
    // multi table delete
    sql = "delete from table_update_executor_test2 where id = 2;delete from table_update_executor_test where id = 3;delete from table_update_executor_test where id = 4;delete from table_update_executor_test";
    multi = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
    executor = new MultiExecutor(statementProxy, (statement, args) -> {
        return null;
    }, multi);
    beforeImage = executor.beforeImage();
    multiSqlGroup = executor.getMultiSqlGroup();
    beforeImagesMap = executor.getBeforeImagesMap();
    Assertions.assertEquals(multiSqlGroup.size(), 2);
    Assertions.assertEquals(beforeImagesMap.size(), 2);
    afterImage = executor.afterImage(beforeImage);
    Assertions.assertEquals(executor.getAfterImagesMap().size(), 2);
    executor.prepareUndoLog(beforeImage, afterImage);
    items = connectionProxy.getContext().getUndoItems();
    itemSet = items.stream().map(t -> t.getTableName()).collect(Collectors.toSet());
    Assertions.assertTrue(itemSet.contains("table_update_executor_test"));
    Assertions.assertTrue(itemSet.contains("table_update_executor_test2"));
    Assertions.assertEquals(items.size(), 2);
    // contains limit delete
    sql = "delete from table_update_executor_test2 where id = 2;delete from table_update_executor_test2 where id = 2 limit 1;";
    multi = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
    executor = new MultiExecutor(statementProxy, (statement, args) -> {
        return null;
    }, multi);
    Assertions.assertThrows(NotSupportYetException.class, executor::beforeImage);
    // contains order by and limit delete
    sql = "delete from table_update_executor_test2 where id = 2;delete from table_update_executor_test2 where id = 2 order by id desc limit 1;";
    multi = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
    executor = new MultiExecutor(statementProxy, (statement, args) -> {
        return null;
    }, multi);
    Assertions.assertThrows(NotSupportYetException.class, executor::beforeImage);
    // contains order by update
    sql = "update table_update_executor_test set name = 'WILL' where id = 1;update table_update_executor_test set name = 'WILL' where id = 1 order by id desc;";
    multi = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
    executor = new MultiExecutor(statementProxy, (statement, args) -> {
        return null;
    }, multi);
    Assertions.assertThrows(NotSupportYetException.class, executor::beforeImage);
    // contains order by and limit update
    sql = "update table_update_executor_test set name = 'WILL' where id = 1;update table_update_executor_test set name = 'WILL' where id = 1 order by id desc limit 1;";
    multi = SQLVisitorFactory.get(sql, JdbcConstants.MYSQL);
    executor = new MultiExecutor(statementProxy, (statement, args) -> {
        return null;
    }, multi);
    Assertions.assertThrows(NotSupportYetException.class, executor::beforeImage);
}
Also used : StatementProxy(io.seata.rm.datasource.StatementProxy) SQLVisitorFactory(io.seata.rm.datasource.sql.SQLVisitorFactory) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLType(io.seata.sqlparser.SQLType) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) MockExecuteHandlerImpl(io.seata.rm.datasource.mock.MockExecuteHandlerImpl) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) Set(java.util.Set) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) NotSupportYetException(io.seata.common.exception.NotSupportYetException) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class MultiExecutorTest method init.

@BeforeAll
public static void init() throws Throwable {
    List<String> returnValueColumnLabels = Lists.newArrayList("id", "name");
    Object[][] returnValue = new Object[][] { new Object[] { 1, "Tom" }, new Object[] { 2, "Jack" } };
    Object[][] columnMetas = new Object[][] { new Object[] { "", "", "table_update_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_update_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" } };
    Object[][] indexMetas = new Object[][] { new Object[] { "PRIMARY", "id", false, "", 3, 1, "A", 34 } };
    mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
    try {
        Field field = dataSourceProxy.getClass().getDeclaredField("dbType");
        field.setAccessible(true);
        field.set(dataSourceProxy, "mysql");
        connectionProxy = new ConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
        MockStatementBase mockStatement = new MockStatement(dataSource.getConnection().getConnection());
        statementProxy = new StatementProxy(connectionProxy, mockStatement);
    } catch (Exception e) {
        throw new RuntimeException("init failed");
    }
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) SQLException(java.sql.SQLException) NotSupportYetException(io.seata.common.exception.NotSupportYetException) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) Field(java.lang.reflect.Field) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 4 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class SelectForUpdateExecutorTest method init.

@BeforeAll
public static void init() {
    RootContext.unbind();
    List<String> returnValueColumnLabels = Lists.newArrayList("id", "name");
    Object[][] returnValue = new Object[][] { new Object[] { 1, "Tom" }, new Object[] { 2, "Jack" } };
    Object[][] columnMetas = new Object[][] { new Object[] { "", "", "table_select_for_update_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_select_for_update_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" } };
    Object[][] indexMetas = new Object[][] { new Object[] { "PRIMARY", "id", false, "", 3, 1, "A", 34 } };
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
    try {
        Field field = dataSourceProxy.getClass().getDeclaredField("dbType");
        field.setAccessible(true);
        field.set(dataSourceProxy, "mysql");
        connectionProxy = new MockConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
        connectionProxy.bind("xid");
        MockStatement mockStatement = new MockStatement(dataSource.getConnection().getConnection());
        statementProxy = new StatementProxy(connectionProxy, mockStatement);
    } catch (Exception e) {
        throw new RuntimeException("init failed");
    }
    String sql = "select * from table_select_for_update_executor_test where id = 1";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLSelectForUpdateRecognizer recognizer = new MySQLSelectForUpdateRecognizer(sql, asts.get(0));
    selectForUpdateExecutor = new SelectForUpdateExecutor(statementProxy, (statement, args) -> {
        return null;
    }, recognizer);
}
Also used : MockConnectionProxy(io.seata.rm.datasource.mock.MockConnectionProxy) MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) StatementProxy(io.seata.rm.datasource.StatementProxy) MockConnectionProxy(io.seata.rm.datasource.mock.MockConnectionProxy) Field(java.lang.reflect.Field) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) MockLockConflictConnectionProxy(io.seata.rm.datasource.mock.MockLockConflictConnectionProxy) Test(org.junit.jupiter.api.Test) RootContext(io.seata.core.context.RootContext) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Field(java.lang.reflect.Field) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class UpdateExecutorTest method init.

@BeforeAll
public static void init() {
    List<String> returnValueColumnLabels = Lists.newArrayList("id", "name");
    Object[][] returnValue = new Object[][] { new Object[] { 1, "Tom" }, new Object[] { 2, "Jack" } };
    Object[][] columnMetas = new Object[][] { new Object[] { "", "", "table_update_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_update_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" } };
    Object[][] indexMetas = new Object[][] { new Object[] { "PRIMARY", "id", false, "", 3, 1, "A", 34 } };
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
    try {
        Field field = dataSourceProxy.getClass().getDeclaredField("dbType");
        field.setAccessible(true);
        field.set(dataSourceProxy, "mysql");
        ConnectionProxy connectionProxy = new ConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
        MockStatementBase mockStatement = new MockStatement(dataSource.getConnection().getConnection());
        statementProxy = new StatementProxy(connectionProxy, mockStatement);
    } catch (Exception e) {
        throw new RuntimeException("init failed");
    }
    String sql = "update table_update_executor_test set name = 'WILL'";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLUpdateRecognizer recognizer = new MySQLUpdateRecognizer(sql, asts.get(0));
    updateExecutor = new UpdateExecutor(statementProxy, (statement, args) -> {
        return null;
    }, recognizer);
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) StatementProxy(io.seata.rm.datasource.StatementProxy) Field(java.lang.reflect.Field) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Test(org.junit.jupiter.api.Test) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLException(java.sql.SQLException) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLException(java.sql.SQLException) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) Field(java.lang.reflect.Field) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

StatementProxy (io.seata.rm.datasource.StatementProxy)12 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)10 Test (org.junit.jupiter.api.Test)10 MockStatement (com.alibaba.druid.mock.MockStatement)9 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)9 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)9 MockDriver (io.seata.rm.datasource.mock.MockDriver)9 Lists (com.google.common.collect.Lists)8 Field (java.lang.reflect.Field)8 SQLException (java.sql.SQLException)8 Types (java.sql.Types)8 List (java.util.List)8 BeforeAll (org.junit.jupiter.api.BeforeAll)8 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)7 SQLUtils (com.alibaba.druid.sql.SQLUtils)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)7 JdbcConstants (com.alibaba.druid.util.JdbcConstants)7 Assertions (org.junit.jupiter.api.Assertions)7 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)5 NotSupportYetException (io.seata.common.exception.NotSupportYetException)2