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: SimpleEB.java 4406 2004-03-19 11:57:20Z benoitf $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.transacted;
027:
028: import java.sql.Connection;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.Types;
032:
033: import javax.ejb.CreateException;
034: import javax.ejb.EJBException;
035: import javax.ejb.EntityBean;
036: import javax.ejb.EntityContext;
037: import javax.ejb.FinderException;
038: import javax.ejb.ObjectNotFoundException;
039: import javax.ejb.RemoveException;
040: import javax.naming.Context;
041: import javax.naming.InitialContext;
042: import javax.sql.DataSource;
043:
044: /**
045: * Entity bean with bean managed persistence.
046: *
047: */
048: public class SimpleEB extends SimpleEC implements EntityBean {
049:
050: // Database related information
051: private DataSource dataSource = null;
052: static final String tableName = "transactedSimpleEB";
053:
054: /*
055: * ejbCreate(.....) method
056: */
057: public java.lang.String ejbCreate()
058: throws javax.ejb.CreateException {
059:
060: super .ejbCreate();
061:
062: java.lang.String pk;
063: // Initialize the pk with the primkey-field
064: pk = this .accno;
065:
066: Connection conn = null;
067: PreparedStatement pStmt = null;
068: try {
069: conn = getConnection();
070: pStmt = conn
071: .prepareStatement("insert into "
072: + tableName
073: + " (c_accno, c_customer, c_balance) values (?, ?, ?)");
074: if (this .accno == null) {
075: pStmt.setNull(1, Types.VARCHAR);
076: } else {
077: pStmt.setString(1, this .accno);
078: }
079: if (this .customer == null) {
080: pStmt.setNull(2, Types.VARCHAR);
081: } else {
082: pStmt.setString(2, this .customer);
083: }
084: pStmt.setLong(3, this .balance);
085: pStmt.executeUpdate();
086: } catch (Exception e) {
087: throw new CreateException(e.toString());
088: } finally {
089: if (pStmt != null) {
090: try {
091: pStmt.close();
092: } catch (Exception ignore) {
093: // just ignore
094: }
095: }
096: if (conn != null) {
097: try {
098: conn.close();
099: } catch (Exception ignore) {
100: // just ignore
101: }
102: }
103: }
104:
105: return pk;
106:
107: }
108:
109: /*
110: * ejbCreate(.....) method
111: */
112: public java.lang.String ejbCreate(int p1)
113: throws javax.ejb.CreateException {
114:
115: // Container-managed fields must be set to the Java language default values
116: this .accno = null;
117: this .customer = null;
118: this .balance = 0L;
119:
120: super .ejbCreate(p1);
121:
122: java.lang.String pk;
123: // Initialize the pk with the primkey-field
124: pk = this .accno;
125:
126: Connection conn = null;
127: PreparedStatement pStmt = null;
128: try {
129: conn = getConnection();
130: pStmt = conn
131: .prepareStatement("insert into "
132: + tableName
133: + " (c_accno, c_customer, c_balance) values (?, ?, ?)");
134: if (this .accno == null) {
135: pStmt.setNull(1, Types.VARCHAR);
136: } else {
137: pStmt.setString(1, this .accno);
138: }
139: if (this .customer == null) {
140: pStmt.setNull(2, Types.VARCHAR);
141: } else {
142: pStmt.setString(2, this .customer);
143: }
144: pStmt.setLong(3, this .balance);
145: pStmt.executeUpdate();
146: } catch (Exception e) {
147: throw new CreateException(e.toString());
148: } finally {
149: if (pStmt != null) {
150: try {
151: pStmt.close();
152: } catch (Exception ignore) {
153: // just ignore
154: }
155: }
156: if (conn != null) {
157: try {
158: conn.close();
159: } catch (Exception ignore) {
160: // just ignore
161: }
162: }
163: }
164:
165: return pk;
166:
167: }
168:
169: /*
170: * ejbCreate(.....) method
171: */
172: public java.lang.String ejbCreateForMandatory(char p1)
173: throws javax.ejb.CreateException {
174:
175: // Container-managed fields must be set to the Java language default values
176: this .accno = null;
177: this .customer = null;
178: this .balance = 0L;
179:
180: super .ejbCreateForMandatory(p1);
181:
182: java.lang.String pk;
183: // Initialize the pk with the primkey-field
184: pk = this .accno;
185:
186: Connection conn = null;
187: PreparedStatement pStmt = null;
188: try {
189: conn = getConnection();
190: pStmt = conn
191: .prepareStatement("insert into "
192: + tableName
193: + " (c_accno, c_customer, c_balance) values (?, ?, ?)");
194: if (this .accno == null) {
195: pStmt.setNull(1, Types.VARCHAR);
196: } else {
197: pStmt.setString(1, this .accno);
198: }
199: if (this .customer == null) {
200: pStmt.setNull(2, Types.VARCHAR);
201: } else {
202: pStmt.setString(2, this .customer);
203: }
204: pStmt.setLong(3, this .balance);
205: pStmt.executeUpdate();
206: } catch (Exception e) {
207: throw new CreateException(e.toString());
208: } finally {
209: if (pStmt != null) {
210: try {
211: pStmt.close();
212: } catch (Exception ignore) {
213: // just ignore
214: }
215: }
216: if (conn != null) {
217: try {
218: conn.close();
219: } catch (Exception ignore) {
220: // just ignore
221: }
222: }
223: }
224:
225: return pk;
226:
227: }
228:
229: /*
230: * ejbCreate(.....) method
231: */
232: public java.lang.String ejbCreateForNever(short p1)
233: throws javax.ejb.CreateException {
234:
235: // Container-managed fields must be set to the Java language default values
236: this .accno = null;
237: this .customer = null;
238: this .balance = 0L;
239:
240: super .ejbCreateForNever(p1);
241:
242: java.lang.String pk;
243: // Initialize the pk with the primkey-field
244: pk = this .accno;
245:
246: Connection conn = null;
247: PreparedStatement pStmt = null;
248: try {
249: conn = getConnection();
250: pStmt = conn
251: .prepareStatement("insert into "
252: + tableName
253: + " (c_accno, c_customer, c_balance) values (?, ?, ?)");
254: if (this .accno == null) {
255: pStmt.setNull(1, Types.VARCHAR);
256: } else {
257: pStmt.setString(1, this .accno);
258: }
259: if (this .customer == null) {
260: pStmt.setNull(2, Types.VARCHAR);
261: } else {
262: pStmt.setString(2, this .customer);
263: }
264: pStmt.setLong(3, this .balance);
265: pStmt.executeUpdate();
266: } catch (Exception e) {
267: throw new CreateException(e.toString());
268: } finally {
269: if (pStmt != null) {
270: try {
271: pStmt.close();
272: } catch (Exception ignore) {
273: // just ignore
274: }
275: }
276: if (conn != null) {
277: try {
278: conn.close();
279: } catch (Exception ignore) {
280: // just ignore
281: }
282: }
283: }
284:
285: return pk;
286:
287: }
288:
289: /*
290: * ejbCreate(.....) method
291: */
292: public java.lang.String ejbCreateForRequired(long p1)
293: throws javax.ejb.CreateException {
294:
295: // Container-managed fields must be set to the Java language default values
296: this .accno = null;
297: this .customer = null;
298: this .balance = 0L;
299:
300: super .ejbCreateForRequired(p1);
301:
302: java.lang.String pk;
303: // Initialize the pk with the primkey-field
304: pk = this .accno;
305:
306: Connection conn = null;
307: PreparedStatement pStmt = null;
308: try {
309: conn = getConnection();
310: pStmt = conn
311: .prepareStatement("insert into "
312: + tableName
313: + " (c_accno, c_customer, c_balance) values (?, ?, ?)");
314: if (this .accno == null) {
315: pStmt.setNull(1, Types.VARCHAR);
316: } else {
317: pStmt.setString(1, this .accno);
318: }
319: if (this .customer == null) {
320: pStmt.setNull(2, Types.VARCHAR);
321: } else {
322: pStmt.setString(2, this .customer);
323: }
324: pStmt.setLong(3, this .balance);
325: pStmt.executeUpdate();
326: } catch (Exception e) {
327: throw new CreateException(e.toString());
328: } finally {
329: if (pStmt != null) {
330: try {
331: pStmt.close();
332: } catch (Exception ignore) {
333: // just ignore
334: }
335: }
336: if (conn != null) {
337: try {
338: conn.close();
339: } catch (Exception ignore) {
340: // just ignore
341: }
342: }
343: }
344:
345: return pk;
346:
347: }
348:
349: /*
350: * ejbCreate(.....) method
351: */
352: public java.lang.String ejbCreateForRequiresNew(java.lang.String p1)
353: throws javax.ejb.CreateException {
354:
355: // Container-managed fields must be set to the Java language default values
356: this .accno = null;
357: this .customer = null;
358: this .balance = 0L;
359:
360: super .ejbCreateForRequiresNew(p1);
361:
362: java.lang.String pk;
363: // Initialize the pk with the primkey-field
364: pk = this .accno;
365:
366: Connection conn = null;
367: PreparedStatement pStmt = null;
368: try {
369: conn = getConnection();
370: pStmt = conn
371: .prepareStatement("insert into "
372: + tableName
373: + " (c_accno, c_customer, c_balance) values (?, ?, ?)");
374: if (this .accno == null) {
375: pStmt.setNull(1, Types.VARCHAR);
376: } else {
377: pStmt.setString(1, this .accno);
378: }
379: if (this .customer == null) {
380: pStmt.setNull(2, Types.VARCHAR);
381: } else {
382: pStmt.setString(2, this .customer);
383: }
384: pStmt.setLong(3, this .balance);
385: pStmt.executeUpdate();
386: } catch (Exception e) {
387: throw new CreateException(e.toString());
388: } finally {
389: if (pStmt != null) {
390: try {
391: pStmt.close();
392: } catch (Exception ignore) {
393: // just ignore
394: }
395: }
396: if (conn != null) {
397: try {
398: conn.close();
399: } catch (Exception ignore) {
400: // just ignore
401: }
402: }
403: }
404:
405: return pk;
406:
407: }
408:
409: /*
410: * ejbCreate(.....) method
411: */
412: public java.lang.String ejbCreateForSupports(boolean p1)
413: throws javax.ejb.CreateException {
414:
415: // Container-managed fields must be set to the Java language default values
416: this .accno = null;
417: this .customer = null;
418: this .balance = 0L;
419:
420: super .ejbCreateForSupports(p1);
421:
422: java.lang.String pk;
423: // Initialize the pk with the primkey-field
424: pk = this .accno;
425:
426: Connection conn = null;
427: PreparedStatement pStmt = null;
428: try {
429: conn = getConnection();
430: pStmt = conn
431: .prepareStatement("insert into "
432: + tableName
433: + " (c_accno, c_customer, c_balance) values (?, ?, ?)");
434: if (this .accno == null) {
435: pStmt.setNull(1, Types.VARCHAR);
436: } else {
437: pStmt.setString(1, this .accno);
438: }
439: if (this .customer == null) {
440: pStmt.setNull(2, Types.VARCHAR);
441: } else {
442: pStmt.setString(2, this .customer);
443: }
444: pStmt.setLong(3, this .balance);
445: pStmt.executeUpdate();
446: } catch (Exception e) {
447: throw new CreateException(e.toString());
448: } finally {
449: if (pStmt != null) {
450: try {
451: pStmt.close();
452: } catch (Exception ignore) {
453: // just ignore
454: }
455: }
456: if (conn != null) {
457: try {
458: conn.close();
459: } catch (Exception ignore) {
460: // just ignore
461: }
462: }
463: }
464:
465: return pk;
466:
467: }
468:
469: /*
470: * ejbFindByPrimaryKey(...) method
471: */
472: public java.lang.String ejbFindByPrimaryKey(java.lang.String pk)
473: throws FinderException {
474:
475: Connection conn = null;
476: PreparedStatement pStmt = null;
477: try {
478: conn = getConnection();
479: pStmt = conn.prepareStatement("select c_accno from "
480: + tableName + " where c_accno=?");
481: pStmt.setString(1, pk);
482: ResultSet rs = pStmt.executeQuery();
483: if (rs.next() == false) {
484: throw new ObjectNotFoundException(
485: "Object not found in database (ejbFindByPrimaryKey("
486: + pk.toString() + "))");
487: }
488: } catch (ObjectNotFoundException oe) {
489: throw oe;
490: } catch (Exception e) {
491: throw new FinderException(
492: "Failed to find bean from database in ejbFindByPrimaryKey");
493: } finally {
494: if (pStmt != null) {
495: try {
496: pStmt.close();
497: } catch (Exception ignore) {
498: // just ignore
499: }
500: }
501: if (conn != null) {
502: try {
503: conn.close();
504: } catch (Exception ignore) {
505: // just ignore
506: }
507: }
508: }
509: return pk;
510: }
511:
512: /*
513: * ejbFinder_mandatory(...) transaction attribute = notsupported
514: */
515: public java.lang.String ejbFinder_mandatory()
516: throws FinderException {
517:
518: // We must be outside any transaction
519: if (!isAssociated()) {
520: throw new EJBException(
521: "ejbFinder_mandatory: should not be in a transaction");
522: }
523:
524: return commonFind("Mandatory");
525: }
526:
527: /*
528: * ejbFinder_never(...) transaction attribute = never
529: */
530: public java.lang.String ejbFinder_never() throws FinderException {
531:
532: if (isAssociated()) {
533: throw new EJBException("ejbFinder_never in a transaction");
534: }
535:
536: return commonFind("Never");
537: }
538:
539: /*
540: * ejbFinder_notsupported(...) transaction attribute = notsupported
541: */
542: public java.lang.String ejbFinder_notsupported()
543: throws FinderException {
544: // We must be outside any transaction
545: if (isAssociated()) {
546: throw new EJBException(
547: "ejbFinder_notsupported: should not be in a transaction");
548: }
549:
550: return commonFind("NotSupported");
551: }
552:
553: /*
554: * ejbFinder_required(...)
555: */
556: public java.lang.String ejbFinder_required() throws FinderException {
557:
558: // We must be outside any transaction
559: if (!isAssociated()) {
560: throw new EJBException(
561: "ejbFinder_notsupported: should not be in a transaction");
562: }
563:
564: return commonFind("Required");
565:
566: }
567:
568: /*
569: * ejbFinder_requiresnew transaction attribute = requiresnew
570: */
571: public java.lang.String ejbFinder_requiresnew()
572: throws FinderException {
573:
574: // We must be in a transaction
575: if (!isAssociated()) {
576: throw new EJBException(
577: "ejbFinder_requiresnew not in a transaction");
578: }
579:
580: return commonFind("RequiresNew");
581: }
582:
583: /*
584: * ejbFinder_supports(...) transaction attribute = supports
585: */
586: public java.lang.String ejbFinder_supports(boolean p1)
587: throws FinderException {
588:
589: if (p1 == true) {
590: if (!isAssociated()) {
591: throw new EJBException(
592: "ejbFinder_supports: should be in a transaction");
593: }
594: } else {
595: if (isAssociated()) {
596: throw new EJBException(
597: "ejbFinder_supports: should not be in a transaction");
598: }
599: }
600: return commonFind("Supports");
601: }
602:
603: /*
604: * ejbLoad() method
605: */
606: public void ejbLoad() {
607: Connection conn = null;
608: PreparedStatement pStmt = null;
609: try {
610: String pk = (String) entityContext.getPrimaryKey();
611: conn = getConnection();
612: pStmt = conn
613: .prepareStatement("select c_accno, c_customer, c_balance from "
614: + tableName + " where c_accno=?");
615: pStmt.setString(1, pk);
616: ResultSet rs = pStmt.executeQuery();
617: if (rs.next() == false) {
618: throw new EJBException(
619: "Failed to load bean from database pk= "
620: + pk.toString());
621: }
622: this .accno = rs.getString(1);
623: this .customer = rs.getString(2);
624: this .balance = rs.getLong(3);
625: } catch (Exception e) {
626: throw new EJBException(e);
627: } finally {
628: if (pStmt != null) {
629: try {
630: pStmt.close();
631: } catch (Exception ignore) {
632: // just ignore
633: }
634: }
635: if (conn != null) {
636: try {
637: conn.close();
638: } catch (Exception ignore) {
639: // just ignore
640: }
641: }
642: }
643: super .ejbLoad();
644: }
645:
646: /*
647: * ejbStore() method
648: */
649: public void ejbStore() {
650: super .ejbStore();
651: Connection conn = null;
652: PreparedStatement pStmt = null;
653: try {
654: String pk = (String) entityContext.getPrimaryKey();
655: ;
656: conn = getConnection();
657: pStmt = conn.prepareStatement("update " + tableName
658: + " set c_customer=?, c_balance=? where c_accno=?");
659: if (this .customer == null) {
660: pStmt.setNull(1, Types.VARCHAR);
661: } else {
662: pStmt.setString(1, this .customer);
663: }
664: pStmt.setLong(2, this .balance);
665: pStmt.setString(3, pk);
666: pStmt.executeUpdate();
667: } catch (Exception e) {
668: throw new EJBException(e);
669: } finally {
670: if (pStmt != null) {
671: try {
672: pStmt.close();
673: } catch (Exception ignore) {
674: // just ignore
675: }
676: }
677: if (conn != null) {
678: try {
679: conn.close();
680: } catch (Exception ignore) {
681: // just ignore
682: }
683: }
684: }
685: }
686:
687: /*
688: * ejbRemove() method
689: */
690: public void ejbRemove() throws javax.ejb.RemoveException {
691:
692: super .ejbRemove();
693: Connection conn = null;
694: PreparedStatement pStmt = null;
695: try {
696: String pk = (String) entityContext.getPrimaryKey();
697: ;
698: conn = getConnection();
699: pStmt = conn.prepareStatement("delete from " + tableName
700: + " where c_accno=?");
701: pStmt.setString(1, pk);
702: pStmt.executeUpdate();
703: } catch (Exception e) {
704: throw new RemoveException(e.toString());
705: } finally {
706: if (pStmt != null) {
707: try {
708: pStmt.close();
709: } catch (Exception ignore) {
710: // just ignore
711: }
712: }
713: if (conn != null) {
714: try {
715: conn.close();
716: } catch (Exception ignore) {
717: // just ignore
718: }
719: }
720: }
721: }
722:
723: public void setEntityContext(EntityContext ctx) {
724: this .entityContext = ctx;
725: }
726:
727: public void unsetEntityContext() {
728: this .entityContext = null;
729: }
730:
731: /*
732: * commonFind is the common part of the ejbFinder_XX methods
733: *
734: */
735:
736: private java.lang.String commonFind(String p1)
737: throws FinderException {
738: java.lang.String pk;
739: Connection conn = null;
740: PreparedStatement pStmt = null;
741: try {
742: conn = getConnection();
743: pStmt = conn.prepareStatement("select c_accno from "
744: + tableName + " where c_customer = " + "'" + p1
745: + "'");
746: ResultSet rs = pStmt.executeQuery();
747: if (rs.next() == false) {
748: throw new ObjectNotFoundException(
749: "Object not found in database ");
750: }
751: pk = rs.getString(1);
752: } catch (ObjectNotFoundException oe) {
753: throw oe;
754: } catch (Exception e) {
755: throw new FinderException(e.toString());
756: } finally {
757: if (pStmt != null) {
758: try {
759: pStmt.close();
760: } catch (Exception ignore) {
761: // just ignore
762: }
763: }
764: if (conn != null) {
765: try {
766: conn.close();
767: } catch (Exception ignore) {
768: // just ignore
769: }
770: }
771: }
772: return pk;
773: }
774:
775: private Connection getConnection() throws java.sql.SQLException {
776: if (dataSource == null) {
777: // Finds DataSource from JNDI
778: Context initialContext = null;
779: try {
780: initialContext = new InitialContext();
781: dataSource = (DataSource) initialContext
782: .lookup("java:comp/env/jdbc/BMP");
783: } catch (Exception e) {
784: throw new javax.ejb.EJBException(
785: "Pb with naming context " + e);
786: }
787: }
788: Connection ret = dataSource.getConnection();
789: if (ret == null) {
790: throw new javax.ejb.EJBException(
791: "dataSource.getConnection() returned null");
792: }
793: return ret;
794: }
795:
796: }
|