From b4c0d30bbaa24790669602cc25b35ef3f27f7d88 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 24 Jul 2022 08:40:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=BB=D0=BB=D1=8E=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../problems/DirtyReadExample.java | 49 ++++++++++++++++ .../problems/NonRepeatableRead.java | 56 +++++++++++++++++++ .../transaction/problems/PhantomRead.java | 56 +++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/DirtyReadExample.java create mode 100644 jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/NonRepeatableRead.java create mode 100644 jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/PhantomRead.java diff --git a/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/DirtyReadExample.java b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/DirtyReadExample.java new file mode 100644 index 0000000..b062485 --- /dev/null +++ b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/DirtyReadExample.java @@ -0,0 +1,49 @@ +package dev.struchkov.example.transaction.problems; + +import dev.struchkov.example.transaction.Repository; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class DirtyReadExample { + + public static void main(String[] args) throws SQLException, InterruptedException { + try ( + final Connection connection = Repository.getConnection(); + final Statement statement = connection.createStatement() + ) { + connection.setAutoCommit(false); + connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + + statement.execute("UPDATE person SET balance = 100000 WHERE id = 1"); + + new OtherTransaction().start(); + Thread.sleep(2000); + connection.rollback(); + } + + } + + static class OtherTransaction extends Thread { + @Override + public void run() { + try ( + final Connection connection = Repository.getConnection(); + final Statement statement = connection.createStatement() + ) { + connection.setAutoCommit(false); + connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); + + final ResultSet resultSet = statement.executeQuery("SELECT * FROM person WHERE id = 1"); + while (resultSet.next()) { + System.out.println(resultSet.getString("balance")); + } + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + } + } + +} diff --git a/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/NonRepeatableRead.java b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/NonRepeatableRead.java new file mode 100644 index 0000000..b372917 --- /dev/null +++ b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/NonRepeatableRead.java @@ -0,0 +1,56 @@ +package dev.struchkov.example.transaction.problems; + +import dev.struchkov.example.transaction.Repository; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class NonRepeatableRead { + + public static void main(String[] args) { + try( + final Connection connection = Repository.getConnection(); + final Statement statement = connection.createStatement() + ) { + connection.setAutoCommit(false); + connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + + final ResultSet resultSet = statement.executeQuery("SELECT * FROM person WHERE id = 1"); + while (resultSet.next()) { + System.out.println(resultSet.getString("balance")); + } + + new OtherTransaction().start(); + Thread.sleep(2000); + + final ResultSet resultSetTwo = statement.executeQuery("SELECT * FROM person WHERE id = 1"); + while (resultSetTwo.next()) { + System.out.println(resultSetTwo.getString("balance")); + } + + } catch (SQLException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + static class OtherTransaction extends Thread { + @Override + public void run() { + try ( + final Connection connection = Repository.getConnection(); + final Statement statement = connection.createStatement() + ) { + connection.setAutoCommit(false); + connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + + statement.executeUpdate("UPDATE person SET balance = 100000 WHERE id = 1"); + connection.commit(); + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + } + } + +} diff --git a/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/PhantomRead.java b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/PhantomRead.java new file mode 100644 index 0000000..2829f80 --- /dev/null +++ b/jdbc-transaction/src/main/java/dev/struchkov/example/transaction/problems/PhantomRead.java @@ -0,0 +1,56 @@ +package dev.struchkov.example.transaction.problems; + +import dev.struchkov.example.transaction.Repository; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class PhantomRead { + + public static void main(String[] args) { + try( + final Connection connection = Repository.getConnection(); + final Statement statement = connection.createStatement() + ) { + connection.setAutoCommit(false); + connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + + final ResultSet resultSet = statement.executeQuery("SELECT count(*) FROM person"); + while (resultSet.next()) { + System.out.println(resultSet.getInt(1)); + } + + new OtherTransaction().start(); + Thread.sleep(2000); + + final ResultSet resultSetTwo = statement.executeQuery("SELECT count(*) FROM person"); + while (resultSetTwo.next()) { + System.out.println(resultSetTwo.getInt(1)); + } + + } catch (SQLException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + static class OtherTransaction extends Thread { + @Override + public void run() { + try ( + final Connection connection = Repository.getConnection(); + final Statement statement = connection.createStatement() + ) { + connection.setAutoCommit(false); + connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + + statement.executeUpdate("INSERT INTO person(name, balance) values ('test', 100)"); + connection.commit(); + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + } + } + +}