Search in sources :

Example 11 with StatementProxy

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

the class SelectForUpdateExecutorTest method testDoExecute.

@Test
public void testDoExecute() throws Throwable {
    Assertions.assertThrows(RuntimeException.class, () -> selectForUpdateExecutor.doExecute((Object) null));
    RootContext.bind("xid");
    Assertions.assertDoesNotThrow(() -> {
        selectForUpdateExecutor.doExecute((Object) null);
    });
    RootContext.unbind();
    RootContext.bindGlobalLockFlag();
    Assertions.assertDoesNotThrow(() -> {
        selectForUpdateExecutor.doExecute((Object) null);
    });
    RootContext.unbindGlobalLockFlag();
    connectionProxy = new MockLockConflictConnectionProxy(connectionProxy.getDataSourceProxy(), connectionProxy.getTargetConnection());
    statementProxy = new StatementProxy(connectionProxy, statementProxy.getTargetStatement());
    statementProxy.getTargetStatement().getConnection().setAutoCommit(false);
    String sql = "select * from dual";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLSelectForUpdateRecognizer recognizer = new MySQLSelectForUpdateRecognizer(sql, asts.get(0));
    selectForUpdateExecutor = new SelectForUpdateExecutor(statementProxy, (statement, args) -> null, recognizer);
    RootContext.bind("xid");
    Assertions.assertThrows(LockWaitTimeoutException.class, () -> selectForUpdateExecutor.doExecute((Object) null));
    RootContext.unbind();
}
Also used : 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) StatementProxy(io.seata.rm.datasource.StatementProxy) MockLockConflictConnectionProxy(io.seata.rm.datasource.mock.MockLockConflictConnectionProxy) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 12 with StatementProxy

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

the class ExecuteTemplate method execute.

/**
 * Execute t.
 *
 * @param <T>               the type parameter
 * @param <S>               the type parameter
 * @param sqlRecognizers    the sql recognizer list
 * @param statementProxy    the statement proxy
 * @param statementCallback the statement callback
 * @param args              the args
 * @return the t
 * @throws SQLException the sql exception
 */
public static <T, S extends Statement> T execute(List<SQLRecognizer> sqlRecognizers, StatementProxy<S> statementProxy, StatementCallback<T, S> statementCallback, Object... args) throws SQLException {
    if (!RootContext.requireGlobalLock() && BranchType.AT != RootContext.getBranchType()) {
        // Just work as original statement
        return statementCallback.execute(statementProxy.getTargetStatement(), args);
    }
    String dbType = statementProxy.getConnectionProxy().getDbType();
    if (CollectionUtils.isEmpty(sqlRecognizers)) {
        sqlRecognizers = SQLVisitorFactory.get(statementProxy.getTargetSQL(), dbType);
    }
    Executor<T> executor;
    if (CollectionUtils.isEmpty(sqlRecognizers)) {
        executor = new PlainExecutor<>(statementProxy, statementCallback);
    } else {
        if (sqlRecognizers.size() == 1) {
            SQLRecognizer sqlRecognizer = sqlRecognizers.get(0);
            switch(sqlRecognizer.getSQLType()) {
                case INSERT:
                    executor = EnhancedServiceLoader.load(InsertExecutor.class, dbType, new Class[] { StatementProxy.class, StatementCallback.class, SQLRecognizer.class }, new Object[] { statementProxy, statementCallback, sqlRecognizer });
                    break;
                case UPDATE:
                    executor = new UpdateExecutor<>(statementProxy, statementCallback, sqlRecognizer);
                    break;
                case DELETE:
                    executor = new DeleteExecutor<>(statementProxy, statementCallback, sqlRecognizer);
                    break;
                case SELECT_FOR_UPDATE:
                    executor = new SelectForUpdateExecutor<>(statementProxy, statementCallback, sqlRecognizer);
                    break;
                default:
                    executor = new PlainExecutor<>(statementProxy, statementCallback);
                    break;
            }
        } else {
            executor = new MultiExecutor<>(statementProxy, statementCallback, sqlRecognizers);
        }
    }
    T rs;
    try {
        rs = executor.execute(args);
    } catch (Throwable ex) {
        if (!(ex instanceof SQLException)) {
            // Turn other exception into SQLException
            ex = new SQLException(ex);
        }
        throw (SQLException) ex;
    }
    return rs;
}
Also used : SQLException(java.sql.SQLException) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) StatementProxy(io.seata.rm.datasource.StatementProxy)

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