1 25 26 package org.objectweb.jonas.jtests.beans.jdbcra; 27 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.sql.Statement ; 33 import java.util.Enumeration ; 34 import java.util.Vector ; 35 36 import javax.ejb.CreateException ; 37 import javax.ejb.EJBException ; 38 import javax.ejb.EntityBean ; 39 import javax.ejb.EntityContext ; 40 import javax.ejb.FinderException ; 41 import javax.ejb.ObjectNotFoundException ; 42 import javax.ejb.RemoveException ; 43 import javax.naming.Context ; 44 import javax.naming.InitialContext ; 45 import javax.sql.DataSource ; 46 47 import org.objectweb.jonas.common.Log; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 import org.objectweb.util.monolog.api.Logger; 50 51 54 55 public class JdbcRA2EBRBean implements EntityBean { 56 57 private DataSource dataSource2 = null; 58 59 static private Logger logger = null; 60 EntityContext ejbContext; 61 62 67 public Integer accno; 68 public String customer; 69 public double balance; 70 71 75 public void setEntityContext(EntityContext ctx) { 76 77 if (logger == null) { 78 logger = Log.getLogger("org.objectweb.jonas_tests"); 79 } 80 logger.log(BasicLevel.DEBUG, ""); 81 ejbContext = ctx; 82 } 83 84 public void unsetEntityContext() { 85 logger.log(BasicLevel.DEBUG, ""); 86 ejbContext = null; 87 } 88 89 public void ejbRemove() throws RemoveException { 90 logger.log(BasicLevel.DEBUG, ""); 91 92 94 Connection conn = null; 95 PreparedStatement stmt = null; 96 97 try { 98 100 conn = getConnection(); 101 102 104 stmt = conn.prepareStatement("delete from jdbc_xa2 where xa_accno=?"); 105 Integer pk = (Integer ) ejbContext.getPrimaryKey(); 106 stmt.setInt(1, pk.intValue()); 107 stmt.executeUpdate(); 108 } catch (SQLException e) { 109 throw new RemoveException ("Failed to delete bean from database"+e); 110 } finally { 111 try { 112 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 115 } 116 } 117 } 118 119 public void ejbLoad() { 120 logger.log(BasicLevel.DEBUG, ""); 121 122 124 Connection conn = null; 125 PreparedStatement stmt = null; 126 127 try { 128 130 conn = getConnection(); 131 132 134 stmt = conn.prepareStatement("select xa_customer,xa_balance from jdbc_xa2 where xa_accno=?"); 135 Integer pk = (Integer ) ejbContext.getPrimaryKey(); 136 stmt.setInt(1, pk.intValue()); 137 ResultSet rs = stmt.executeQuery(); 138 139 if (rs.next() == false) { 140 throw new EJBException ("Failed to load bean from database"); 141 } 142 143 145 accno = pk; 146 customer = rs.getString("xa_customer"); 147 balance = rs.getDouble("xa_balance"); 148 149 } catch (SQLException e) { 150 throw new EJBException ("Failed to load bean from database "+e); 151 } finally { 152 try { 153 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 156 } 157 } 158 159 } 160 161 public void ejbStore() { 162 logger.log(BasicLevel.DEBUG, ""); 163 164 166 Connection conn = null; 167 PreparedStatement stmt = null; 168 169 try { 170 172 conn = getConnection(); 173 174 176 stmt = conn.prepareStatement("update jdbc_xa2 set xa_customer=?,xa_balance=? where xa_accno=?"); 177 stmt.setString(1, customer); 178 stmt.setDouble(2, balance); 179 Integer pk = (Integer ) ejbContext.getPrimaryKey(); 180 stmt.setInt(3, pk.intValue()); 181 stmt.executeUpdate(); 182 183 } catch (SQLException e) { 184 throw new EJBException ("Failed to store bean to database "+e); 185 } finally { 186 try { 187 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 190 } 191 } 192 193 } 194 195 public void ejbPassivate() { 196 logger.log(BasicLevel.DEBUG, ""); 197 } 198 199 public void ejbActivate() { 200 logger.log(BasicLevel.DEBUG, ""); 201 } 202 203 public void ejbPostCreate(int val_accno, String val_customer, double val_balance){ 204 logger.log(BasicLevel.DEBUG, ""); 205 } 206 207 public void ejbPostCreate(){ 208 logger.log(BasicLevel.DEBUG, ""); 209 } 210 211 public java.lang.Integer ejbCreate(int val_accno, String val_customer, double val_balance) 212 throws CreateException { 213 214 logger.log(BasicLevel.DEBUG, ""); 215 216 accno = new Integer (val_accno); 219 customer = val_customer; 220 balance = val_balance; 221 222 Connection conn = null; 223 224 PreparedStatement stmt = null; 225 226 try { 227 229 conn = getConnection(); 230 231 233 stmt = conn.prepareStatement("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (?, ?, ?)"); 234 stmt.setInt(1, accno.intValue()); 235 stmt.setString(2, customer); 236 stmt.setDouble(3, balance); 237 stmt.executeUpdate(); 238 } catch (SQLException e) { 239 throw new CreateException ("Failed to create bean in database: "+e); 240 } finally { 241 try { 242 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 245 } 246 } 247 248 250 return accno; 251 252 } 253 254 public java.lang.Integer ejbFindByPrimaryKey(Integer pk) 255 throws ObjectNotFoundException , FinderException { 256 logger.log(BasicLevel.DEBUG, ""); 257 258 260 Connection conn = null; 261 PreparedStatement stmt = null; 262 263 try { 264 266 conn = getConnection(); 267 268 270 stmt = conn.prepareStatement("select xa_accno from jdbc_xa2 where xa_accno=?"); 271 stmt.setInt(1, pk.intValue()); 272 ResultSet rs = stmt.executeQuery(); 273 274 if (rs.next() == false) { 275 throw new javax.ejb.ObjectNotFoundException (); 276 } 277 278 } catch (SQLException e) { 279 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 280 } finally { 281 try { 282 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 285 } 286 } 287 288 return pk; 289 } 290 291 292 300 301 public Integer ejbFindByNumber(int accno) 302 throws ObjectNotFoundException , FinderException { 303 304 Connection conn = null; 305 PreparedStatement stmt = null; 306 307 try { 308 310 conn = getConnection(); 311 312 314 stmt = conn.prepareStatement("select xa_accno from jdbc_xa2 where xa_accno=?"); 315 stmt.setInt(1, accno); 316 ResultSet rs = stmt.executeQuery(); 317 318 if (rs.next() == false) { 319 throw new javax.ejb.ObjectNotFoundException (); 320 } 321 322 } catch (SQLException e) { 323 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 324 } finally { 325 try { 326 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 329 } 330 } 331 332 334 return new Integer (accno); 335 } 336 337 344 345 public Enumeration ejbFindAllAccounts() throws FinderException { 346 347 Connection conn = null; 348 PreparedStatement stmt = null; 349 350 Vector pkv = new Vector (); 351 352 try { 353 355 conn = getConnection(); 356 357 359 stmt = conn.prepareStatement("select xa_accno from jdbc_xa2"); 360 ResultSet rs = stmt.executeQuery(); 361 362 364 while (rs.next()) { 365 Integer pk = new Integer (rs.getInt("xa_accno")); 366 pkv.addElement((Object )pk); 367 }; 368 369 } catch (SQLException e) { 370 throw new javax.ejb.FinderException ("Failed to executeQuery " +e); 371 } finally { 372 try { 373 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 376 } 377 } 378 379 return pkv.elements(); 381 } 382 383 public java.lang.Integer ejbCreate() 384 throws CreateException { 385 386 logger.log(BasicLevel.DEBUG, ""); 387 388 390 Connection conn = null; 391 Statement stmt = null; 392 Integer tempint = new Integer (0); 393 394 try { 395 397 conn = getConnection(); 398 399 401 stmt = conn.createStatement(); 402 stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (201, 'Albert Smith', 500)"); 403 stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (202, 'Bob Smith', 500)"); 404 stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (203, 'Carl Smith', 500)"); 405 stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (204, 'David Smith', 500)"); 406 stmt.addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (205, 'Edward Smith', 500)"); 407 int[] upCounts = stmt.executeBatch(); 408 409 } catch (SQLException e) { 410 throw new CreateException ("Failed to create bean in database: "+e); 411 } finally { 412 try { 413 if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { 416 } 417 } 418 return tempint; 419 } 420 421 427 428 private Connection getConnection() throws EJBException , SQLException { 429 430 Connection myconn2 = null; 431 432 if (dataSource2 == null) { 433 434 436 Context initialContext = null; 437 438 try { 439 initialContext = new InitialContext (); 440 dataSource2 = (DataSource )initialContext.lookup("java:comp/env/jdbc/JdbcRA2Ds"); 441 } catch (Exception e) { 442 throw new javax.ejb.EJBException ("Cannot lookup dataSource2 "+e); 443 } 444 } 445 446 try { 447 myconn2 = dataSource2.getConnection(); 448 } catch (Exception e) { 449 throw new javax.ejb.EJBException ("Cannot getConnection dataSource2 "+e); 450 } 451 452 return myconn2; 453 } 454 455 456 457 463 464 public double getBalance(){ 465 return balance; 466 } 467 468 474 475 public void setBalance(double d){ 476 balance = balance + d; 477 } 478 479 485 486 public String getCustomer(){ 487 return customer; 488 } 489 490 496 497 public void setCustomer(String c) { 498 customer = c; 499 } 500 501 504 505 public int getNumber() { 506 return accno.intValue(); 507 } 508 509 } 510 511 512 513 | Popular Tags |