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: JdbcRA2EBRBean.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:
055: public class JdbcRA2EBRBean implements EntityBean {
056:
057: private DataSource dataSource2 = null;
058:
059: static private Logger logger = null;
060: EntityContext ejbContext;
061:
062: // ------------------------------------------------------------------
063: // State of the bean.
064: //
065: // ------------------------------------------------------------------
066:
067: public Integer accno;
068: public String customer;
069: public double balance;
070:
071: // ------------------------------------------------------------------
072: // EntityBean implementation
073: // ------------------------------------------------------------------
074:
075: public void setEntityContext(EntityContext ctx) {
076:
077: if (logger == null) {
078: logger = Log.getLogger("org.objectweb.jonas_tests");
079: }
080: logger.log(BasicLevel.DEBUG, "");
081: ejbContext = ctx;
082: }
083:
084: public void unsetEntityContext() {
085: logger.log(BasicLevel.DEBUG, "");
086: ejbContext = null;
087: }
088:
089: public void ejbRemove() throws RemoveException {
090: logger.log(BasicLevel.DEBUG, "");
091:
092: // Access database to remove bean in table
093:
094: Connection conn = null;
095: PreparedStatement stmt = null;
096:
097: try {
098: // get a connection for this transaction context
099:
100: conn = getConnection();
101:
102: // delete Object in DB
103:
104: stmt = conn
105: .prepareStatement("delete from jdbc_xa2 where xa_accno=?");
106: Integer pk = (Integer) ejbContext.getPrimaryKey();
107: stmt.setInt(1, pk.intValue());
108: stmt.executeUpdate();
109: } catch (SQLException e) {
110: throw new RemoveException(
111: "Failed to delete bean from database" + e);
112: } finally {
113: try {
114: if (stmt != null)
115: stmt.close(); // close statement
116: if (conn != null)
117: conn.close(); // release connection
118: } catch (Exception ignore) {
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_xa2 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:
170: public void ejbStore() {
171: logger.log(BasicLevel.DEBUG, "");
172:
173: // Access database to store bean state in table
174:
175: Connection conn = null;
176: PreparedStatement stmt = null;
177:
178: try {
179: // get a connection for this transaction context
180:
181: conn = getConnection();
182:
183: // store Object state in DB
184:
185: stmt = conn
186: .prepareStatement("update jdbc_xa2 set xa_customer=?,xa_balance=? where xa_accno=?");
187: stmt.setString(1, customer);
188: stmt.setDouble(2, balance);
189: Integer pk = (Integer) ejbContext.getPrimaryKey();
190: stmt.setInt(3, pk.intValue());
191: stmt.executeUpdate();
192:
193: } catch (SQLException e) {
194: throw new EJBException("Failed to store bean to database "
195: + e);
196: } finally {
197: try {
198: if (stmt != null)
199: stmt.close(); // close statement
200: if (conn != null)
201: conn.close(); // release connection
202: } catch (Exception ignore) {
203: }
204: }
205:
206: }
207:
208: public void ejbPassivate() {
209: logger.log(BasicLevel.DEBUG, "");
210: }
211:
212: public void ejbActivate() {
213: logger.log(BasicLevel.DEBUG, "");
214: }
215:
216: public void ejbPostCreate(int val_accno, String val_customer,
217: double val_balance) {
218: logger.log(BasicLevel.DEBUG, "");
219: }
220:
221: public void ejbPostCreate() {
222: logger.log(BasicLevel.DEBUG, "");
223: }
224:
225: public java.lang.Integer ejbCreate(int val_accno,
226: String val_customer, double val_balance)
227: throws CreateException {
228:
229: logger.log(BasicLevel.DEBUG, "");
230:
231: // Init here the bean fields
232: // Init object state
233: accno = new Integer(val_accno);
234: customer = val_customer;
235: balance = val_balance;
236:
237: Connection conn = null;
238:
239: PreparedStatement stmt = null;
240:
241: try {
242: // get a connection for this transaction context
243:
244: conn = getConnection();
245:
246: // create object in DB
247:
248: stmt = conn
249: .prepareStatement("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (?, ?, ?)");
250: stmt.setInt(1, accno.intValue());
251: stmt.setString(2, customer);
252: stmt.setDouble(3, balance);
253: stmt.executeUpdate();
254: } catch (SQLException e) {
255: throw new CreateException(
256: "Failed to create bean in database: " + e);
257: } finally {
258: try {
259: if (stmt != null)
260: stmt.close(); // close statement
261: if (conn != null)
262: conn.close(); // release connection
263: } catch (Exception ignore) {
264: }
265: }
266:
267: // Return the primary key
268:
269: return accno;
270:
271: }
272:
273: public java.lang.Integer ejbFindByPrimaryKey(Integer pk)
274: throws ObjectNotFoundException, FinderException {
275: logger.log(BasicLevel.DEBUG, "");
276:
277: // Access database to find entry in table
278:
279: Connection conn = null;
280: PreparedStatement stmt = null;
281:
282: try {
283: // get a connection for this transaction context
284:
285: conn = getConnection();
286:
287: // lookup for this primary key in DB
288:
289: stmt = conn
290: .prepareStatement("select xa_accno from jdbc_xa2 where xa_accno=?");
291: stmt.setInt(1, pk.intValue());
292: ResultSet rs = stmt.executeQuery();
293:
294: if (rs.next() == false) {
295: throw new javax.ejb.ObjectNotFoundException();
296: }
297:
298: } catch (SQLException e) {
299: throw new javax.ejb.FinderException(
300: "Failed to executeQuery " + e);
301: } finally {
302: try {
303: if (stmt != null)
304: stmt.close(); // close statement
305: if (conn != null)
306: conn.close(); // release connection
307: } catch (Exception ignore) {
308: }
309: }
310:
311: return pk;
312: }
313:
314: /**
315: * Find Account by its account number
316: *
317: * @return pk The primary key
318: *
319: * @exception FinderException - Failed to execute the query.
320: * @exception ObjectNotFoundException - Object not found for this account number
321: */
322:
323: public Integer ejbFindByNumber(int accno)
324: throws ObjectNotFoundException, FinderException {
325:
326: Connection conn = null;
327: PreparedStatement stmt = null;
328:
329: try {
330: // get a connection for this transaction context
331:
332: conn = getConnection();
333:
334: // lookup for this primary key in DB
335:
336: stmt = conn
337: .prepareStatement("select xa_accno from jdbc_xa2 where xa_accno=?");
338: stmt.setInt(1, accno);
339: ResultSet rs = stmt.executeQuery();
340:
341: if (rs.next() == false) {
342: throw new javax.ejb.ObjectNotFoundException();
343: }
344:
345: } catch (SQLException e) {
346: throw new javax.ejb.FinderException(
347: "Failed to executeQuery " + e);
348: } finally {
349: try {
350: if (stmt != null)
351: stmt.close(); // close statement
352: if (conn != null)
353: conn.close(); // release connection
354: } catch (Exception ignore) {
355: }
356: }
357:
358: // return a primary key for this account
359:
360: return new Integer(accno);
361: }
362:
363: /**
364: * Creates an enumeration of primary keys for all accounts
365: *
366: * @return pkv The primary keys
367: *
368: * @exception FinderException - Failed to execute the query.
369: */
370:
371: public Enumeration ejbFindAllAccounts() throws FinderException {
372:
373: Connection conn = null;
374: PreparedStatement stmt = null;
375:
376: Vector pkv = new Vector();
377:
378: try {
379: // get a connection for this transaction context
380:
381: conn = getConnection();
382:
383: // Lookup for all accounts in DB
384:
385: stmt = conn
386: .prepareStatement("select xa_accno from jdbc_xa2");
387: ResultSet rs = stmt.executeQuery();
388:
389: // Build the vector of primary keys
390:
391: while (rs.next()) {
392: Integer pk = new Integer(rs.getInt("xa_accno"));
393: pkv.addElement((Object) pk);
394: }
395: ;
396:
397: } catch (SQLException e) {
398: throw new javax.ejb.FinderException(
399: "Failed to executeQuery " + e);
400: } finally {
401: try {
402: if (stmt != null)
403: stmt.close(); // close statement
404: if (conn != null)
405: conn.close(); // release connection
406: } catch (Exception ignore) {
407: }
408: }
409:
410: // return primary keys
411: return pkv.elements();
412: }
413:
414: public java.lang.Integer ejbCreate() throws CreateException {
415:
416: logger.log(BasicLevel.DEBUG, "");
417:
418: // Init object state
419:
420: Connection conn = null;
421: Statement stmt = null;
422: Integer tempint = new Integer(0);
423:
424: try {
425: // get a connection for this transaction context
426:
427: conn = getConnection();
428:
429: // create object in DB
430:
431: stmt = conn.createStatement();
432: stmt
433: .addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (201, 'Albert Smith', 500)");
434: stmt
435: .addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (202, 'Bob Smith', 500)");
436: stmt
437: .addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (203, 'Carl Smith', 500)");
438: stmt
439: .addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (204, 'David Smith', 500)");
440: stmt
441: .addBatch("insert into jdbc_xa2 (xa_accno, xa_customer, xa_balance) values (205, 'Edward Smith', 500)");
442: int[] upCounts = stmt.executeBatch();
443:
444: } catch (SQLException e) {
445: throw new CreateException(
446: "Failed to create bean in database: " + e);
447: } finally {
448: try {
449: if (stmt != null)
450: stmt.close(); // close statement
451: if (conn != null)
452: conn.close(); // release connection
453: } catch (Exception ignore) {
454: }
455: }
456: return tempint;
457: }
458:
459: /**
460: * @return the connection from the dataSource
461: * @exception EJBException Thrown by the method if the dataSource is not found
462: * in the naming.
463: * @exception SQLException may be thrown by dataSource.getConnection()
464: */
465:
466: private Connection getConnection() throws EJBException,
467: SQLException {
468:
469: Connection myconn2 = null;
470:
471: if (dataSource2 == null) {
472:
473: // Finds DataSource from JNDI
474:
475: Context initialContext = null;
476:
477: try {
478: initialContext = new InitialContext();
479: dataSource2 = (DataSource) initialContext
480: .lookup("java:comp/env/jdbc/JdbcRA2Ds");
481: } catch (Exception e) {
482: throw new javax.ejb.EJBException(
483: "Cannot lookup dataSource2 " + e);
484: }
485: }
486:
487: try {
488: myconn2 = dataSource2.getConnection();
489: } catch (Exception e) {
490: throw new javax.ejb.EJBException(
491: "Cannot getConnection dataSource2 " + e);
492: }
493:
494: return myconn2;
495: }
496:
497: /*========================= Account implementation ============================*/
498:
499: /**
500: * Business method for returning the balance.
501: *
502: * @return balance
503: *
504: */
505:
506: public double getBalance() {
507: return balance;
508: }
509:
510: /**
511: * Business method for updating the balance.
512: *
513: * @param d balance to update
514: *
515: */
516:
517: public void setBalance(double d) {
518: balance = balance + d;
519: }
520:
521: /**
522: * Business method for returning the customer.
523: *
524: * @return customer
525: *
526: */
527:
528: public String getCustomer() {
529: return customer;
530: }
531:
532: /**
533: * Business method for changing the customer name.
534: *
535: * @param c customer to update
536: *
537: */
538:
539: public void setCustomer(String c) {
540: customer = c;
541: }
542:
543: /**
544: * Business method to get the Account number
545: */
546:
547: public int getNumber() {
548: return accno.intValue();
549: }
550:
551: }
|