mirror of
https://github.com/Example-uPagge/transactional.git
synced 2024-06-14 11:52:55 +03:00
Иллюстрация проблем
This commit is contained in:
parent
8547eee701
commit
b4c0d30bba
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user