Search in sources :

Example 1 with TransactionLogStorageException

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

the class RdbTransactionLogStorage method add.

@Override
public void add(final TransactionLog transactionLog) {
    String sql = "INSERT INTO `transaction_log` (`id`, `transaction_type`, `data_source`, `sql`, `parameters`, `creation_time`) VALUES (?, ?, ?, ?, ?, ?);";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        preparedStatement.setString(1, transactionLog.getId());
        preparedStatement.setString(2, SoftTransactionType.BestEffortsDelivery.name());
        preparedStatement.setString(3, transactionLog.getDataSource());
        preparedStatement.setString(4, transactionLog.getSql());
        preparedStatement.setString(5, new Gson().toJson(transactionLog.getParameters()));
        preparedStatement.setLong(6, transactionLog.getCreationTime());
        preparedStatement.executeUpdate();
    } catch (final SQLException ex) {
        throw new TransactionLogStorageException(ex);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) TransactionLogStorageException(com.dangdang.ddframe.rdb.transaction.soft.exception.TransactionLogStorageException)

Example 2 with TransactionLogStorageException

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

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

the class RdbTransactionLogStorage method increaseAsyncDeliveryTryTimes.

@Override
public void increaseAsyncDeliveryTryTimes(final String id) {
    String sql = "UPDATE `transaction_log` SET `async_delivery_try_times`=`async_delivery_try_times`+1 WHERE `id`=?;";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        preparedStatement.setString(1, id);
        preparedStatement.executeUpdate();
    } catch (final SQLException ex) {
        throw new TransactionLogStorageException(ex);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TransactionLogStorageException(com.dangdang.ddframe.rdb.transaction.soft.exception.TransactionLogStorageException)

Example 4 with TransactionLogStorageException

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

the class RdbTransactionLogStorage method remove.

@Override
public void remove(final String id) {
    String sql = "DELETE FROM `transaction_log` WHERE `id`=?;";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        preparedStatement.setString(1, id);
        preparedStatement.executeUpdate();
    } catch (final SQLException ex) {
        throw new TransactionLogStorageException(ex);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TransactionLogStorageException(com.dangdang.ddframe.rdb.transaction.soft.exception.TransactionLogStorageException)

Aggregations

TransactionLogStorageException (com.dangdang.ddframe.rdb.transaction.soft.exception.TransactionLogStorageException)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 Gson (com.google.gson.Gson)2 TransactionLog (com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog)1 TypeToken (com.google.gson.reflect.TypeToken)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1