// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class SafeOrNot extends Thread {
    public static void exec(String sql) throws Exception {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "", "");
        exec(con, sql);
        con.close();
    }
    public static void exec(Connection con, String sql) throws Exception {
        Statement stmt = con.createStatement();
        stmt.executeUpdate(sql);
        stmt.close();
    }
    public void run() {
        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "", "");
            for(int i = 0; i < 10000; i++) {
                exec(con, "INSERT INTO tricky(val) SELECT MAX(val)+1 FROM tricky");
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        exec("CREATE TABLE tricky (id INTEGER AUTO_INCREMENT PRIMARY KEY, val INTEGER) TYPE=xxxxxx");
        exec("INSERT INTO tricky(val) VALUES(1)");
        SafeOrNot[] t = new SafeOrNot[25];
        for(int i = 0; i < t.length; i++) {
            t[i] = new SafeOrNot();
        }
        for(int i = 0; i < t.length; i++) {
            t[i].start();
        }
        for(int i = 0; i < t.length; i++) {
            t[i].join();
        }
        exec("DROP TABLE tricky");
    }
}
