From 0ab798cb5f86ef89210d256520280aa561e2b51d Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 24 Jul 2022 08:54:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B1=D0=B4=20=D0=BD=D0=B0=20H2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../struchkov/example/transaction/Repository.java | 12 ++++++++++++ .../transaction/problems/DirtyReadExample.java | 6 +++--- pom.xml | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-) 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 3ddf9ee..287b8e2 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 @@ -24,4 +24,16 @@ public final class Repository { return null; } + public static Connection getConnectionH2() { + try { + final String url = "jdbc:h2:file:/Users/upagge/IdeaProjects/struchkov/example/spring-boot/spring-boot-transaction/db/test"; + final String user = "sa"; + final String passwd = "pass"; + return DriverManager.getConnection(url, user, passwd); + } catch (SQLException e) { + log.error(e.getMessage(), e); + } + return null; + } + } 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 index b062485..ebc2863 100644 --- 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 @@ -11,13 +11,13 @@ public class DirtyReadExample { public static void main(String[] args) throws SQLException, InterruptedException { try ( - final Connection connection = Repository.getConnection(); + final Connection connection = Repository.getConnectionH2(); final Statement statement = connection.createStatement() ) { connection.setAutoCommit(false); connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); - statement.execute("UPDATE person SET balance = 100000 WHERE id = 1"); + statement.executeUpdate("UPDATE person SET balance = 100000 WHERE id = 1"); new OtherTransaction().start(); Thread.sleep(2000); @@ -30,7 +30,7 @@ public class DirtyReadExample { @Override public void run() { try ( - final Connection connection = Repository.getConnection(); + final Connection connection = Repository.getConnectionH2(); final Statement statement = connection.createStatement() ) { connection.setAutoCommit(false); diff --git a/pom.xml b/pom.xml index 091add6..ecfbf5d 100644 --- a/pom.xml +++ b/pom.xml @@ -39,5 +39,10 @@ slf4j-api 1.7.36 + + com.h2database + h2 + 2.1.214 +