001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JdbcRA1EBRBean.java 4406 2004-03-19 11:57:20Z benoitf $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.jdbcra;
027:
028: import java.sql.Connection;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Statement;
033: import java.util.Enumeration;
034: import java.util.Vector;
035:
036: import javax.ejb.CreateException;
037: import javax.ejb.EJBException;
038: import javax.ejb.EntityBean;
039: import javax.ejb.EntityContext;
040: import javax.ejb.FinderException;
041: import javax.ejb.ObjectNotFoundException;
042: import javax.ejb.RemoveException;
043: import javax.naming.Context;
044: import javax.naming.InitialContext;
045: import javax.sql.DataSource;
046:
047: import org.objectweb.jonas.common.Log;
048: import org.objectweb.util.monolog.api.BasicLevel;
049: import org.objectweb.util.monolog.api.Logger;
050:
051: /**
052: *
053: */
054: public class JdbcRA1EBRBean implements EntityBean {
055:
056: private DataSource dataSource1 = null;
057:
058: static private Logger logger = null;
059: EntityContext ejbContext;
060:
061: // ------------------------------------------------------------------
062: // State of the bean.
063: //
064: // ------------------------------------------------------------------
065:
066: public Integer accno;
067: public String customer;
068: public double balance;
069:
070: // ------------------------------------------------------------------
071: // EntityBean implementation
072: // ------------------------------------------------------------------
073:
074: public void setEntityContext(EntityContext ctx) {
075: if (logger == null) {
076: logger = Log.getLogger("org.objectweb.jonas_tests");
077: }
078: logger.log(BasicLevel.DEBUG, "");
079: ejbContext = ctx;
080: }
081:
082: public void unsetEntityContext() {
083: logger.log(BasicLevel.DEBUG, "");
084: ejbContext = null;
085: }
086:
087: public void ejbRemove() throws RemoveException {
088: logger.log(BasicLevel.DEBUG, "");
089:
090: // Access database to remove bean in table
091:
092: Connection conn = null;
093: PreparedStatement stmt = null;
094:
095: try {
096: // get a connection for this transaction context
097:
098: conn = getConnection();
099:
100: // delete Object in DB
101:
102: stmt = conn
103: .prepareStatement("delete from jdbc_xa1 where xa_accno=?");
104: Integer pk = (Integer) ejbContext.getPrimaryKey();
105: stmt.setInt(1, pk.intValue());
106: stmt.executeUpdate();
107:
108: } catch (SQLException e) {
109: throw new RemoveException(
110: "Failed to delete bean from database " + e);
111: } finally {
112: try {
113: if (stmt != null)
114: stmt.close(); // close statement
115: if (conn != null)
116: conn.close(); // release connection
117: } catch (Exception ignore) {
118: }
119: }
120:
121: }
122:
123: public void ejbLoad() {
124: logger.log(BasicLevel.DEBUG, "");
125:
126: // Access database to load bean state from table
127:
128: Connection conn = null;
129: PreparedStatement stmt = null;
130:
131: try {
132: // get a connection for this transaction context
133:
134: conn = getConnection();
135:
136: // find account in DB
137:
138: stmt = conn
139: .prepareStatement("select xa_customer,xa_balance from jdbc_xa1 where xa_accno=?");
140: Integer pk = (Integer) ejbContext.getPrimaryKey();
141: stmt.setInt(1, pk.intValue());
142: ResultSet rs = stmt.executeQuery();
143:
144: if (rs.next() == false) {
145: throw new EJBException(
146: "Failed to load bean from database");
147: }
148:
149: // update object state
150:
151: accno = pk;
152: customer = rs.getString("xa_customer");
153: balance = rs.getDouble("xa_balance");
154:
155: } catch (SQLException e) {
156: throw new EJBException("Failed to load bean from database "
157: + e);
158: } finally {
159: try {
160: if (stmt != null)
161: stmt.close(); // close statement
162: if (conn != null)
163: conn.close(); // release connection
164: } catch (Exception ignore) {
165: }
166: }
167: }
168:
169: public void ejbStore() {
170: logger.log(BasicLevel.DEBUG, "");
171:
172: // Access database to store bean state in table
173:
174: Connection conn = null;
175: PreparedStatement stmt = null;
176:
177: try {
178: // get a connection for this transaction context
179:
180: conn = getConnection();
181:
182: // store Object state in DB
183:
184: stmt = conn
185: .prepareStatement("update jdbc_xa1 set xa_customer=?,xa_balance=? where xa_accno=?");
186: stmt.setString(1, customer);
187: stmt.setDouble(2, balance);
188: Integer pk = (Integer) ejbContext.getPrimaryKey();
189: stmt.setInt(3, pk.intValue());
190: stmt.executeUpdate();
191:
192: } catch (SQLException e) {
193: throw new EJBException("Failed to store bean to database "
194: + e);
195: } finally {
196: try {
197: if (stmt != null)
198: stmt.close(); // close statement
199: if (conn != null)
200: conn.close(); // release connection
201: } catch (Exception ignore) {
202: }
203: }
204: }
205:
206: public void ejbPassivate() {
207: logger.log(BasicLevel.DEBUG, "");
208: }
209:
210: public void ejbActivate() {
211: logger.log(BasicLevel.DEBUG, "");
212: }
213:
214: public void ejbPostCreate(int val_accno, String val_customer,
215: double val_balance) {
216: logger.log(BasicLevel.DEBUG, "");
217: }
218:
219: public void ejbPostCreate() {
220: logger.log(BasicLevel.DEBUG, "");
221: }
222:
223: public java.lang.Integer ejbCreate(int val_accno,
224: String val_customer, double val_balance)
225: throws CreateException {
226:
227: logger.log(BasicLevel.DEBUG, "");
228:
229: // Init here the bean fields
230: // Init object state
231:
232: accno = new Integer(val_accno);
233: customer = val_customer;
234: balance = val_balance;
235:
236: Connection conn = null;
237: PreparedStatement stmt = null;
238:
239: try {
240: // get a connection for this transaction context
241:
242: conn = getConnection();
243:
244: // create object in DB
245:
246: stmt = conn
247: .prepareStatement("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (?, ?, ?)");
248: stmt.setInt(1, accno.intValue());
249: stmt.setString(2, customer);
250: stmt.setDouble(3, balance);
251: stmt.executeUpdate();
252:
253: } catch (SQLException e) {
254: throw new CreateException(
255: "Failed to create bean in database: " + e);
256: } finally {
257: try {
258: if (stmt != null)
259: stmt.close(); // close statement
260: if (conn != null)
261: conn.close(); // release connection
262: } catch (Exception ignore) {
263: }
264: }
265:
266: // Return the primary key
267: return accno;
268:
269: }
270:
271: public java.lang.Integer ejbFindByPrimaryKey(Integer pk)
272: throws ObjectNotFoundException, FinderException {
273: logger.log(BasicLevel.DEBUG, "");
274:
275: // Access database to find entry in table
276:
277: Connection conn = null;
278: PreparedStatement stmt = null;
279:
280: try {
281: // get a connection for this transaction context
282:
283: conn = getConnection();
284:
285: // lookup for this primary key in DB
286: stmt = conn
287: .prepareStatement("select xa_accno from jdbc_xa1 where xa_accno=?");
288: stmt.setInt(1, pk.intValue());
289: ResultSet rs = stmt.executeQuery();
290:
291: if (rs.next() == false) {
292: throw new javax.ejb.ObjectNotFoundException();
293: }
294:
295: } catch (SQLException e) {
296: throw new javax.ejb.FinderException(
297: "Failed to executeQuery " + e);
298: } finally {
299: try {
300: if (stmt != null)
301: stmt.close(); // close statement
302: if (conn != null)
303: conn.close(); // release connection
304: } catch (Exception ignore) {
305: }
306: }
307:
308: return pk;
309: }
310:
311: /**
312: * Find Account by its account number
313: *
314: * @return pk The primary key
315: *
316: * @exception FinderException - Failed to execute the query.
317: * @exception ObjectNotFoundException - Object not found for this account number
318: */
319:
320: public Integer ejbFindByNumber(int accno)
321: throws ObjectNotFoundException, FinderException {
322:
323: Connection conn = null;
324: PreparedStatement stmt = null;
325:
326: try {
327: // get a connection for this transaction context
328:
329: conn = getConnection();
330:
331: // lookup for this primary key in DB
332:
333: stmt = conn
334: .prepareStatement("select xa_accno from jdbc_xa1 where xa_accno=?");
335: stmt.setInt(1, accno);
336: ResultSet rs = stmt.executeQuery();
337:
338: if (rs.next() == false) {
339: throw new javax.ejb.ObjectNotFoundException();
340: }
341:
342: } catch (SQLException e) {
343: throw new javax.ejb.FinderException(
344: "Failed to executeQuery " + e);
345: } finally {
346: try {
347: if (stmt != null)
348: stmt.close(); // close statement
349: if (conn != null)
350: conn.close(); // release connection
351: } catch (Exception ignore) {
352: }
353: }
354:
355: // return a primary key for this account
356: return new Integer(accno);
357: }
358:
359: /**
360: * Creates an enumeration of primary keys for all accounts
361: *
362: * @return pkv The primary keys
363: *
364: * @exception FinderException - Failed to execute the query.
365: */
366:
367: public Enumeration ejbFindAllAccounts() throws FinderException {
368:
369: Connection conn = null;
370: PreparedStatement stmt = null;
371:
372: Vector pkv = new Vector();
373:
374: try {
375: // get a connection for this transaction context
376:
377: conn = getConnection();
378:
379: // Lookup for all accounts in DB
380:
381: stmt = conn
382: .prepareStatement("select xa_accno from jdbc_xa1");
383: ResultSet rs = stmt.executeQuery();
384:
385: // Build the vector of primary keys
386:
387: while (rs.next()) {
388: Integer pk = new Integer(rs.getInt("xa_accno"));
389: pkv.addElement((Object) pk);
390: }
391: ;
392:
393: } catch (SQLException e) {
394: e.printStackTrace();
395: throw new javax.ejb.FinderException(
396: "Failed to executeQuery " + e);
397: } finally {
398: try {
399: if (stmt != null)
400: stmt.close(); // close statement
401: if (conn != null)
402: conn.close(); // release connection
403: } catch (Exception ignore) {
404: }
405: }
406:
407: // return primary keys
408: return pkv.elements();
409: }
410:
411: public java.lang.Integer ejbCreate() throws CreateException {
412:
413: logger.log(BasicLevel.DEBUG, "");
414:
415: // Init object state
416: Integer tempint = new Integer(0);
417: Connection conn = null;
418: Statement stmt = null;
419:
420: try {
421: // get a connection for this transaction context
422:
423: conn = getConnection();
424:
425: // create object in DB
426:
427: stmt = conn.createStatement();
428: stmt
429: .addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (201, 'Albert Smith', 500)");
430: stmt
431: .addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (202, 'Bob Smith', 500)");
432: stmt
433: .addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (203, 'Carl Smith', 500)");
434: stmt
435: .addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (204, 'David Smith', 500)");
436: stmt
437: .addBatch("insert into jdbc_xa1 (xa_accno, xa_customer, xa_balance) values (205, 'Edward Smith', 500)");
438: int[] upCounts = stmt.executeBatch();
439:
440: } catch (SQLException e) {
441: throw new CreateException(
442: "Failed to create bean in database: " + e);
443: } finally {
444: try {
445: if (stmt != null)
446: stmt.close(); // close statement
447: if (conn != null)
448: conn.close(); // release connection
449: } catch (Exception ignore) {
450: }
451: }
452: return tempint;
453: }
454:
455: /**
456: * @return the connection from the dataSource
457: * @exception EJBException Thrown by the method if the dataSource is not found
458: * in the naming.
459: * @exception SQLException may be thrown by dataSource.getConnection()
460: */
461:
462: private Connection getConnection() throws EJBException,
463: SQLException {
464:
465: Connection myconn1 = null;
466:
467: if (dataSource1 == null) {
468:
469: // Finds DataSource from JNDI
470:
471: Context initialContext = null;
472:
473: try {
474: initialContext = new InitialContext();
475: dataSource1 = (DataSource) initialContext
476: .lookup("java:comp/env/jdbc/JdbcRA1Ds");
477: } catch (Exception e) {
478: throw new javax.ejb.EJBException(
479: "Cannot lookup dataSource1 " + e);
480: }
481: }
482:
483: try {
484: myconn1 = dataSource1.getConnection();
485: } catch (Exception e) {
486: throw new javax.ejb.EJBException(
487: "Cannot getConnection dataSource1 " + e);
488: }
489:
490: return myconn1;
491: }
492:
493: /*========================= Account implementation ============================*/
494:
495: /**
496: * Business method for returning the balance.
497: *
498: * @return balance
499: *
500: */
501:
502: public double getBalance() {
503: return balance;
504: }
505:
506: /**
507: * Business method for updating the balance.
508: *
509: * @param d balance to update
510: *
511: */
512:
513: public void setBalance(double d) {
514: balance = balance + d;
515: }
516:
517: /**
518: * Business method for returning the customer.
519: *
520: * @return customer
521: *
522: */
523:
524: public String getCustomer() {
525: return customer;
526: }
527:
528: /**
529: * Business method for changing the customer name.
530: *
531: * @param c customer to update
532: *
533: */
534:
535: public void setCustomer(String c) {
536: customer = c;
537: }
538:
539: /**
540: * Business method to get the Account number
541: */
542:
543: public int getNumber() {
544: return accno.intValue();
545: }
546:
547: }
|