-
Notifications
You must be signed in to change notification settings - Fork 3
/
TransactionIsolation.java
71 lines (57 loc) · 2.02 KB
/
TransactionIsolation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.jayden.study.transaction;
import com.jayden.study.utils.JdbcUtils;
import java.sql.*;
/**
* Transaction Isolation Levels
*
* @author jayden-lee
*/
public class TransactionIsolation {
private static final String url = "jdbc:mysql://localhost:3306/mysql";
private static final String user = "user";
private static final String password = "password";
private static Connection connection;
public enum TxIsolationLevel {
NONE("NONE", 0),
READ_UNCOMMITTED("READ UNCOMMITTED", 1),
READ_COMMITTED("READ COMMITTED", 2),
REPEATABLE_READ("REPEATABLE READ", 4),
SERIALIZABLE("SERIALIZABLE", 8);
private String name;
private int level;
TxIsolationLevel(String name, int level) {
this.name = name;
this.level = level;
}
public String getName() {
return name;
}
public int getLevel() {
return level;
}
}
public static void main(String[] args) {
try {
connection = JdbcUtils.getConnection(url, user, password);
for (TxIsolationLevel txIsolationLevel : TxIsolationLevel.values()) {
changeTxIsolationLevel(txIsolationLevel);
printTxIsolationInfo(txIsolationLevel);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeConnection(connection);
}
}
private static void changeTxIsolationLevel(TxIsolationLevel txIsolationLevel) throws SQLException {
String sql = "SET SESSION TRANSACTION ISOLATION LEVEL " + txIsolationLevel.getName();
try (Statement statement = connection.createStatement()) {
statement.execute(sql);
} catch (SQLException e) {
throw e;
}
}
private static void printTxIsolationInfo(TxIsolationLevel txIsolationLevel) throws SQLException {
System.out.println(txIsolationLevel.getName() + ", " + connection.getTransactionIsolation());
}
}