1 22 package org.jboss.test.jca.ejb; 23 24 import java.rmi.RemoteException ; 25 import java.sql.CallableStatement ; 26 import java.sql.Connection ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.sql.Statement ; 31 32 import javax.ejb.CreateException ; 33 import javax.ejb.DuplicateKeyException ; 34 import javax.ejb.EJBException ; 35 import javax.ejb.EntityBean ; 36 import javax.ejb.EntityContext ; 37 import javax.ejb.FinderException ; 38 import javax.ejb.NoSuchEntityException ; 39 import javax.ejb.ObjectNotFoundException ; 40 import javax.naming.InitialContext ; 41 import javax.naming.NamingException ; 42 import javax.sql.DataSource ; 43 44 import org.jboss.ejb.plugins.cmp.jdbc.WrappedStatement; 45 import org.jboss.logging.Logger; 46 47 53 public class PreparedStatementBean 54 implements EntityBean 55 { 56 57 private static final long serialVersionUID = 6204314647869034863L; 58 static Logger log = Logger.getLogger(PreparedStatementBean.class); 59 private EntityContext ctx = null; 60 private DataSource ds; 61 private String key; 62 private String name; 63 64 public void ejbActivate() 65 { 66 } 67 public void ejbPassivate() 68 { 69 } 70 71 public void ejbLoad() 72 { 73 key = (String ) ctx.getPrimaryKey(); 74 log.debug("ejbLoad("+key+")"); 75 try 76 { 77 Connection con = ds.getConnection(); 78 try 79 { 80 PreparedStatement ps = con.prepareStatement("SELECT name FROM BMPTABLE WHERE pk=?"); 81 try 82 { 83 ps.setString(1, key); 84 ResultSet rs = ps.executeQuery(); 85 86 if (rs.next() == false) 87 throw new NoSuchEntityException ("Instance " +key +" not found in database."); 88 name = rs.getString(1); 89 rs.close(); 90 } 91 finally 92 { 93 ps.close(); 94 } 95 } 96 finally 97 { 98 con.close(); 99 } 100 } 101 catch (SQLException e) 102 { 103 throw new EJBException (e); 104 } 105 } 106 public void ejbStore() 107 { 108 log.debug("ejbStore(" + key + ")"); 109 try 110 { 111 Connection con = ds.getConnection(); 112 try 113 { 114 PreparedStatement ps = con.prepareStatement("UPDATE BMPTABLE SET name=? WHERE pk=?"); 115 try 116 { 117 ps.setString(1, key); 118 ps.setString(2, name); 119 ps.executeUpdate(); 120 } 121 finally 122 { 123 ps.close(); 124 } 125 } 126 finally 127 { 128 con.close(); 129 } 130 } 131 catch (SQLException e) 132 { 133 throw new EJBException (e); 134 } 135 } 136 public void ejbRemove() 137 { 138 log.debug("ejbRemove(" + key + ") called"); 139 try 140 { 141 Connection con = ds.getConnection(); 142 try 143 { 144 PreparedStatement ps = con.prepareStatement("DELETE FROM BMPTABLE WHERE pk=?"); 145 try 146 { 147 ps.setString(1, key); 148 ps.executeUpdate(); 149 } 150 finally 151 { 152 ps.close(); 153 } 154 } 155 finally 156 { 157 con.close(); 158 } 159 } 160 catch (SQLException e) 161 { 162 throw new EJBException (e); 163 } 164 165 log.debug("Removed "+key); 166 } 167 168 public void setEntityContext(EntityContext ctx) 169 { 170 log.debug("setEntityContext() called"); 171 this.ctx = ctx; 172 try 173 { 174 InitialContext ic = new InitialContext (); 175 ds = (DataSource ) ic.lookup("java:comp/env/jdbc/DataSource"); 176 } 177 catch (NamingException e) 178 { 179 throw new EJBException ("DataSource init failed", e); 180 } 181 } 182 public void unsetEntityContext() throws EJBException , RemoteException 183 { 184 ds = null; 185 186 } 187 188 public String ejbCreate(String pk, String name) 189 throws CreateException , DuplicateKeyException 190 { 191 log.debug("entry ejbCreate("+ pk + ", " + name + ")"); 192 ensureTableExists(); 193 try 194 { 195 Connection con = ds.getConnection(); 196 try 197 { 198 PreparedStatement ps = con.prepareStatement("INSERT INTO BMPTABLE VALUES (?,?)"); 199 try 200 { 201 ps.setString(1, pk); 202 ps.setString(2, name); 203 ps.execute(); 204 } 205 finally 206 { 207 ps.close(); 208 } 209 } 210 finally 211 { 212 con.close(); 213 } 214 } 215 catch (SQLException e) 216 { 217 log.debug("failed", e); 218 219 throw new CreateException ("Entity bean creation failure: " + 220 e.getMessage()); 221 } 222 223 this.key = pk; 224 this.name = name; 225 log.debug("Created BMP: "+pk); 226 return pk; 227 } 228 229 public void ejbPostCreate(String pk, String name) 230 { 231 log.debug("entry ejbCreate("+ pk + ", " + name + ")"); 232 } 233 234 public String ejbFindByPrimaryKey(String pk) 235 throws FinderException 236 { 237 log.debug("ejbFindByPrimaryKey, pk="+pk); 238 ensureTableExists(); 239 try 240 { 241 Connection con = ds.getConnection(); 242 try 243 { 244 PreparedStatement ps; 245 246 ps = con.prepareStatement("SELECT pk FROM BMPTABLE WHERE pk=?"); 247 try 248 { 249 ps.setString(1, pk); 250 ResultSet rs = ps.executeQuery(); 251 if (!rs.next()) 252 throw new ObjectNotFoundException ("No bean with " +"pk=" +pk+ " found."); 253 rs.close(); 254 return pk; 255 } 256 finally 257 { 258 ps.close(); 259 } 260 } 261 finally 262 { 263 con.close(); 264 } 265 } 266 catch (SQLException e) 267 { 268 throw new FinderException ("Could not find pk="+pk+", msg=" + e.getMessage()); 269 } 270 } 271 272 public long hashEntityTable() 273 throws RemoteException 274 { 275 long hash = 0; 276 try 277 { 278 Connection con = ds.getConnection(); 279 try 280 { 281 PreparedStatement ps = con.prepareStatement("SELECT pk FROM BMPTABLE"); 282 try 283 { 284 ResultSet rs = ps.executeQuery(); 285 while( rs.next() ) 286 { 287 String pk = rs.getString(1); 288 PreparedStatement ps2 = con.prepareStatement("SELECT name FROM BMPTABLE WHERE pk=?"); 289 ps2.setString(1, pk); 290 ResultSet rs2 = ps2.executeQuery(); 291 if( rs2.next() ) 292 { 293 String pkName = rs2.getString(1); 294 hash += pkName.hashCode(); 295 } 296 rs2.close(); 297 ps2.close(); 298 } 299 rs.close(); 300 } 301 finally 302 { 303 ps.close(); 304 } 305 } 306 finally 307 { 308 con.close(); 309 } 310 } 311 catch (SQLException e) 312 { 313 throw new RemoteException ("Failed to calculate hash", e); 314 } 315 return hash; 316 } 317 318 public void testPreparedStatementCache() throws RemoteException 319 { 320 try 321 { 322 Connection con = ds.getConnection(); 323 try 324 { 325 PreparedStatement ps = con.prepareStatement("SELECT pk FROM BMPTABLE"); 326 Statement ps1 = getWrappedStatement(ps); 327 ps.close(); 328 ps = con.prepareStatement("SELECT pk FROM BMPTABLE"); 329 Statement ps2 = getWrappedStatement(ps); 330 if (ps1 != ps2) 331 throw new EJBException ("Statement " + ps1 + " was not cached: got " + ps2); 332 ps.close(); 333 ps = con.prepareStatement("SELECT pk FROM BMPTABLE", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 334 ps2 = getWrappedStatement(ps); 335 if (ps1 != ps2) 336 throw new EJBException ("Statement " + ps1 + " was not cached against default result set parameters: got " + ps2); 337 ps.close(); 338 ps = con.prepareStatement("SELECT pk FROM BMPTABLE", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); 339 ps2 = getWrappedStatement(ps); 340 if (ps1 == ps2) 341 throw new EJBException ("Statement " + ps1 + " should be different with different result set parameters: got " + ps2); 342 ps.close(); 343 } 344 finally 345 { 346 con.close(); 347 } 348 } 349 catch (SQLException e) 350 { 351 throw new RemoteException ("Unexpected sql exception", e); 352 } 353 } 354 355 public void testPreparedStatementCacheDoubleClose() throws RemoteException 356 { 357 try 358 { 359 Connection con = ds.getConnection(); 360 try 361 { 362 PreparedStatement ps = con.prepareStatement("SELECT pk FROM BMPTABLE"); 363 ps.close(); 364 try 365 { 366 ps.close(); 367 } 368 catch (SQLException e) 369 { 370 log.debug("Got expected double close exception", e); 371 } 372 } 373 finally 374 { 375 con.close(); 376 } 377 } 378 catch (SQLException e) 379 { 380 throw new RemoteException ("Unexpected sql exception", e); 381 } 382 } 383 384 public void testCallableStatementCache(String name) throws RemoteException 385 { 386 try 387 { 388 InitialContext ctx = new InitialContext (); 389 String query = (String ) ctx.lookup("java:comp/env/"+name); 390 391 Connection con = ds.getConnection(); 392 try 393 { 394 CallableStatement ps = con.prepareCall(query); 395 Statement ps1 = getWrappedStatement(ps); 396 ps.close(); 397 ps = con.prepareCall(query); 398 Statement ps2 = getWrappedStatement(ps); 399 if (ps1 != ps2) 400 throw new EJBException ("Statement " + ps1 + " was not cached: got " + ps2); 401 ps.close(); 402 ps = con.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 403 ps2 = getWrappedStatement(ps); 404 if (ps1 != ps2) 405 throw new EJBException ("Statement " + ps1 + " was not cached against default result set parameters: got " + ps2); 406 ps.close(); 407 ps = con.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); 408 ps2 = getWrappedStatement(ps); 409 if (ps1 == ps2) 410 throw new EJBException ("Statement " + ps1 + " should be different with different result set parameters: got " + ps2); 411 ps.close(); 412 } 413 finally 414 { 415 con.close(); 416 } 417 } 418 catch (SQLException e) 419 { 420 throw new RemoteException ("Unexpected sql exception", e); 421 } 422 catch (NamingException e) 423 { 424 throw new RemoteException ("Failed to lookup query", e); 425 } 426 } 427 428 public void testCallableStatementCacheDoubleClose(String name) throws RemoteException 429 { 430 try 431 { 432 InitialContext ctx = new InitialContext (); 433 String query = (String ) ctx.lookup("java:comp/env/"+name); 434 435 Connection con = ds.getConnection(); 436 try 437 { 438 CallableStatement ps = con.prepareCall(query); 439 ps.close(); 440 try 441 { 442 ps.close(); 443 } 444 catch (SQLException e) 445 { 446 log.debug("Got expected double close exception", e); 447 } 448 } 449 finally 450 { 451 con.close(); 452 } 453 } 454 catch (SQLException e) 455 { 456 throw new RemoteException ("Unexpected sql exception", e); 457 } 458 catch (NamingException e) 459 { 460 throw new RemoteException ("Failed to lookup query", e); 461 } 462 } 463 464 public String executeStoredProc(String name) 465 throws RemoteException 466 { 467 String value = null; 468 try 469 { 470 InitialContext ctx = new InitialContext (); 471 String query = (String ) ctx.lookup("java:comp/env/"+name); 472 473 Connection con = ds.getConnection(); 474 try 475 { 476 CallableStatement ps = con.prepareCall(query); 477 try 478 { 479 ResultSet rs = ps.executeQuery(); 480 while( rs.next() ) 481 { 482 value = rs.getString(1); 483 } 484 rs.close(); 485 } 486 finally 487 { 488 ps.close(); 489 } 490 } 491 finally 492 { 493 con.close(); 494 } 495 } 496 catch (SQLException e) 497 { 498 throw new RemoteException ("Failed to execuate CallableStatement", e); 499 } 500 catch (NamingException e) 501 { 502 throw new RemoteException ("Failed to lookup query", e); 503 } 504 return value; 505 } 506 507 private void ensureTableExists() 508 { 509 boolean exists = true; 510 511 try 512 { 513 Connection con = ds.getConnection(); 514 try 515 { 516 Statement s = con.createStatement(); 517 try 518 { 519 ResultSet rs = s.executeQuery("SELECT * FROM BMPTABLE"); 520 rs.close(); 521 } 522 finally 523 { 524 s.close(); 525 } 526 } 527 finally 528 { 529 con.close(); 530 } 531 } 532 catch (SQLException e) 533 { 534 exists = false; 535 } 536 537 if (!exists) 538 { 539 try 540 { 541 Connection con = ds.getConnection(); 542 try 543 { 544 Statement s = con.createStatement(); 545 try 546 { 547 s.executeUpdate("CREATE TABLE BMPTABLE (pk VARCHAR(16), name VARCHAR(32))"); 548 } 549 finally 550 { 551 s.close(); 552 } 553 } 554 finally 555 { 556 con.close(); 557 } 558 } 559 catch (SQLException e) 560 { 561 throw new EJBException (e); 562 } 563 } 564 } 565 566 public Statement getWrappedStatement(Statement s) throws SQLException 567 { 568 while (s instanceof WrappedStatement) 569 s = ((WrappedStatement) s).getUnderlyingStatement(); 570 return s; 571 } 572 573 } 574 | Popular Tags |