1 24 25 package org.objectweb.cjdbc.scenario.raidb1.loadbalancer; 26 27 import java.sql.Connection ; 28 import java.sql.DriverManager ; 29 import java.sql.Statement ; 30 import java.util.ArrayList ; 31 import java.util.Properties ; 32 33 import org.objectweb.cjdbc.scenario.templates.SimpleRaidb1Template; 34 35 41 public class Raidb1ParallelDBScenario extends SimpleRaidb1Template 42 { 43 46 public void testFailFast() 47 { 48 try 49 { 50 cm.loadVirtualDatabases(controller, "myDB", 52 "hsqldb-raidb1-parallel-lprf.xml"); 53 mainVdb = controller.getVirtualDatabase("myDB"); 54 mainVdb.enableAllBackends(); 55 execute(); 56 } 57 catch (Exception e) 58 { 59 e.printStackTrace(); 60 fail("failed to test c-jdbc parallel loadbalancer of type least pending request first"); 61 } 62 } 63 64 67 public void testRoundRobin() 68 { 69 try 70 { 71 cm.loadVirtualDatabases(controller, "myDB", 73 "hsqldb-raidb1-parallel-roundrobin.xml"); 74 mainVdb = controller.getVirtualDatabase("myDB"); 75 mainVdb.enableAllBackends(); 76 execute(); 77 } 78 catch (Exception e) 79 { 80 e.printStackTrace(); 81 fail("failed to test c-jdbc parallel loadbalancer of type round robin"); 82 } 83 } 84 85 private void execute() throws Exception 86 { 87 Class.forName("org.objectweb.cjdbc.driver.Driver"); 88 int threadCount = 3; 89 ArrayList threads = new ArrayList (threadCount); 90 for (int i = 0; i < threadCount; i++) 91 { 92 ParallelThread par = new ParallelThread(); 93 par.start(); 94 threads.add(par); 95 } 96 for (int i = 0; i < threadCount; i++) 97 { 98 ParallelThread par = (ParallelThread) threads.get(i); 99 par.join(); 100 } 101 for (int i = 0; i < threadCount; i++) 102 { 103 ParallelThread par = (ParallelThread) threads.get(i); 104 assertNull("A thread had an exception" , 105 par.exception); 106 } 107 } 108 109 class ParallelThread extends Thread 110 { 111 Exception exception = null; 112 113 116 public void run() 117 { 118 try 119 { 120 Properties props = new Properties (); 122 props.put("user", "user"); 123 props.put("password", ""); 124 Connection con = DriverManager.getConnection( 125 "jdbc:cjdbc://localhost/myDB", props); 126 assertNotNull("Connection is null", con); 127 Statement statement = con.createStatement(); 128 for (int i = 0; i < 20; i++) 129 { 130 statement.executeUpdate("update product set name='Parallel Test" + i 131 + "'"); 132 statement.executeQuery("select * from PRODUCT"); 133 } 134 } 135 catch (Exception e) 136 { 137 if (exception == null) 138 { 139 exception = e; 140 } 141 e.printStackTrace(); 142 } 143 } 144 } 145 } 146 | Popular Tags |