use of com.dangdang.ddframe.rdb.transaction.soft.api.config.SoftTransactionConfiguration in project sharding-jdbc by dangdangdotcom.
the class BestEffortsDeliveryListener method listen.
@Subscribe
@AllowConcurrentEvents
public void listen(final DMLExecutionEvent event) {
if (!isProcessContinuously()) {
return;
}
SoftTransactionConfiguration transactionConfig = SoftTransactionManager.getCurrentTransactionConfiguration().get();
TransactionLogStorage transactionLogStorage = TransactionLogStorageFactory.createTransactionLogStorage(transactionConfig.buildTransactionLogDataSource());
BEDSoftTransaction bedSoftTransaction = (BEDSoftTransaction) SoftTransactionManager.getCurrentTransaction().get();
switch(event.getEventExecutionType()) {
case BEFORE_EXECUTE:
//TODO 对于批量执行的SQL需要解析成两层列表
transactionLogStorage.add(new TransactionLog(event.getId(), bedSoftTransaction.getTransactionId(), bedSoftTransaction.getTransactionType(), event.getDataSource(), event.getSql(), event.getParameters(), System.currentTimeMillis(), 0));
return;
case EXECUTE_SUCCESS:
transactionLogStorage.remove(event.getId());
return;
case EXECUTE_FAILURE:
boolean deliverySuccess = false;
for (int i = 0; i < transactionConfig.getSyncMaxDeliveryTryTimes(); i++) {
if (deliverySuccess) {
return;
}
boolean isNewConnection = false;
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLStatementType.UPDATE);
if (!isValidConnection(conn)) {
bedSoftTransaction.getConnection().releaseBrokenConnection(conn);
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource(), SQLStatementType.UPDATE);
isNewConnection = true;
}
preparedStatement = conn.prepareStatement(event.getSql());
//TODO 对于批量事件需要解析成两层列表
for (int parameterIndex = 0; parameterIndex < event.getParameters().size(); parameterIndex++) {
preparedStatement.setObject(parameterIndex + 1, event.getParameters().get(parameterIndex));
}
preparedStatement.executeUpdate();
deliverySuccess = true;
transactionLogStorage.remove(event.getId());
} catch (final SQLException ex) {
log.error(String.format("Delivery times %s error, max try times is %s", i + 1, transactionConfig.getSyncMaxDeliveryTryTimes()), ex);
} finally {
close(isNewConnection, conn, preparedStatement);
}
}
return;
default:
throw new UnsupportedOperationException(event.getEventExecutionType().toString());
}
}
use of com.dangdang.ddframe.rdb.transaction.soft.api.config.SoftTransactionConfiguration in project sharding-jdbc by dangdangdotcom.
the class SoftTransactionTest method getSoftTransactionConfiguration.
private SoftTransactionConfiguration getSoftTransactionConfiguration(final ShardingDataSource dataSource) {
SoftTransactionConfiguration result = new SoftTransactionConfiguration(dataSource);
result.setTransactionLogDataSource(getTransactionDataSource());
return result;
}
use of com.dangdang.ddframe.rdb.transaction.soft.api.config.SoftTransactionConfiguration in project sharding-jdbc by dangdangdotcom.
the class AbstractSoftTransactionMockTest method setUp.
@Before
public void setUp() throws Exception {
SoftTransactionConfiguration softTransactionConfiguration = mock(SoftTransactionConfiguration.class);
when(softTransactionConfiguration.getTransactionLogDataSource()).thenReturn(mock(DataSource.class));
when(softTransactionConfiguration.getBestEffortsDeliveryJobConfiguration()).thenReturn(Optional.<NestedBestEffortsDeliveryJobConfiguration>absent());
softTransactionConfiguration.setBestEffortsDeliveryJobConfiguration(Optional.<NestedBestEffortsDeliveryJobConfiguration>absent());
softTransactionManager = new SoftTransactionManager(softTransactionConfiguration);
}
use of com.dangdang.ddframe.rdb.transaction.soft.api.config.SoftTransactionConfiguration in project sharding-jdbc by dangdangdotcom.
the class Main method getSoftTransactionConfiguration.
private static SoftTransactionConfiguration getSoftTransactionConfiguration(final DataSource dataSource) {
SoftTransactionConfiguration result = new SoftTransactionConfiguration(dataSource);
if (useNestedJob) {
result.setBestEffortsDeliveryJobConfiguration(Optional.of(new NestedBestEffortsDeliveryJobConfiguration()));
}
result.setTransactionLogDataSource(createTransactionLogDataSource());
return result;
}
Aggregations