Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- private static String URL = "jdbc:mysql:loadbalance://" +
- "localhost:3306,localhost:3310/test?" +
- "loadBalanceConnectionGroup=first&loadBalanceEnableJMX=true";
- public static void main(String[] args) throws Exception {
- new Thread(new Repeater()).start();
- new Thread(new Repeater()).start();
- new Thread(new Repeater()).start();
- }
- static Connection getNewConnection() throws SQLException, ClassNotFoundException {
- Class.forName("com.mysql.jdbc.Driver");
- return DriverManager.getConnection(URL, "root", "");
- }
- static void executeSimpleTransaction(Connection c, int conn, int trans){
- try {
- c.setAutoCommit(false);
- Statement s = c.createStatement();
- s.executeQuery("SELECT SLEEP(1) /* Connection: " + conn + ", transaction: " + trans + " */");
- c.commit();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static class Repeater implements Runnable {
- public void run() {
- for(int i=0; i < 100; i++){
- try {
- Connection c = getNewConnection();
- for(int j=0; j < 10; j++){
- executeSimpleTransaction(c, i, j);
- Thread.sleep(Math.round(100 * Math.random()));
- }
- c.close();
- Thread.sleep(100);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment