Search in sources :

Example 1 with TransactionLogStorage

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

the class RdbTransactionLogStorageOperationsTest method assertRdbTransactionLogStorageOperations.

@Test
public void assertRdbTransactionLogStorageOperations() throws SQLException {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(org.h2.Driver.class.getName());
    dataSource.setUrl("jdbc:h2:mem:db_transaction_storage");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    createTable(dataSource);
    TransactionLogStorage storage = new RdbTransactionLogStorage(dataSource);
    assertTransactionLogStorageOperations(storage);
}
Also used : RdbTransactionLogStorage(com.dangdang.ddframe.rdb.transaction.soft.storage.impl.RdbTransactionLogStorage) RdbTransactionLogStorage(com.dangdang.ddframe.rdb.transaction.soft.storage.impl.RdbTransactionLogStorage) TransactionLogStorage(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLogStorage) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) Test(org.junit.Test)

Example 2 with TransactionLogStorage

use of com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLogStorage 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 3 with TransactionLogStorage

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

the class MemoryTransactionLogStorageOperationsTest method assertMemoryTransactionLogStorageOperations.

@Test
public void assertMemoryTransactionLogStorageOperations() {
    TransactionLogStorage storage = new MemoryTransactionLogStorage();
    assertTransactionLogStorageOperations(storage);
}
Also used : MemoryTransactionLogStorage(com.dangdang.ddframe.rdb.transaction.soft.storage.impl.MemoryTransactionLogStorage) TransactionLogStorage(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLogStorage) MemoryTransactionLogStorage(com.dangdang.ddframe.rdb.transaction.soft.storage.impl.MemoryTransactionLogStorage) Test(org.junit.Test)

Aggregations

TransactionLogStorage (com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLogStorage)3 Test (org.junit.Test)2 SoftTransactionConfiguration (com.dangdang.ddframe.rdb.transaction.soft.api.config.SoftTransactionConfiguration)1 BEDSoftTransaction (com.dangdang.ddframe.rdb.transaction.soft.bed.BEDSoftTransaction)1 TransactionLog (com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog)1 MemoryTransactionLogStorage (com.dangdang.ddframe.rdb.transaction.soft.storage.impl.MemoryTransactionLogStorage)1 RdbTransactionLogStorage (com.dangdang.ddframe.rdb.transaction.soft.storage.impl.RdbTransactionLogStorage)1 AllowConcurrentEvents (com.google.common.eventbus.AllowConcurrentEvents)1 Subscribe (com.google.common.eventbus.Subscribe)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)1