有时候可能需要重试机制,比如mysql连接长时间没有请求,会自动断开,下次使用时需要重新连接
public static List<Map<String, String>> execute(String queruSQL) { try { List<Map<String, String>> table = new ArrayList<>(); for (int i = 0; i < 2; i++) { if (conn != null && conn.isValid(10)) { ps = conn.prepareStatement(queruSQL); ResultSet rs = ps.executeQuery(); return rs; } else { if (i == 0) { System.out.println("mysql is invalid, reinit..."); if (conn != null) conn.close(); init(); } else { System.out.println("mysql still invalid after reinit"); } } } } catch (Exception e) { throw new RuntimeException(e); } throw new RuntimeException("mysql fail after try 2 times"); }
用于跟踪一个事件从开始到处理结束的所有细节。
写了一个Tracker类,见https://github.com/fangqiang/JavaProject