1 30 31 32 package org.hsqldb.test; 33 34 import java.sql.Connection ; 35 import java.sql.DriverManager ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.Savepoint ; 39 import java.sql.Statement ; 40 41 import org.hsqldb.WebServer; 42 43 import junit.framework.TestCase; 44 import junit.framework.TestResult; 45 46 54 public class TestJDBCSavepoints extends TestCase { 55 56 String serverProps = "database.0=mem:test;silent=true;trace=false"; 59 60 String url = "jdbc:hsqldb:http://localhost"; 62 String user; 63 String password; 64 Statement stmt; 65 Connection conn1; 66 Connection conn2; 67 68 WebServer server; 73 74 public TestJDBCSavepoints(String name) { 75 super(name); 76 } 77 78 protected void setUp() { 79 80 user = "sa"; 81 password = ""; 82 stmt = null; 83 conn1 = null; 84 conn2 = null; 85 86 server = new WebServer(); 88 89 server.putPropertiesFromString(serverProps); 90 server.setLogWriter(null); 91 server.start(); 92 93 try { 94 Class.forName("org.hsqldb.jdbcDriver"); 95 96 conn1 = DriverManager.getConnection(url, user, password); 97 conn2 = DriverManager.getConnection(url, user, password); 98 stmt = conn1.createStatement(); 99 } catch (Exception e) { 100 101 System.out.println(this + ".setUp() error: " + e.getMessage()); 103 } 104 } 105 106 protected void tearDown() { 107 108 try { 109 conn1.close(); 110 } catch (Exception e) { 111 112 System.out.println(this + ".tearDown() error: " + e.getMessage()); 114 } 115 116 try { 117 conn2.close(); 118 } catch (Exception e) { 119 120 System.out.println(this + ".tearDown() error: " + e.getMessage()); 122 } 123 124 server.stop(); 125 } 126 127 public void testJDBCSavepoints() throws Exception { 128 129 String sql; 130 String msg; 131 int i; 132 PreparedStatement ps; 133 ResultSet rs; 134 Savepoint sp1; 135 Savepoint sp2; 136 Savepoint sp3; 137 Savepoint sp4; 138 Savepoint sp5; 139 Savepoint sp6; 140 Savepoint sp7; 141 int rowcount = 0; 142 143 sql = "drop table t if exists"; 144 145 stmt.executeUpdate(sql); 146 147 sql = "create table t(id int, fn varchar, ln varchar, zip int)"; 148 149 stmt.executeUpdate(sql); 150 conn1.setAutoCommit(true); 151 152 166 167 conn1.setAutoCommit(false); 169 170 sql = "insert into t values(?,?,?,?)"; 171 ps = conn1.prepareStatement(sql); 172 173 ps.setString(2, "Mary"); 174 ps.setString(3, "Peterson-Clancy"); 175 176 i = 0; 177 178 for (; i < 10; i++) { 179 ps.setInt(1, i); 180 ps.setInt(4, i); 181 ps.executeUpdate(); 182 } 183 184 sp1 = conn1.setSavepoint("savepoint1"); 185 186 for (; i < 20; i++) { 187 ps.setInt(1, i); 188 ps.setInt(4, i); 189 ps.executeUpdate(); 190 } 191 192 sp2 = conn1.setSavepoint("savepoint2"); 193 194 for (; i < 30; i++) { 195 ps.setInt(1, i); 196 ps.setInt(4, i); 197 ps.executeUpdate(); 198 } 199 200 sp3 = conn1.setSavepoint("savepoint3"); 201 202 for (; i < 40; i++) { 203 ps.setInt(1, i); 204 ps.setInt(4, i); 205 ps.executeUpdate(); 206 } 207 208 sp4 = conn1.setSavepoint("savepoint4"); 209 210 for (; i < 50; i++) { 211 ps.setInt(1, i); 212 ps.setInt(4, i); 213 ps.executeUpdate(); 214 } 215 216 sp5 = conn1.setSavepoint("savepoint5"); 217 sp6 = conn1.setSavepoint("savepoint6"); 218 sp7 = conn1.setSavepoint("savepoint7"); 219 rs = stmt.executeQuery("select count(*) from t"); 220 221 rs.next(); 222 223 rowcount = rs.getInt(1); 224 225 rs.close(); 226 227 msg = "select count(*) from t value"; 231 232 try { 233 assertEquals(msg, 50, rowcount); 234 } catch (Exception e) {} 235 236 conn2.setAutoCommit(false); 237 conn2.setSavepoint("savepoint1"); 238 conn2.setSavepoint("savepoint2"); 239 240 msg = "savepoint released succesfully on non-originating connection"; 244 245 try { 246 conn2.releaseSavepoint(sp2); 247 assertTrue(msg, false); 248 } catch (Exception e) {} 249 250 try { 254 conn2.rollback(sp1); 255 256 msg = "succesful rollback to savepoint on " 257 + "non-originating connection"; 258 259 assertTrue(msg, false); 260 } catch (Exception e) {} 261 262 msg = "direct execution of <release savepoint> statement failed to " 266 + "release JDBC-created SQL-savepoint with identical savepoint name"; 267 268 try { 269 conn2.createStatement().executeUpdate( 270 "release savepoint \"savepoint2\""); 271 } catch (Exception e) { 272 try { 273 assertTrue(msg, false); 274 } catch (Exception e2) {} 275 } 276 277 msg = "direct execution of <rollback to savepoint> statement failed to " 282 + "roll back to existing JDBC-created SQL-savepoint with identical " 283 + "savepoint name"; 284 285 try { 286 conn2.createStatement().executeUpdate( 287 "rollback to savepoint \"savepoint1\""); 288 } catch (Exception e) { 289 e.printStackTrace(); 290 291 try { 292 assertTrue(msg, false); 293 } catch (Exception e2) {} 294 } 295 296 conn1.releaseSavepoint(sp6); 297 298 msg = "savepoint released succesfully > 1 times"; 300 301 try { 302 conn1.releaseSavepoint(sp6); 303 assertTrue(msg, false); 304 } catch (Exception e) {} 305 306 msg = "savepoint released successfully after preceding savepoint released"; 309 310 try { 311 conn1.releaseSavepoint(sp7); 312 assertTrue(msg, false); 313 } catch (Exception e) {} 314 315 msg = "preceding same-point savepoint destroyed by following savepoint release"; 318 319 try { 320 conn1.releaseSavepoint(sp5); 321 } catch (Exception e) { 322 try { 323 assertTrue(msg, false); 324 } catch (Exception e2) {} 325 } 326 327 conn1.rollback(sp4); 328 329 rs = stmt.executeQuery("select count(*) from t"); 330 331 rs.next(); 332 333 rowcount = rs.getInt(1); 334 335 rs.close(); 336 337 msg = "select * rowcount after 50 inserts - 10 rolled back:"; 340 341 try { 342 assertEquals(msg, 40, rowcount); 343 } catch (Exception e) {} 344 345 msg = "savepoint rolled back succesfully > 1 times"; 348 349 try { 350 conn1.rollback(sp4); 351 assertTrue(msg, false); 352 } catch (Exception e) {} 353 354 conn1.rollback(sp3); 355 356 rs = stmt.executeQuery("select count(*) from t"); 357 358 rs.next(); 359 360 rowcount = rs.getInt(1); 361 362 rs.close(); 363 364 msg = "select count(*) after 50 inserts - 20 rolled back:"; 367 368 try { 369 assertEquals(msg, 30, rowcount); 370 } catch (Exception e) {} 371 372 msg = "savepoint released succesfully after use in rollback"; 375 376 try { 377 conn1.releaseSavepoint(sp3); 378 assertTrue(msg, false); 379 } catch (Exception e) {} 380 381 conn1.rollback(sp1); 382 383 msg = "savepoint rolled back without raising an exception after " 387 + "rollback to a preceeding savepoint"; 388 389 try { 390 conn1.rollback(sp2); 391 assertTrue(msg, false); 392 } catch (Exception e) {} 393 394 conn1.rollback(); 395 396 msg = "savepoint released succesfully when it should have been " 400 + "destroyed by a full rollback"; 401 402 try { 403 conn1.releaseSavepoint(sp1); 404 assertTrue(msg, false); 405 } catch (Exception e) {} 406 407 conn1.setAutoCommit(false); 408 409 sp1 = conn1.setSavepoint("savepoint1"); 410 411 conn1.rollback(); 412 conn1.setAutoCommit(false); 413 conn1.createStatement().executeUpdate("savepoint \"savepoint1\""); 414 415 429 conn1.setAutoCommit(false); 430 431 sp1 = conn1.setSavepoint("savepoint1"); 432 433 conn1.createStatement().executeUpdate("savepoint \"savepoint1\""); 434 435 449 450 464 conn1.setAutoCommit(false); 465 466 sp1 = conn1.setSavepoint("savepoint1"); 467 468 conn1.createStatement().executeUpdate("savepoint \"savepoint1\""); 469 470 484 } 485 486 489 public static void main(String [] args) throws Exception { 490 491 TestResult result; 492 TestCase test; 493 java.util.Enumeration failures; 494 int count; 495 496 result = new TestResult(); 497 test = new TestJDBCSavepoints("testJDBCSavepoints"); 498 499 test.run(result); 500 501 count = result.failureCount(); 502 503 System.out.println("TestJDBCSavepoints failure count: " + count); 504 505 failures = result.failures(); 506 507 while (failures.hasMoreElements()) { 508 System.out.println(failures.nextElement()); 509 } 510 } 511 } 512 | Popular Tags |