Search in sources :

Example 6 with ConnectionProxy

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

the class MySQLUndoLogManagerTest method init.

@BeforeEach
public void init() throws SQLException {
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    dataSourceProxy = new DataSourceProxy(dataSource);
    connectionProxy = new ConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
    undoLogManager = new MySQLUndoLogManager();
    tableMeta = new TableMeta();
    tableMeta.setTableName("table_plain_executor_test");
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with ConnectionProxy

use of io.seata.rm.datasource.ConnectionProxy 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)

Example 8 with ConnectionProxy

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

the class AbstractDMLBaseExecutor method executeAutoCommitTrue.

/**
 * Execute auto commit true t.
 *
 * @param args the args
 * @return the t
 * @throws Throwable the throwable
 */
protected T executeAutoCommitTrue(Object[] args) throws Throwable {
    ConnectionProxy connectionProxy = statementProxy.getConnectionProxy();
    try {
        connectionProxy.changeAutoCommit();
        return new LockRetryPolicy(connectionProxy).execute(() -> {
            T result = executeAutoCommitFalse(args);
            connectionProxy.commit();
            return result;
        });
    } catch (Exception e) {
        // when exception occur in finally,this exception will lost, so just print it here
        LOGGER.error("execute executeAutoCommitTrue error:{}", e.getMessage(), e);
        if (!LockRetryPolicy.isLockRetryPolicyBranchRollbackOnConflict()) {
            connectionProxy.getTargetConnection().rollback();
        }
        throw e;
    } finally {
        connectionProxy.getContext().reset();
        connectionProxy.setAutoCommit(true);
    }
}
Also used : AbstractConnectionProxy(io.seata.rm.datasource.AbstractConnectionProxy) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) NotSupportYetException(io.seata.common.exception.NotSupportYetException) SQLException(java.sql.SQLException)

Example 9 with ConnectionProxy

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

the class BaseTransactionalExecutor method getTableMeta.

/**
 * Gets table meta.
 *
 * @param tableName the table name
 * @return the table meta
 */
protected TableMeta getTableMeta(String tableName) {
    if (tableMeta != null) {
        return tableMeta;
    }
    ConnectionProxy connectionProxy = statementProxy.getConnectionProxy();
    tableMeta = TableMetaCacheFactory.getTableMetaCache(connectionProxy.getDbType()).getTableMeta(connectionProxy.getTargetConnection(), tableName, connectionProxy.getDataSourceProxy().getResourceId());
    return tableMeta;
}
Also used : ConnectionProxy(io.seata.rm.datasource.ConnectionProxy)

Example 10 with ConnectionProxy

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

the class BaseTransactionalExecutor method prepareUndoLog.

/**
 * prepare undo log.
 *
 * @param beforeImage the before image
 * @param afterImage  the after image
 * @throws SQLException the sql exception
 */
protected void prepareUndoLog(TableRecords beforeImage, TableRecords afterImage) throws SQLException {
    if (beforeImage.getRows().isEmpty() && afterImage.getRows().isEmpty()) {
        return;
    }
    if (SQLType.UPDATE == sqlRecognizer.getSQLType()) {
        if (beforeImage.getRows().size() != afterImage.getRows().size()) {
            throw new ShouldNeverHappenException("Before image size is not equaled to after image size, probably because you updated the primary keys.");
        }
    }
    ConnectionProxy connectionProxy = statementProxy.getConnectionProxy();
    TableRecords lockKeyRecords = sqlRecognizer.getSQLType() == SQLType.DELETE ? beforeImage : afterImage;
    String lockKeys = buildLockKey(lockKeyRecords);
    if (null != lockKeys) {
        connectionProxy.appendLockKey(lockKeys);
        SQLUndoLog sqlUndoLog = buildUndoItem(beforeImage, afterImage);
        connectionProxy.appendUndoLog(sqlUndoLog);
    }
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy)

Aggregations

ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)15 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)8 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)7 StatementProxy (io.seata.rm.datasource.StatementProxy)7 MockDriver (io.seata.rm.datasource.mock.MockDriver)7 MockStatement (com.alibaba.druid.mock.MockStatement)6 Field (java.lang.reflect.Field)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Test (org.junit.jupiter.api.Test)6 Lists (com.google.common.collect.Lists)5 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)5 SQLException (java.sql.SQLException)5 Types (java.sql.Types)5 List (java.util.List)5 BeforeAll (org.junit.jupiter.api.BeforeAll)5 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)4 SQLUtils (com.alibaba.druid.sql.SQLUtils)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)4 JdbcConstants (com.alibaba.druid.util.JdbcConstants)4 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)4