Guest User

Untitled

a guest
Jan 9th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. public class Test {
  2.  
  3. private static String URL = "jdbc:mysql:loadbalance://" +
  4. "localhost:3306,localhost:3310/test?" +
  5. "loadBalanceConnectionGroup=first&loadBalanceEnableJMX=true";
  6.  
  7. public static void main(String[] args) throws Exception {
  8. new Thread(new Repeater()).start();
  9. new Thread(new Repeater()).start();
  10. new Thread(new Repeater()).start();
  11. }
  12.  
  13. static Connection getNewConnection() throws SQLException, ClassNotFoundException {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. return DriverManager.getConnection(URL, "root", "");
  16. }
  17.  
  18. static void executeSimpleTransaction(Connection c, int conn, int trans){
  19. try {
  20. c.setAutoCommit(false);
  21. Statement s = c.createStatement();
  22. s.executeQuery("SELECT SLEEP(1) /* Connection: " + conn + ", transaction: " + trans + " */");
  23. c.commit();
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28.  
  29. public static class Repeater implements Runnable {
  30. public void run() {
  31. for(int i=0; i < 100; i++){
  32. try {
  33. Connection c = getNewConnection();
  34. for(int j=0; j < 10; j++){
  35. executeSimpleTransaction(c, i, j);
  36. Thread.sleep(Math.round(100 * Math.random()));
  37. }
  38. c.close();
  39. Thread.sleep(100);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment