Заменил бд на H2

This commit is contained in:
Struchkov Mark 2022-07-24 08:54:03 +03:00
parent b4c0d30bba
commit 0ab798cb5f
3 changed files with 20 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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);

View File

@ -39,5 +39,10 @@
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
</dependencies>
</project>