1 22 package org.jboss.test.jca.ejb; 23 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.sql.Statement ; 27 28 import javax.ejb.EJBException ; 29 import javax.ejb.SessionBean ; 30 import javax.ejb.SessionContext ; 31 import javax.naming.InitialContext ; 32 import javax.sql.DataSource ; 33 import javax.transaction.UserTransaction ; 34 35 import org.jboss.logging.Logger; 36 import org.jboss.resource.adapter.jdbc.WrappedConnection; 37 38 50 public class LocalWrapperCleanupTestSessionBean implements SessionBean 51 { 52 53 private static final long serialVersionUID = 1L; 54 55 56 private static final Logger log = Logger.getLogger(LocalWrapperCleanupTestSessionBean.class); 57 58 public void testAutoCommitInReturnedConnection() 59 { 60 Connection c = null; 61 try 62 { 63 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/SingleConnectionDS"); 64 c = ds.getConnection(); 65 if (c.getAutoCommit() == false) 66 { 67 throw new EJBException ("Initial autocommit state false!"); 68 } 69 c.setAutoCommit(false); 70 c.commit(); 71 c.close(); 72 c = null; 73 c = ds.getConnection(); 74 if (c.getAutoCommit() == false) 75 { 76 throw new EJBException ("Returned and reaccessed autocommit state false!"); 77 } 78 c.close(); 79 c = null; 80 } 81 catch (EJBException e) 82 { 83 throw e; 84 } 85 catch (Exception e) 86 { 87 log.error("Error", e); 88 throw new EJBException ("Untested problem in test: " + e); 89 } 90 finally 91 { 92 try 93 { 94 if (c != null) 95 c.close(); 96 } 97 catch (Throwable ignored) 98 { 99 } 100 } 101 } 102 103 public void testAutoCommit() 104 { 105 Connection c1 = null; 106 Connection c2 = null; 107 try 108 { 109 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/DefaultDS"); 110 c1 = ds.getConnection(); 111 Connection uc1 = ((WrappedConnection) c1).getUnderlyingConnection(); 112 if (c1.getAutoCommit() == false) 113 { 114 throw new EJBException ("Initial autocommit state false!"); 115 } 116 117 c2 = ds.getConnection(); 118 if (c2.getAutoCommit() == false) 119 { 120 throw new EJBException ("Initial autocommit state false!"); 121 } 122 Statement s1 = c1.createStatement(); 123 Statement s2 = c2.createStatement(); 124 s1.execute("create table autocommittest (id integer)"); 125 try 126 { 127 s1.execute("insert into autocommittest values (1)"); 128 uc1.rollback(); 129 ResultSet rs2 = s2.executeQuery("select * from autocommittest where id = 1"); 130 if (!rs2.next()) 131 { 132 throw new EJBException ("Row not visible to other connection, autocommit failed"); 133 } 134 rs2.close(); 135 136 } 137 finally 138 { 139 s1.execute("drop table autocommittest"); 140 } 141 } 142 catch (EJBException e) 143 { 144 throw e; 145 } 146 catch (Exception e) 147 { 148 log.error("Error", e); 149 throw new EJBException ("Untested problem in test: " + e); 150 } 151 finally 152 { 153 try 154 { 155 if (c1 != null) 156 c1.close(); 157 } 158 catch (Throwable ignored) 159 { 160 } 161 try 162 { 163 if (c2 != null) 164 c2.close(); 165 } 166 catch (Throwable ignored) 167 { 168 } 169 } 170 } 171 172 public void testAutoCommitOffInUserTx() 173 { 174 Connection c1 = null; 175 try 176 { 177 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/DefaultDS"); 178 c1 = ds.getConnection(); 179 Connection uc1 = ((WrappedConnection) c1).getUnderlyingConnection(); 180 if (c1.getAutoCommit() == false) 181 { 182 throw new EJBException ("Initial autocommit state false!"); 183 } 184 185 Statement s1 = c1.createStatement(); 186 s1.execute("create table autocommittest (id integer)"); 187 try 188 { 189 UserTransaction ut = (UserTransaction ) new InitialContext ().lookup("UserTransaction"); 190 ut.begin(); 191 s1.execute("insert into autocommittest values (1)"); 192 if (uc1.getAutoCommit()) 193 { 194 throw new EJBException ("Underlying autocommit is true in user tx!"); 195 } 196 197 ut.rollback(); 198 ResultSet rs1 = s1.executeQuery("select * from autocommittest where id = 1"); 199 if (rs1.next()) 200 { 201 throw new EJBException ("Row committed, autocommit still on!"); 202 } 203 rs1.close(); 204 205 } 206 finally 207 { 208 s1.execute("drop table autocommittest"); 209 } 210 } 211 catch (EJBException e) 212 { 213 throw e; 214 } 215 catch (Exception e) 216 { 217 log.error("Error", e); 218 throw new EJBException ("Untested problem in test: " + e); 219 } 220 finally 221 { 222 try 223 { 224 if (c1 != null) 225 c1.close(); 226 } 227 catch (Throwable ignored) 228 { 229 } 230 } 231 } 232 233 public void testAutoCommitOffInUserTx2() 234 { 235 try 236 { 237 createTable(); 238 UserTransaction ut = (UserTransaction ) new InitialContext ().lookup("UserTransaction"); 239 ut.begin(); 240 insertAndCheckAutoCommit(); 241 ut.rollback(); 242 } 243 catch (EJBException e) 244 { 245 throw e; 246 } 247 catch (Exception e) 248 { 249 log.error("Error", e); 250 throw new EJBException ("Untested problem in test: " + e); 251 } 252 finally 253 { 254 checkRowAndDropTable(); 255 } 256 257 } 258 259 public void testReadOnly() 260 { 261 Connection c1 = null; 262 try 263 { 264 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/DefaultDS"); 265 c1 = ds.getConnection(); 266 Connection uc1 = ((WrappedConnection) c1).getUnderlyingConnection(); 267 268 if (uc1.isReadOnly() == true) 269 throw new EJBException ("Initial underlying readonly true!"); 270 if (c1.isReadOnly() == true) 271 throw new EJBException ("Initial readonly true!"); 272 273 c1.setReadOnly(true); 274 275 if (uc1.isReadOnly() == true) 276 throw new EJBException ("Read Only should be lazy!"); 277 if (c1.isReadOnly() == false) 278 throw new EJBException ("Changed readonly false!"); 279 280 c1.createStatement(); 281 282 if (uc1.isReadOnly() == false) 283 throw new EJBException ("Lazy read only failed!"); 284 if (c1.isReadOnly() == false) 285 throw new EJBException ("Read only changed unexpectedly!"); 286 } 287 catch (EJBException e) 288 { 289 throw e; 290 } 291 catch (Exception e) 292 { 293 log.error("Error", e); 294 throw new EJBException ("Untested problem in test: " + e); 295 } 296 finally 297 { 298 try 299 { 300 if (c1 != null) 301 c1.close(); 302 } 303 catch (Throwable ignored) 304 { 305 } 306 } 307 } 308 309 public void createTable() 310 { 311 try 312 { 313 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/DefaultDS"); 314 Connection c1 = ds.getConnection(); 315 try 316 { 317 if (c1.getAutoCommit() == false) 318 throw new EJBException ("Initial autocommit state false!"); 319 Statement s1 = c1.createStatement(); 320 s1.execute("create table autocommittest (id integer)"); 321 } 322 finally 323 { 324 c1.close(); 325 } 326 } 327 catch (EJBException e) 328 { 329 throw e; 330 } 331 catch (Exception e) 332 { 333 log.error("Error", e); 334 throw new EJBException ("Untested problem in test: " + e); 335 } 336 } 337 338 public void insertAndCheckAutoCommit() 339 { 340 try 341 { 342 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/DefaultDS"); 343 Connection c1 = ds.getConnection(); 344 Connection uc1 = ((WrappedConnection) c1).getUnderlyingConnection(); 345 try 346 { 347 Statement s1 = c1.createStatement(); 348 s1.execute("insert into autocommittest values (1)"); 349 if (uc1.getAutoCommit()) 350 throw new EJBException ("Underlying autocommit is true in user tx!"); 351 } 352 finally 353 { 354 c1.close(); 355 } 356 } 357 catch (EJBException e) 358 { 359 throw e; 360 } 361 catch (Exception e) 362 { 363 log.error("Error", e); 364 throw new EJBException ("Untested problem in test: " + e); 365 } 366 367 } 368 369 public void testManualNoCommitRollback() 370 { 371 try 372 { 373 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/DefaultDS"); 374 Connection c1 = ds.getConnection(); 375 try 376 { 377 c1.setAutoCommit(false); 378 Statement s1 = c1.createStatement(); 379 s1.execute("insert into autocommittest values (1)"); 380 } 381 finally 382 { 383 c1.close(); 384 } 385 } 386 catch (EJBException e) 387 { 388 throw e; 389 } 390 catch (Exception e) 391 { 392 log.error("Error", e); 393 throw new EJBException ("Untested problem in test: " + e); 394 } 395 } 396 397 public void testManualSecondNoCommitRollback() 398 { 399 try 400 { 401 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/DefaultDS"); 402 Connection c1 = ds.getConnection(); 403 try 404 { 405 c1.setAutoCommit(false); 406 Statement s1 = c1.createStatement(); 407 s1.execute("insert into autocommittest values (0)"); 408 c1.commit(); 409 s1.execute("insert into autocommittest values (1)"); 410 } 411 finally 412 { 413 c1.close(); 414 } 415 } 416 catch (EJBException e) 417 { 418 throw e; 419 } 420 catch (Exception e) 421 { 422 log.error("Error", e); 423 throw new EJBException ("Untested problem in test: " + e); 424 } 425 } 426 427 public void checkRowAndDropTable() 428 { 429 try 430 { 431 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/DefaultDS"); 432 Connection c1 = ds.getConnection(); 433 try 434 { 435 if (c1.getAutoCommit() == false) 436 throw new EJBException ("Initial autocommit state false!"); 437 438 Statement s1 = c1.createStatement(); 439 ResultSet rs1 = s1.executeQuery("select * from autocommittest where id = 1"); 440 if (rs1.next()) 441 throw new EJBException ("Row committed, autocommit still on!"); 442 } 443 finally 444 { 445 try 446 { 447 Statement s1 = c1.createStatement(); 448 s1.execute("drop table autocommittest"); 449 } 450 catch (Throwable t) 451 { 452 log.warn("Ignored", t); 453 } 454 c1.close(); 455 } 456 457 } 458 catch (EJBException e) 459 { 460 throw e; 461 } 462 catch (Exception e) 463 { 464 log.error("Error", e); 465 throw new EJBException ("Untested problem in test: " + e); 466 } 467 468 } 469 470 public void addRowCheckAndDropTable() 471 { 472 try 473 { 474 DataSource ds = (DataSource ) new InitialContext ().lookup("java:/DefaultDS"); 475 Connection c1 = ds.getConnection(); 476 try 477 { 478 if (c1.getAutoCommit() == false) 479 throw new EJBException ("Initial autocommit state false!"); 480 c1.setAutoCommit(false); 481 482 Statement s1 = c1.createStatement(); 483 s1.execute("insert into autocommittest values (2)"); 484 c1.commit(); 485 486 ResultSet rs1 = s1.executeQuery("select * from autocommittest where id = 1"); 487 if (rs1.next()) 488 throw new EJBException ("Row committed, didn't rollback!"); 489 } 490 finally 491 { 492 try 493 { 494 Statement s1 = c1.createStatement(); 495 s1.execute("drop table autocommittest"); 496 } 497 catch (Throwable ignored) 498 { 499 } 500 c1.close(); 501 } 502 503 } 504 catch (EJBException e) 505 { 506 throw e; 507 } 508 catch (Exception e) 509 { 510 log.error("Error", e); 511 throw new EJBException ("Untested problem in test: " + e); 512 } 513 } 514 515 public void ejbCreate() 516 { 517 } 518 519 public void ejbActivate() 520 { 521 } 522 523 public void ejbPassivate() 524 { 525 } 526 527 public void ejbRemove() 528 { 529 } 530 531 public void setSessionContext(SessionContext ctx) 532 { 533 } 534 535 public void unsetSessionContext() 536 { 537 } 538 } 539 | Popular Tags |