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;
}
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);
}
}
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;
}
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);
}
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));
}
Aggregations