From 8547eee7014145cd8b06b543e674ccc2b27acaf8 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 24 Jul 2022 07:52:22 +0300 Subject: [PATCH] jdbc RepeatableReadExample --- ...dbcExample.java => JdbcSimpleExample.java} | 15 +++-- .../transaction/RepeatableReadExample.java | 61 +++++++++++++++++++ .../example/transaction/Repository.java | 5 +- pom.xml | 2 + 4 files changed, 74 insertions(+), 9 deletions(-) rename jdbc-transaction/src/main/java/dev/struchkov/example/transaction/{JdbcExample.java => JdbcSimpleExample.java} (78%) create mode 100644 jdbc-transaction/src/main/java/dev/struchkov/example/transaction/RepeatableReadExample.java diff --git a/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/JdbcExample.java b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/JdbcSimpleExample.java similarity index 78% rename from jdbc-transaction/src/main/java/dev/struchkov/example/transaction/JdbcExample.java rename to jdbc-transaction/src/main/java/dev/struchkov/example/transaction/JdbcSimpleExample.java index 0d6c30e..96aff06 100644 --- a/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/JdbcExample.java +++ b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/JdbcSimpleExample.java @@ -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 { diff --git a/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/RepeatableReadExample.java b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/RepeatableReadExample.java new file mode 100644 index 0000000..c1fd0fc --- /dev/null +++ b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/RepeatableReadExample.java @@ -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); + } + + } + +} diff --git a/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/Repository.java b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/Repository.java index a9bbba7..3ddf9ee 100644 --- a/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/Repository.java +++ b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/Repository.java @@ -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 { diff --git a/pom.xml b/pom.xml index 955dc3e..091add6 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,8 @@ jdbc-transaction + hibernate-transaction + spring-transaction