package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestTimeout {
	public static void main(String[] args) throws SQLException {		
		Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?connectTimeout=5&socketTimeout=2", "sa", "sa");
		boolean wasException = false;
		boolean bugReproduced = false;
		for(int i = 0; i < 10000; i++) {
			try {
				int v1 = selectValue(con, 1);
				int v2 = selectValue(con, 2);
				if (v1 != 1 || v2 != 2) {
					bugReproduced = true;
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
				wasException = true;
			}
		}
		
		System.out.println("Was exception:" + wasException + " Bug reproduced:" + bugReproduced);
	}
	
	private static int selectValue(Connection con, int value) throws SQLException {
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery("select " + value);
			rs.next();
			return rs.getInt(1);
		} finally {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}
	}
}
