Search in sources :

Example 1 with ConnectionContext

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

the class AbstractDMLBaseExecutorTest method testOnlySupportMysqlWhenUseMultiPk.

@Test
public void testOnlySupportMysqlWhenUseMultiPk() {
    Mockito.when(connectionProxy.getContext()).thenReturn(new ConnectionContext());
    PreparedStatementProxy statementProxy = Mockito.mock(PreparedStatementProxy.class);
    Mockito.when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
    StatementCallback statementCallback = Mockito.mock(StatementCallback.class);
    SQLInsertRecognizer sqlInsertRecognizer = Mockito.mock(SQLInsertRecognizer.class);
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    executor = Mockito.spy(new OracleInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    Mockito.when(executor.getDbType()).thenReturn(JdbcConstants.ORACLE);
    Mockito.doReturn(tableMeta).when(executor).getTableMeta();
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList("id", "userCode"));
    Assertions.assertThrows(NotSupportYetException.class, () -> executor.executeAutoCommitFalse(null));
}
Also used : OracleInsertExecutor(io.seata.rm.datasource.exec.oracle.OracleInsertExecutor) ConnectionContext(io.seata.rm.datasource.ConnectionContext) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectionContext

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

the class AbstractDMLBaseExecutorTest method initBeforeEach.

@BeforeEach
public void initBeforeEach() throws Exception {
    branchRollbackFlagField = ConnectionProxy.LockRetryPolicy.class.getDeclaredField("LOCK_RETRY_POLICY_BRANCH_ROLLBACK_ON_CONFLICT");
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(branchRollbackFlagField, branchRollbackFlagField.getModifiers() & ~Modifier.FINAL);
    branchRollbackFlagField.setAccessible(true);
    boolean branchRollbackFlag = (boolean) branchRollbackFlagField.get(null);
    Assertions.assertTrue(branchRollbackFlag);
    Connection targetConnection = Mockito.mock(Connection.class);
    connectionProxy = Mockito.mock(ConnectionProxy.class);
    Mockito.doThrow(new LockConflictException()).when(connectionProxy).commit();
    Mockito.when(connectionProxy.getAutoCommit()).thenReturn(Boolean.TRUE);
    Mockito.when(connectionProxy.getTargetConnection()).thenReturn(targetConnection);
    Mockito.when(connectionProxy.getContext()).thenReturn(new ConnectionContext());
    PreparedStatementProxy statementProxy = Mockito.mock(PreparedStatementProxy.class);
    Mockito.when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
    StatementCallback statementCallback = Mockito.mock(StatementCallback.class);
    SQLInsertRecognizer sqlInsertRecognizer = Mockito.mock(SQLInsertRecognizer.class);
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    executor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    Mockito.doReturn(tableMeta).when(executor).getTableMeta();
    TableRecords tableRecords = new TableRecords();
    Mockito.doReturn(tableRecords).when(executor).beforeImage();
    Mockito.doReturn(tableRecords).when(executor).afterImage(tableRecords);
}
Also used : Connection(java.sql.Connection) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(java.lang.reflect.Field) MySQLInsertExecutor(io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor) ConnectionContext(io.seata.rm.datasource.ConnectionContext) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ConnectionContext

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

the class MySQLUndoLogManagerTest method testFlushUndoLogs.

@Test
public void testFlushUndoLogs() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
    connectionProxy.bind("xid");
    ConnectionContext context = connectionProxy.getContext();
    Method method = context.getClass().getDeclaredMethod("setBranchId", Long.class);
    method.setAccessible(true);
    method.invoke(context, 1L);
    SQLUndoLog undoLogItem = getUndoLogItem(1);
    undoLogItem.setTableName("test");
    Method appendUndoItemMethod = context.getClass().getDeclaredMethod("appendUndoItem", SQLUndoLog.class);
    appendUndoItemMethod.setAccessible(true);
    appendUndoItemMethod.invoke(context, undoLogItem);
    Assertions.assertDoesNotThrow(() -> undoLogManager.flushUndoLogs(connectionProxy));
}
Also used : SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) ConnectionContext(io.seata.rm.datasource.ConnectionContext) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectionContext

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

the class AbstractUndoLogManager method flushUndoLogs.

/**
 * Flush undo logs.
 *
 * @param cp the cp
 * @throws SQLException the sql exception
 */
@Override
public void flushUndoLogs(ConnectionProxy cp) throws SQLException {
    ConnectionContext connectionContext = cp.getContext();
    if (!connectionContext.hasUndoLog()) {
        return;
    }
    String xid = connectionContext.getXid();
    long branchId = connectionContext.getBranchId();
    BranchUndoLog branchUndoLog = new BranchUndoLog();
    branchUndoLog.setXid(xid);
    branchUndoLog.setBranchId(branchId);
    branchUndoLog.setSqlUndoLogs(connectionContext.getUndoItems());
    UndoLogParser parser = UndoLogParserFactory.getInstance();
    byte[] undoLogContent = parser.encode(branchUndoLog);
    CompressorType compressorType = CompressorType.NONE;
    if (needCompress(undoLogContent)) {
        compressorType = ROLLBACK_INFO_COMPRESS_TYPE;
        undoLogContent = CompressorFactory.getCompressor(compressorType.getCode()).compress(undoLogContent);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Flushing UNDO LOG: {}", new String(undoLogContent, Constants.DEFAULT_CHARSET));
    }
    insertUndoLogWithNormal(xid, branchId, buildContext(parser.getName(), compressorType), undoLogContent, cp.getTargetConnection());
}
Also used : CompressorType(io.seata.core.compressor.CompressorType) ConnectionContext(io.seata.rm.datasource.ConnectionContext)

Aggregations

ConnectionContext (io.seata.rm.datasource.ConnectionContext)4 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)2 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)2 SQLInsertRecognizer (io.seata.sqlparser.SQLInsertRecognizer)2 Test (org.junit.jupiter.api.Test)2 CompressorType (io.seata.core.compressor.CompressorType)1 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)1 MySQLInsertExecutor (io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor)1 OracleInsertExecutor (io.seata.rm.datasource.exec.oracle.OracleInsertExecutor)1 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)1 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1