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));
}
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);
}
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));
}
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());
}
Aggregations