jdbc RepeatableReadExample

This commit is contained in:
Struchkov Mark 2022-07-24 07:52:22 +03:00
parent 8e259a6a4e
commit 8547eee701
4 changed files with 74 additions and 9 deletions

View File

@ -6,16 +6,16 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JdbcExample {
public class JdbcSimpleExample {
private static final String INSERT_TRANSACTION_SQL = "INSERT INTO transaction(person_from, person_to, amount) values (?, ?, ?)";
private static final String UPDATE_BALANCE_PERSON_FROM_SQL = "UPDATE person SET balance = (balance - ?) WHERE id = ?";
private static final String UPDATE_BALANCE_PERSON_TO_SQL = "UPDATE person SET balance = (balance + ?) WHERE id = ?";
public static final String INSERT_TRANSACTION_SQL = "INSERT INTO transaction(person_from, person_to, amount) values (?, ?, ?)";
public static final String UPDATE_BALANCE_PERSON_FROM_SQL = "UPDATE person SET balance = (balance - ?) WHERE id = ?";
public static final String UPDATE_BALANCE_PERSON_TO_SQL = "UPDATE person SET balance = (balance + ?) WHERE id = ?";
public static void main(String[] args) {
final JdbcExample jdbcExample = new JdbcExample();
final JdbcSimpleExample jdbcSimpleExample = new JdbcSimpleExample();
// jdbcExample.runNoTransaction(2L, 1L, 100L);
jdbcExample.runWithTransaction(2L, 1L, 100L);
jdbcSimpleExample.runWithTransaction(2L, 1L, 100L);
}
@SneakyThrows
@ -32,10 +32,9 @@ public class JdbcExample {
final Connection connection = Repository.getConnection();
try (connection) {
connection.setAutoCommit(false);
sendMoney(connection, personIdFrom, personIdTo, amount);
connection.commit();
} catch (RuntimeException | SQLException e) {
connection.rollback();
} finally {

View File

@ -0,0 +1,61 @@
package dev.struchkov.example.transaction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class RepeatableReadExample {
public static final String READ = "SELECT person.balance FROM person WHERE id = ?";
public static final String UPDATE = "UPDATE person SET balance = ? WHERE id = ?";
public static void main(String[] args) {
try {
final Connection connectionOne = Repository.getConnection();
final Connection connectionTwo = Repository.getConnection();
connectionOne.setAutoCommit(false);
connectionTwo.setAutoCommit(false);
final int transactionLevel = Connection.TRANSACTION_REPEATABLE_READ;
connectionOne.setTransactionIsolation(transactionLevel);
connectionTwo.setTransactionIsolation(transactionLevel);
final PreparedStatement readOne = connectionOne.prepareStatement(READ);
readOne.setLong(1, 1);
final PreparedStatement readTwo = connectionTwo.prepareStatement(READ);
readTwo.setLong(1, 1);
final ResultSet resultSetOne = readOne.executeQuery();
resultSetOne.next();
final long balanceOne = resultSetOne.getLong(1);
final ResultSet resultSetTwo = readTwo.executeQuery();
resultSetTwo.next();
final long balanceTwo = resultSetTwo.getLong(1);
final PreparedStatement updateOne = connectionOne.prepareStatement(UPDATE);
updateOne.setLong(1, balanceOne + 10);
updateOne.setLong(2, 1);
updateOne.execute();
connectionOne.commit();
connectionOne.close();
final PreparedStatement updateTwo = connectionTwo.prepareStatement(UPDATE);
updateTwo.setLong(1, balanceTwo + 5);
updateTwo.setLong(2, 1);
updateTwo.execute();
connectionTwo.commit();
connectionTwo.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}

View File

@ -1,5 +1,7 @@
package dev.struchkov.example.transaction;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.sql.Connection;
@ -7,7 +9,8 @@ import java.sql.DriverManager;
import java.sql.SQLException;
@Slf4j
public class Repository {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class Repository {
public static Connection getConnection() {
try {

View File

@ -10,6 +10,8 @@
<modules>
<module>jdbc-transaction</module>
<module>hibernate-transaction</module>
<module>spring-transaction</module>
</modules>
<properties>