import java.io.*;
import java.sql.*;
import java.util.Properties;

public class TestMysqlR1{

	public static void main(String[] args){
		Connection conn=null;
		Statement st =null;
		try{	
			Class.forName("com.mysql.jdbc.Driver");			
			Properties props = new Properties();
			props.put("password", "123456");
			props.put("autoReconnect", "true");
			props.put("roundRobinLoadBalance", "false");
			props.put("failOverReadOnly","true");		// I want to turn it to false manually
			props.put("user", "ppg");
			props.put("password", "123456");
			props.put("autoReconnectForPools", "false");
			props.put("queriesBeforeRetryMaster","2");
			props.put("secondsBeforeRetryMaster","60000");
			props.put("autoCommit","true");
			props.put("connectTimeout", "3000");
			props.put("socketTimeout", "3000");
			props.put("initialTimeout","1");
			props.put("prepStmtCacheSize","50");
			conn=DriverManager.getConnection("jdbc:mysql://192.168.0.30:3306,192.168.0.26:3306/ppg_load", 
								props);	
			st=conn.createStatement();
			st.addBatch("insert into TEST (ID, COUNT, UPDATE_TIME) values(1,1, now())");
			st.addBatch("insert into TEST (ID, COUNT, UPDATE_TIME) values(2,1, now())");
			st.addBatch("insert into TEST (ID, COUNT, UPDATE_TIME) values(3,1, now())");
			st.addBatch("update TEST set COUNT=10");
			st.executeBatch();
			st.close();
			System.out.println("Shutdown master");
			Thread.sleep(20000);
			System.out.println("master has shutdown");
			st=conn.createStatement();
			st.addBatch("insert into TEST (ID, COUNT, UPDATE_TIME) values(1,1, now())");//this statement has been losted
			st.addBatch("insert into TEST (ID, COUNT, UPDATE_TIME) values(2,1, now())");
			st.addBatch("insert into TEST (ID, COUNT, UPDATE_TIME) values(3,1, now())");//this statement has been losted
			st.addBatch("update TEST set COUNT=10");									//this statement has been losted
			st.executeBatch();
		}catch(Exception e){
			//e.printStackTrace(System.err);		
			try{
				conn.setReadOnly(false);	// I open back the connection for update/insert
				st.executeBatch();			// retry again
			}catch(Exception ee){
				ee.printStackTrace(System.err);		
			}		
		}finally{
			try{
				if(st!=null)st.close();
				if(conn!=null) conn.close();	
			}catch(Exception e){}	
		}				
	}
}