Search in sources :

Example 6 with TransactionLog

use of com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog 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());
    }
}
Also used : TransactionLog(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog) SQLException(java.sql.SQLException) TransactionLogStorage(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLogStorage) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) BEDSoftTransaction(com.dangdang.ddframe.rdb.transaction.soft.bed.BEDSoftTransaction) SoftTransactionConfiguration(com.dangdang.ddframe.rdb.transaction.soft.api.config.SoftTransactionConfiguration) AllowConcurrentEvents(com.google.common.eventbus.AllowConcurrentEvents) Subscribe(com.google.common.eventbus.Subscribe)

Example 7 with TransactionLog

use of com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog in project sharding-jdbc by dangdangdotcom.

the class RdbTransactionLogStorageTest method assertAddTransactionLogStorage.

@Test
public void assertAddTransactionLogStorage() throws SQLException {
    String id = UUID.randomUUID().toString();
    String transactionId = UUID.randomUUID().toString();
    TransactionLog transactionLog = buildTransactionLog(id, transactionId);
    storage.add(transactionLog);
    assertThat(storage.findEligibleTransactionLogs(1, 1, 1L).size(), is(1));
    storage.remove(id);
}
Also used : TransactionLog(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog) Test(org.junit.Test)

Example 8 with TransactionLog

use of com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog in project sharding-jdbc by dangdangdotcom.

the class AbstractTransactionLogStorageOperationsTest method assertTransactionLogStorageOperations.

protected void assertTransactionLogStorageOperations(final TransactionLogStorage storage) {
    String id = UUID.randomUUID().toString();
    String transactionId = UUID.randomUUID().toString();
    TransactionLog transactionLog = new TransactionLog(id, transactionId, SoftTransactionType.BestEffortsDelivery, "ds_1", "UPDATE t_order_0 SET not_existed_column = 1 WHERE user_id = 1 AND order_id = ?", Lists.newArrayList(), 1461062858701L, 0);
    storage.add(transactionLog);
    storage.increaseAsyncDeliveryTryTimes(id);
    assertThat(storage.findEligibleTransactionLogs(1, 2, 0L).get(0).getAsyncDeliveryTryTimes(), is(1));
    storage.remove(id);
    assertThat(storage.findEligibleTransactionLogs(1, 2, 0L).size(), is(0));
}
Also used : TransactionLog(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog)

Aggregations

TransactionLog (com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog)8 Test (org.junit.Test)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 SoftTransactionConfiguration (com.dangdang.ddframe.rdb.transaction.soft.api.config.SoftTransactionConfiguration)1 BEDSoftTransaction (com.dangdang.ddframe.rdb.transaction.soft.bed.BEDSoftTransaction)1 TransactionLogStorageException (com.dangdang.ddframe.rdb.transaction.soft.exception.TransactionLogStorageException)1 TransactionLogStorage (com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLogStorage)1 AllowConcurrentEvents (com.google.common.eventbus.AllowConcurrentEvents)1 Subscribe (com.google.common.eventbus.Subscribe)1 Gson (com.google.gson.Gson)1 TypeToken (com.google.gson.reflect.TypeToken)1 ResultSet (java.sql.ResultSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1