1 24 25 package org.objectweb.cjdbc.scenario.horizontal; 26 27 import java.sql.ResultSet ; 28 import java.sql.Statement ; 29 30 import org.objectweb.cjdbc.controller.core.Controller; 31 import org.objectweb.cjdbc.driver.ControllerInfo; 32 import org.objectweb.cjdbc.scenario.templates.HorizontalTemplate; 33 34 39 public class TransparentHorizontalBalancingScenario extends HorizontalTemplate 40 { 41 42 47 public void testTransparentBalancing() throws Exception 48 { 49 ControllerInfo[] controllers = new ControllerInfo[]{ 50 new ControllerInfo("localhost", 25322), 51 new ControllerInfo("localhost", 25323)}; 52 String readQuery = "Select * from document"; 53 int connectionsLoad = 50; 54 55 int[] result1 = execute(controllers, connectionsLoad); 57 System.out.println(result1[0] + ":" + result1[1]); 58 assertTrue("controller1 did not get connection request before failure", 59 result1[0] > 0); 60 assertTrue("controller2 did not get connection request before failure", 61 result1[1] > 0); 62 63 org.objectweb.cjdbc.driver.Connection con = (org.objectweb.cjdbc.driver.Connection) getCJDBCConnection(controllers); 65 Statement statement = con.createStatement(); 66 ResultSet rs = statement.executeQuery(readQuery); 67 assertNotNull("ResultSet before failure is null", rs); 68 69 int wasStopped = -1; 72 System.out.println("########Stopping controller"); 73 if (con.getControllerInfo().equals(controllers[0])) 74 { 75 cm.stop(controller1.getPortNumber()); 76 assertFalse("Controller1 should be stopped now", cm.isStarted("25322")); 77 wasStopped = 1; 78 } 79 else 80 { 81 cm.stop(controller2.getPortNumber()); 82 assertFalse("Controller2 should be stopped now", cm.isStarted("25323")); 83 wasStopped = 2; 84 } 85 86 int[] result2 = execute(controllers, connectionsLoad); 88 int fresult = (wasStopped == 1) ? result2[1] : result2[0]; 89 assertTrue( 90 "remaining controller did not get connection request during failure", 91 fresult > 0); 92 93 rs = statement.executeQuery(readQuery); 95 Statement statement2 = con.createStatement(); 96 ResultSet rs2 = statement2.executeQuery(readQuery); 97 assertNotNull("ResultSet after failure is null", rs2); 98 99 System.out.println("wasStopped" + wasStopped); 100 101 if (wasStopped == 1) 103 { 104 String port1 = "25322"; 105 controller1 = (Controller) cm.start(port1).getProcess(); 106 cm.loadVirtualDatabases(controller1, "myDB", 107 "hsqldb-raidb1-distribution-1.xml"); 108 mainVdb1 = controller1.getVirtualDatabase("myDB"); 109 mainVdb1.enableAllBackends(); 110 } 111 else if (wasStopped == 2) 112 { 113 String port2 = "25323"; 114 controller2 = (Controller) cm.start(port2).getProcess(); 115 cm.loadVirtualDatabases(controller2, "myDB", 116 "hsqldb-raidb1-distribution-2.xml"); 117 mainVdb2 = controller2.getVirtualDatabase("myDB"); 118 mainVdb2.enableAllBackends(); 119 } 120 121 int[] result3 = execute(controllers, connectionsLoad); 123 assertTrue("controller1 did not get connection request after failure", 124 result3[0] > 0); 125 assertTrue("controller2 did not get connection request after failure", 126 result3[1] > 0); 127 } 128 129 137 private int[] execute(ControllerInfo[] controllers, int connections) 138 throws Exception 139 { 140 org.objectweb.cjdbc.driver.Connection con = null; 141 int[] count = new int[controllers.length]; 142 ControllerInfo connected = null; 143 for (int i = 0; i < connections; i++) 144 { 145 con = (org.objectweb.cjdbc.driver.Connection) getCJDBCConnection(controllers); 146 assertNotNull("Received null connection", con); 147 Statement statement = con.createStatement(); 148 statement.executeUpdate("update product set name='horizontalTest'"); 149 connected = con.getControllerInfo(); 150 System.out.println("Client connected to:" + connected); 151 assertNotNull("Received null for connected controller", connected); 152 for (int j = 0; j < controllers.length; j++) 153 { 154 if (connected.equals(controllers[j])) 155 { 156 count[j]++; 157 break; 158 } 159 } 160 } 161 return count; 162 } 163 } | Popular Tags |