Search in sources :

Example 16 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class ConnectionContextProxyTest method testReleaseSavepoint.

@Test
public void testReleaseSavepoint() {
    Savepoint sp1 = new MockSavepoint();
    connectionContext.appendSavepoint(sp1);
    connectionContext.appendUndoItem(new SQLUndoLog());
    connectionContext.appendLockKey("sp1-lock-key");
    Savepoint sp2 = new MockSavepoint();
    connectionContext.appendSavepoint(sp2);
    Savepoint sp3 = new MockSavepoint();
    connectionContext.appendSavepoint(sp3);
    connectionContext.appendLockKey("sp3-lock-key");
    connectionContext.appendUndoItem(new SQLUndoLog());
    Assertions.assertEquals(connectionContext.getUndoItems().size(), 2);
    Assertions.assertEquals(connectionContext.buildLockKeys(), "sp3-lock-key;sp1-lock-key");
    connectionContext.releaseSavepoint(sp3);
    Assertions.assertEquals(connectionContext.getUndoItems().size(), 2);
    Assertions.assertEquals(connectionContext.buildLockKeys(), "sp3-lock-key;sp1-lock-key");
    connectionContext.releaseSavepoint(null);
    Assertions.assertEquals(connectionContext.getUndoItems().size(), 2);
    Assertions.assertEquals(connectionContext.buildLockKeys(), "sp3-lock-key;sp1-lock-key");
}
Also used : MockSavepoint(com.alibaba.druid.mock.MockSavepoint) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) MockSavepoint(com.alibaba.druid.mock.MockSavepoint) Savepoint(java.sql.Savepoint) Test(org.junit.jupiter.api.Test)

Example 17 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class ConnectionContextProxyTest method testAppendUndoItem.

@Test
public void testAppendUndoItem() {
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    connectionContext.appendUndoItem(sqlUndoLog);
    SQLUndoLog sqlUndoLog1 = new SQLUndoLog();
    connectionContext.appendUndoItem(sqlUndoLog1);
    Assertions.assertTrue(connectionContext.hasUndoLog());
    Assertions.assertEquals(connectionContext.getUndoItems().size(), 2);
    Assertions.assertSame(connectionContext.getUndoItems().get(0), sqlUndoLog);
    Assertions.assertSame(connectionContext.getUndoItems().get(1), sqlUndoLog1);
}
Also used : SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) Test(org.junit.jupiter.api.Test)

Example 18 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class ConnectionProxyTest method testLockRetryPolicyRollbackOnConflict.

@Test
public void testLockRetryPolicyRollbackOnConflict() throws Exception {
    boolean oldBranchRollbackFlag = (boolean) branchRollbackFlagField.get(null);
    branchRollbackFlagField.set(null, true);
    ConnectionProxy connectionProxy = new ConnectionProxy(dataSourceProxy, null);
    connectionProxy.bind(TEST_XID);
    connectionProxy.appendUndoLog(new SQLUndoLog());
    connectionProxy.appendLockKey(lockKey);
    Assertions.assertThrows(LockConflictException.class, connectionProxy::commit);
    branchRollbackFlagField.set(null, oldBranchRollbackFlag);
}
Also used : SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) Test(org.junit.jupiter.api.Test)

Example 19 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class BaseTransactionalExecutor method buildUndoItem.

/**
 * build a SQLUndoLog
 *
 * @param beforeImage the before image
 * @param afterImage  the after image
 * @return sql undo log
 */
protected SQLUndoLog buildUndoItem(TableRecords beforeImage, TableRecords afterImage) {
    SQLType sqlType = sqlRecognizer.getSQLType();
    String tableName = sqlRecognizer.getTableName();
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(sqlType);
    sqlUndoLog.setTableName(tableName);
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    return sqlUndoLog;
}
Also used : SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) SQLType(io.seata.sqlparser.SQLType)

Example 20 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class MySQLUndoDeleteExecutorTest method init.

@BeforeAll
public static void init() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "id" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("table_name");
    TableRecords beforeImage = new TableRecords();
    beforeImage.setTableName("table_name");
    beforeImage.setTableMeta(tableMeta);
    List<Row> beforeRows = new ArrayList<>();
    Row row0 = new Row();
    addField(row0, "id", 1, "12345");
    addField(row0, "age", 1, "1");
    beforeRows.add(row0);
    Row row1 = new Row();
    addField(row1, "id", 1, "12346");
    addField(row1, "age", 1, "1");
    beforeRows.add(row1);
    beforeImage.setRows(beforeRows);
    TableRecords afterImage = new TableRecords();
    afterImage.setTableName("table_name");
    afterImage.setTableMeta(tableMeta);
    List<Row> afterRows = new ArrayList<>();
    Row row2 = new Row();
    addField(row2, "id", 1, "12345");
    addField(row2, "age", 1, "2");
    afterRows.add(row2);
    Row row3 = new Row();
    addField(row3, "id", 1, "12346");
    addField(row3, "age", 1, "2");
    afterRows.add(row3);
    afterImage.setRows(afterRows);
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.UPDATE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("table_name");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    executor = new MySQLUndoDeleteExecutor(sqlUndoLog);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)22 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)13 Test (org.junit.jupiter.api.Test)12 Row (io.seata.rm.datasource.sql.struct.Row)11 ArrayList (java.util.ArrayList)9 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)6 Field (io.seata.rm.datasource.sql.struct.Field)4 BeforeAll (org.junit.jupiter.api.BeforeAll)4 UndoExecutorTest (io.seata.rm.datasource.undo.UndoExecutorTest)3 Savepoint (java.sql.Savepoint)3 MockSavepoint (com.alibaba.druid.mock.MockSavepoint)2 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)2 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)2 BranchUndoLog (io.seata.rm.datasource.undo.BranchUndoLog)2 SQLType (io.seata.sqlparser.SQLType)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 Map (java.util.Map)2