I've wrote code with 'phantom read' and my code should print different values if isolation level not serializable, but I have 'repeatable read' isolation level and it works like serializable. It shows me same digits, but should second time show bigger digit. Why so? I've MySql Database. Here my Example:
public class PhantomReadLesson {
static String url = "jdbc:mysql://localhost:3306/Lessons";
static String username = "root";
static String password = "1";
public static void main(String[] args) throws SQLException, InterruptedException {
try(Connection conn = DriverManager.getConnection(url, username, password);
Statement statement = conn.createStatement()) {
conn.setAutoCommit(false);
conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
ResultSet rs = statement.executeQuery("Select count(*) from Books");
while(rs.next()){
System.out.println(rs.getInt(1));
}
new OtherTransaction2().start();
Thread.currentThread().sleep(1000);
rs = statement.executeQuery("Select count(*) from Books");
while(rs.next()){
System.out.println(rs.getString(1));
}
}
}
static class OtherTransaction2 extends Thread {
@Override
public void run() {
try(Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement()) {
conn.setAutoCommit(false);
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
stmt.executeUpdate("insert into Books (name) VALUES ('new Row')");
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
I'm imitating 'phantom reads' in here. If I use 'repeatable_read' or 'serializable' levels it show the same numbers, if use 'read_commmited' or 'read_uncomited' levels it will show different numbers. But according to java doc https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html only serializable save from 'phantom reads'. So why repeatable read level save from 'phanotom read'?