use of com.dangdang.ddframe.rdb.transaction.soft.bed.BEDSoftTransaction 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.bed.BEDSoftTransaction in project sharding-jdbc by dangdangdotcom.
the class Main method updateFailure.
private static void updateFailure(final DataSource dataSource) throws SQLException {
String sql1 = "UPDATE t_order SET status='UPDATE_1' WHERE user_id=10 AND order_id=1000";
String sql2 = "UPDATE t_order SET not_existed_column=1 WHERE user_id=1 AND order_id=?";
String sql3 = "UPDATE t_order SET status='UPDATE_2' WHERE user_id=10 AND order_id=1000";
SoftTransactionManager transactionManager = new SoftTransactionManager(getSoftTransactionConfiguration(dataSource));
transactionManager.init();
BEDSoftTransaction transaction = (BEDSoftTransaction) transactionManager.getTransaction(SoftTransactionType.BestEffortsDelivery);
Connection conn = null;
try {
conn = dataSource.getConnection();
transaction.begin(conn);
PreparedStatement preparedStatement1 = conn.prepareStatement(sql1);
PreparedStatement preparedStatement2 = conn.prepareStatement(sql2);
preparedStatement2.setObject(1, 1000);
PreparedStatement preparedStatement3 = conn.prepareStatement(sql3);
preparedStatement1.executeUpdate();
preparedStatement2.executeUpdate();
preparedStatement3.executeUpdate();
} finally {
transaction.end();
if (conn != null) {
conn.close();
}
}
}
use of com.dangdang.ddframe.rdb.transaction.soft.bed.BEDSoftTransaction in project sharding-jdbc by dangdangdotcom.
the class SoftTransactionManager method getTransaction.
/**
* 获取柔性事务管理器.
*
* @param type 柔性事务类型
* @return 柔性事务
*/
public AbstractSoftTransaction getTransaction(final SoftTransactionType type) {
AbstractSoftTransaction result;
switch(type) {
case BestEffortsDelivery:
result = new BEDSoftTransaction();
break;
case TryConfirmCancel:
result = new TCCSoftTransaction();
break;
default:
throw new UnsupportedOperationException(type.toString());
}
// TODO 目前使用不支持嵌套事务,以后这里需要可配置
if (getCurrentTransaction().isPresent()) {
throw new UnsupportedOperationException("Cannot support nested transaction.");
}
ExecutorDataMap.getDataMap().put(TRANSACTION, result);
ExecutorDataMap.getDataMap().put(TRANSACTION_CONFIG, transactionConfig);
return result;
}
use of com.dangdang.ddframe.rdb.transaction.soft.bed.BEDSoftTransaction in project sharding-jdbc by dangdangdotcom.
the class SoftTransactionTest method bedSoftTransactionTest.
@Test
public void bedSoftTransactionTest() throws SQLException {
SoftTransactionManager transactionManagerFactory = new SoftTransactionManager(getSoftTransactionConfiguration(getShardingDataSource()));
transactionManagerFactory.init();
BEDSoftTransaction transactionManager = (BEDSoftTransaction) transactionManagerFactory.getTransaction(SoftTransactionType.BestEffortsDelivery);
transactionManager.begin(getShardingDataSource().getConnection());
insert();
assertThat(select(), is(1));
transactionManager.end();
}
Aggregations