Search in sources :

Example 1 with TransactionLog

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

the class MemoryTransactionLogStorage method findEligibleTransactionLogs.

@Override
public List<TransactionLog> findEligibleTransactionLogs(final int size, final int maxDeliveryTryTimes, final long maxDeliveryTryDelayMillis) {
    List<TransactionLog> result = new ArrayList<>();
    int count = 0;
    for (TransactionLog each : DATA.values()) {
        if (count >= size) {
            break;
        }
        if (each.getAsyncDeliveryTryTimes() < maxDeliveryTryTimes && SoftTransactionType.BestEffortsDelivery == each.getTransactionType() && each.getCreationTime() < System.currentTimeMillis() - maxDeliveryTryDelayMillis) {
            result.add(each);
        }
        count++;
    }
    return result;
}
Also used : TransactionLog(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog) ArrayList(java.util.ArrayList)

Example 2 with TransactionLog

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

the class MemoryTransactionLogStorage method increaseAsyncDeliveryTryTimes.

@Override
public void increaseAsyncDeliveryTryTimes(final String id) {
    if (DATA.containsKey(id)) {
        TransactionLog transactionLog = DATA.get(id);
        transactionLog.setAsyncDeliveryTryTimes(new AtomicInteger(transactionLog.getAsyncDeliveryTryTimes()).incrementAndGet());
        DATA.put(id, transactionLog);
    }
}
Also used : TransactionLog(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 3 with TransactionLog

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

the class RdbTransactionLogStorage method findEligibleTransactionLogs.

@Override
public List<TransactionLog> findEligibleTransactionLogs(final int size, final int maxDeliveryTryTimes, final long maxDeliveryTryDelayMillis) {
    List<TransactionLog> result = new ArrayList<>(size);
    String sql = "SELECT `id`, `transaction_type`, `data_source`, `sql`, `parameters`, `creation_time`, `async_delivery_try_times` " + "FROM `transaction_log` WHERE `async_delivery_try_times`<? AND `transaction_type`=? AND `creation_time`<? LIMIT ?;";
    try (Connection conn = dataSource.getConnection()) {
        try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
            preparedStatement.setInt(1, maxDeliveryTryTimes);
            preparedStatement.setString(2, SoftTransactionType.BestEffortsDelivery.name());
            preparedStatement.setLong(3, System.currentTimeMillis() - maxDeliveryTryDelayMillis);
            preparedStatement.setInt(4, size);
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    Gson gson = new Gson();
                    //TODO 对于批量执行的参数需要解析成两层列表
                    List<Object> parameters = gson.fromJson(rs.getString(5), new TypeToken<List<Object>>() {
                    }.getType());
                    result.add(new TransactionLog(rs.getString(1), "", SoftTransactionType.valueOf(rs.getString(2)), rs.getString(3), rs.getString(4), parameters, rs.getLong(6), rs.getInt(7)));
                }
            }
        }
    } catch (final SQLException ex) {
        throw new TransactionLogStorageException(ex);
    }
    return result;
}
Also used : TransactionLog(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) TransactionLogStorageException(com.dangdang.ddframe.rdb.transaction.soft.exception.TransactionLogStorageException) TypeToken(com.google.gson.reflect.TypeToken) ResultSet(java.sql.ResultSet)

Example 4 with TransactionLog

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

the class RdbTransactionLogStorageTest method assertIncreaseAsyncDeliveryTryTimes.

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

Example 5 with TransactionLog

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

the class RdbTransactionLogStorageTest method assertRemoveTransactionLogStorage.

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

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