KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > transacted > SimpleEB


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SimpleEB.java,v 1.4 2004/03/19 11:57:17 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.transacted;
27
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.Types JavaDoc;
32
33 import javax.ejb.CreateException JavaDoc;
34 import javax.ejb.EJBException JavaDoc;
35 import javax.ejb.EntityBean JavaDoc;
36 import javax.ejb.EntityContext JavaDoc;
37 import javax.ejb.FinderException JavaDoc;
38 import javax.ejb.ObjectNotFoundException JavaDoc;
39 import javax.ejb.RemoveException JavaDoc;
40 import javax.naming.Context JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import javax.sql.DataSource JavaDoc;
43
44 /**
45  * Entity bean with bean managed persistence.
46  *
47  */

48 public class SimpleEB extends SimpleEC implements EntityBean JavaDoc {
49
50  
51     // Database related information
52
private DataSource JavaDoc dataSource = null;
53     static final String JavaDoc tableName = "transactedSimpleEB";
54
55
56     /*
57      * ejbCreate(.....) method
58      */

59     public java.lang.String JavaDoc ejbCreate()
60         throws javax.ejb.CreateException JavaDoc {
61         
62         super.ejbCreate();
63
64         java.lang.String JavaDoc pk;
65         // Initialize the pk with the primkey-field
66
pk = this.accno;
67
68         Connection JavaDoc conn = null;
69         PreparedStatement JavaDoc pStmt = null;
70         try {
71             conn = getConnection();
72             pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)");
73             if ( this.accno == null ) {
74                 pStmt.setNull(1, Types.VARCHAR);
75             } else {
76                 pStmt.setString(1, this.accno);
77             }
78             if ( this.customer == null ) {
79                 pStmt.setNull(2, Types.VARCHAR);
80             } else {
81                 pStmt.setString(2, this.customer);
82             }
83             pStmt.setLong(3, this.balance);
84             pStmt.executeUpdate();
85         } catch (Exception JavaDoc e) {
86             throw new CreateException JavaDoc(e.toString());
87         } finally {
88             if (pStmt != null) {
89                 try {
90                     pStmt.close();
91                 } catch (Exception JavaDoc ignore) {
92                     // just ignore
93
}
94             }
95             if (conn != null) {
96                 try {
97                     conn.close();
98                 } catch (Exception JavaDoc ignore) {
99                     // just ignore
100
}
101             }
102         }
103
104         return pk;
105
106     }
107
108
109
110     /*
111      * ejbCreate(.....) method
112      */

113     public java.lang.String JavaDoc ejbCreate(int p1)
114         throws javax.ejb.CreateException JavaDoc {
115
116
117         // Container-managed fields must be set to the Java language default values
118
this.accno = null;
119         this.customer = null;
120         this.balance = 0L;
121
122         super.ejbCreate(p1);
123
124         java.lang.String JavaDoc pk;
125         // Initialize the pk with the primkey-field
126
pk = this.accno;
127
128         Connection JavaDoc conn = null;
129         PreparedStatement JavaDoc pStmt = null;
130         try {
131             conn = getConnection();
132             pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)");
133             if ( this.accno == null ) {
134                 pStmt.setNull(1, Types.VARCHAR);
135             } else {
136                 pStmt.setString(1, this.accno);
137             }
138             if ( this.customer == null ) {
139                 pStmt.setNull(2, Types.VARCHAR);
140             } else {
141                 pStmt.setString(2, this.customer);
142             }
143             pStmt.setLong(3, this.balance);
144             pStmt.executeUpdate();
145         } catch (Exception JavaDoc e) {
146             throw new CreateException JavaDoc(e.toString());
147         } finally {
148             if (pStmt != null) {
149                 try {
150                     pStmt.close();
151                 } catch (Exception JavaDoc ignore) {
152                     // just ignore
153
}
154             }
155             if (conn != null) {
156                 try {
157                     conn.close();
158                 } catch (Exception JavaDoc ignore) {
159                     // just ignore
160
}
161             }
162         }
163
164         return pk;
165
166     }
167
168
169     /*
170      * ejbCreate(.....) method
171      */

172     public java.lang.String JavaDoc ejbCreateForMandatory(char p1)
173         throws javax.ejb.CreateException JavaDoc {
174
175
176         // Container-managed fields must be set to the Java language default values
177
this.accno = null;
178         this.customer = null;
179         this.balance = 0L;
180
181         super.ejbCreateForMandatory(p1);
182
183         java.lang.String JavaDoc pk;
184         // Initialize the pk with the primkey-field
185
pk = this.accno;
186
187         Connection JavaDoc conn = null;
188         PreparedStatement JavaDoc pStmt = null;
189         try {
190             conn = getConnection();
191             pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)");
192             if ( this.accno == null ) {
193                 pStmt.setNull(1, Types.VARCHAR);
194             } else {
195                 pStmt.setString(1, this.accno);
196             }
197             if ( this.customer == null ) {
198                 pStmt.setNull(2, Types.VARCHAR);
199             } else {
200                 pStmt.setString(2, this.customer);
201             }
202             pStmt.setLong(3, this.balance);
203             pStmt.executeUpdate();
204         } catch (Exception JavaDoc e) {
205             throw new CreateException JavaDoc(e.toString());
206         } finally {
207             if (pStmt != null) {
208                 try {
209                     pStmt.close();
210                 } catch (Exception JavaDoc ignore) {
211                     // just ignore
212
}
213             }
214             if (conn != null) {
215                 try {
216                     conn.close();
217                 } catch (Exception JavaDoc ignore) {
218                     // just ignore
219
}
220             }
221         }
222
223         return pk;
224
225     }
226
227
228     /*
229      * ejbCreate(.....) method
230      */

231     public java.lang.String JavaDoc ejbCreateForNever(short p1)
232         throws javax.ejb.CreateException JavaDoc {
233
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 JavaDoc pk;
243         // Initialize the pk with the primkey-field
244
pk = this.accno;
245
246         Connection JavaDoc conn = null;
247         PreparedStatement JavaDoc pStmt = null;
248         try {
249             conn = getConnection();
250             pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)");
251             if ( this.accno == null ) {
252                 pStmt.setNull(1, Types.VARCHAR);
253             } else {
254                 pStmt.setString(1, this.accno);
255             }
256             if ( this.customer == null ) {
257                 pStmt.setNull(2, Types.VARCHAR);
258             } else {
259                 pStmt.setString(2, this.customer);
260             }
261             pStmt.setLong(3, this.balance);
262             pStmt.executeUpdate();
263         } catch (Exception JavaDoc e) {
264             throw new CreateException JavaDoc(e.toString());
265         } finally {
266             if (pStmt != null) {
267                 try {
268                     pStmt.close();
269                 } catch (Exception JavaDoc ignore) {
270                     // just ignore
271
}
272             }
273             if (conn != null) {
274                 try {
275                     conn.close();
276                 } catch (Exception JavaDoc ignore) {
277                     // just ignore
278
}
279             }
280         }
281
282         return pk;
283
284     }
285
286
287     /*
288      * ejbCreate(.....) method
289      */

290     public java.lang.String JavaDoc ejbCreateForRequired(long p1)
291         throws javax.ejb.CreateException JavaDoc {
292
293
294         // Container-managed fields must be set to the Java language default values
295
this.accno = null;
296         this.customer = null;
297         this.balance = 0L;
298
299         super.ejbCreateForRequired(p1);
300
301         java.lang.String JavaDoc pk;
302         // Initialize the pk with the primkey-field
303
pk = this.accno;
304
305         Connection JavaDoc conn = null;
306         PreparedStatement JavaDoc pStmt = null;
307         try {
308             conn = getConnection();
309             pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)");
310             if ( this.accno == null ) {
311                 pStmt.setNull(1, Types.VARCHAR);
312             } else {
313                 pStmt.setString(1, this.accno);
314             }
315             if ( this.customer == null ) {
316                 pStmt.setNull(2, Types.VARCHAR);
317             } else {
318                 pStmt.setString(2, this.customer);
319             }
320             pStmt.setLong(3, this.balance);
321             pStmt.executeUpdate();
322         } catch (Exception JavaDoc e) {
323             throw new CreateException JavaDoc(e.toString());
324         } finally {
325             if (pStmt != null) {
326                 try {
327                     pStmt.close();
328                 } catch (Exception JavaDoc ignore) {
329                     // just ignore
330
}
331             }
332             if (conn != null) {
333                 try {
334                     conn.close();
335                 } catch (Exception JavaDoc ignore) {
336                     // just ignore
337
}
338             }
339         }
340
341         return pk;
342
343     }
344
345
346     /*
347      * ejbCreate(.....) method
348      */

349     public java.lang.String JavaDoc ejbCreateForRequiresNew(java.lang.String JavaDoc p1)
350         throws javax.ejb.CreateException JavaDoc {
351
352
353         // Container-managed fields must be set to the Java language default values
354
this.accno = null;
355         this.customer = null;
356         this.balance = 0L;
357
358         super.ejbCreateForRequiresNew(p1);
359
360         java.lang.String JavaDoc pk;
361         // Initialize the pk with the primkey-field
362
pk = this.accno;
363
364         Connection JavaDoc conn = null;
365         PreparedStatement JavaDoc pStmt = null;
366         try {
367             conn = getConnection();
368             pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)");
369             if ( this.accno == null ) {
370                 pStmt.setNull(1, Types.VARCHAR);
371             } else {
372                 pStmt.setString(1, this.accno);
373             }
374             if ( this.customer == null ) {
375                 pStmt.setNull(2, Types.VARCHAR);
376             } else {
377                 pStmt.setString(2, this.customer);
378             }
379             pStmt.setLong(3, this.balance);
380             pStmt.executeUpdate();
381         } catch (Exception JavaDoc e) {
382             throw new CreateException JavaDoc(e.toString());
383         } finally {
384             if (pStmt != null) {
385                 try {
386                     pStmt.close();
387                 } catch (Exception JavaDoc ignore) {
388                     // just ignore
389
}
390             }
391             if (conn != null) {
392                 try {
393                     conn.close();
394                 } catch (Exception JavaDoc ignore) {
395                     // just ignore
396
}
397             }
398         }
399
400         return pk;
401
402     }
403
404
405     /*
406      * ejbCreate(.....) method
407      */

408     public java.lang.String JavaDoc ejbCreateForSupports(boolean p1)
409         throws javax.ejb.CreateException JavaDoc {
410
411
412         // Container-managed fields must be set to the Java language default values
413
this.accno = null;
414         this.customer = null;
415         this.balance = 0L;
416
417         super.ejbCreateForSupports(p1);
418
419         java.lang.String JavaDoc pk;
420         // Initialize the pk with the primkey-field
421
pk = this.accno;
422
423         Connection JavaDoc conn = null;
424         PreparedStatement JavaDoc pStmt = null;
425         try {
426             conn = getConnection();
427             pStmt = conn.prepareStatement("insert into "+tableName+" (c_accno, c_customer, c_balance) values (?, ?, ?)");
428             if ( this.accno == null ) {
429                 pStmt.setNull(1, Types.VARCHAR);
430             } else {
431                 pStmt.setString(1, this.accno);
432             }
433             if ( this.customer == null ) {
434                 pStmt.setNull(2, Types.VARCHAR);
435             } else {
436                 pStmt.setString(2, this.customer);
437             }
438             pStmt.setLong(3, this.balance);
439             pStmt.executeUpdate();
440         } catch (Exception JavaDoc e) {
441             throw new CreateException JavaDoc(e.toString());
442         } finally {
443             if (pStmt != null) {
444                 try {
445                     pStmt.close();
446                 } catch (Exception JavaDoc ignore) {
447                     // just ignore
448
}
449             }
450             if (conn != null) {
451                 try {
452                     conn.close();
453                 } catch (Exception JavaDoc ignore) {
454                     // just ignore
455
}
456             }
457         }
458
459         return pk;
460
461     }
462
463
464     /*
465      * ejbFindByPrimaryKey(...) method
466      */

467     public java.lang.String JavaDoc ejbFindByPrimaryKey(java.lang.String JavaDoc pk)
468         throws FinderException JavaDoc {
469
470         Connection JavaDoc conn = null;
471         PreparedStatement JavaDoc pStmt = null;
472         try {
473             conn = getConnection();
474             pStmt = conn.prepareStatement("select c_accno from "+tableName+" where c_accno=?");
475             pStmt.setString(1, pk);
476             ResultSet JavaDoc rs = pStmt.executeQuery();
477             if (rs.next() == false) {
478                 throw new ObjectNotFoundException JavaDoc("Object not found in database (ejbFindByPrimaryKey("+pk.toString()+"))");
479             }
480         } catch (ObjectNotFoundException JavaDoc oe) {
481             throw oe;
482         } catch (Exception JavaDoc e) {
483             throw new FinderException JavaDoc("Failed to find bean from database in ejbFindByPrimaryKey");
484         } finally {
485             if (pStmt != null) {
486                 try {
487                     pStmt.close();
488                 } catch (Exception JavaDoc ignore) {
489                     // just ignore
490
}
491             }
492             if (conn != null) {
493                 try {
494                     conn.close();
495                 } catch (Exception JavaDoc ignore) {
496                     // just ignore
497
}
498             }
499         }
500         return pk;
501     }
502
503
504     /*
505      * ejbFinder_mandatory(...) transaction attribute = notsupported
506      */

507     public java.lang.String JavaDoc ejbFinder_mandatory()
508         throws FinderException JavaDoc {
509  
510
511         // We must be outside any transaction
512
if (!isAssociated()) {
513             throw new EJBException JavaDoc("ejbFinder_mandatory: should not be in a transaction");
514         }
515
516         return commonFind("Mandatory");
517     }
518
519
520     /*
521      * ejbFinder_never(...) transaction attribute = never
522      */

523     public java.lang.String JavaDoc ejbFinder_never()
524         throws FinderException JavaDoc {
525
526         if (isAssociated()) {
527             throw new EJBException JavaDoc("ejbFinder_never in a transaction");
528         }
529
530         return commonFind("Never");
531     }
532
533
534     /*
535      * ejbFinder_notsupported(...) transaction attribute = notsupported
536      */

537     public java.lang.String JavaDoc ejbFinder_notsupported()
538         throws FinderException JavaDoc {
539         // We must be outside any transaction
540
if (isAssociated()) {
541             throw new EJBException JavaDoc("ejbFinder_notsupported: should not be in a transaction");
542         }
543
544         return commonFind("NotSupported");
545     }
546
547
548     /*
549      * ejbFinder_required(...)
550      */

551     public java.lang.String JavaDoc ejbFinder_required()
552         throws FinderException JavaDoc {
553  
554         // We must be outside any transaction
555
if (!isAssociated()) {
556             throw new EJBException JavaDoc("ejbFinder_notsupported: should not be in a transaction");
557         }
558
559         return commonFind("Required");
560
561     }
562
563
564     /*
565      * ejbFinder_requiresnew transaction attribute = requiresnew
566      */

567     public java.lang.String JavaDoc ejbFinder_requiresnew()
568         throws FinderException JavaDoc {
569  
570         // We must be in a transaction
571
if (!isAssociated()) {
572             throw new EJBException JavaDoc("ejbFinder_requiresnew not in a transaction");
573         }
574
575         return commonFind("RequiresNew");
576     }
577
578
579     /*
580      * ejbFinder_supports(...) transaction attribute = supports
581      */

582     public java.lang.String JavaDoc ejbFinder_supports(boolean p1)
583         throws FinderException JavaDoc {
584
585
586         if (p1 == true) {
587             if (!isAssociated()) {
588                 throw new EJBException JavaDoc("ejbFinder_supports: should be in a transaction");
589             }
590         } else {
591             if (isAssociated()) {
592                 throw new EJBException JavaDoc("ejbFinder_supports: should not be in a transaction");
593             }
594         }
595         return commonFind("Supports");
596     }
597
598
599     /*
600      * ejbLoad() method
601      */

602     public void ejbLoad()
603     {
604         Connection JavaDoc conn = null;
605         PreparedStatement JavaDoc pStmt = null;
606         try {
607             String JavaDoc pk = (String JavaDoc)entityContext.getPrimaryKey();
608             conn = getConnection();
609             pStmt = conn.prepareStatement("select c_accno, c_customer, c_balance from "+tableName+" where c_accno=?");
610             pStmt.setString(1, pk);
611             ResultSet JavaDoc rs = pStmt.executeQuery();
612             if (rs.next() == false) {
613                 throw new EJBException JavaDoc("Failed to load bean from database pk= "+pk.toString());
614             }
615             this.accno = rs.getString(1);
616             this.customer = rs.getString(2);
617             this.balance = rs.getLong(3);
618         } catch (Exception JavaDoc e) {
619             throw new EJBException JavaDoc(e);
620         } finally {
621             if (pStmt != null) {
622                 try {
623                     pStmt.close();
624                 } catch (Exception JavaDoc ignore) {
625                     // just ignore
626
}
627             }
628             if (conn != null) {
629                 try {
630                     conn.close();
631                 } catch (Exception JavaDoc ignore) {
632                     // just ignore
633
}
634             }
635         }
636         super.ejbLoad();
637     }
638
639
640     /*
641      * ejbStore() method
642      */

643     public void ejbStore()
644     {
645         super.ejbStore();
646         Connection JavaDoc conn = null;
647         PreparedStatement JavaDoc pStmt = null;
648         try {
649             String JavaDoc pk = (String JavaDoc)entityContext.getPrimaryKey();;
650             conn = getConnection();
651             pStmt = conn.prepareStatement("update "+tableName+" set c_customer=?, c_balance=? where c_accno=?");
652             if ( this.customer == null ) {
653                 pStmt.setNull(1, Types.VARCHAR);
654             } else {
655                 pStmt.setString(1, this.customer);
656             }
657             pStmt.setLong(2, this.balance);
658             pStmt.setString(3, pk);
659             pStmt.executeUpdate();
660         } catch (Exception JavaDoc e) {
661             throw new EJBException JavaDoc(e);
662         } finally {
663             if (pStmt != null) {
664                 try {
665                     pStmt.close();
666                 } catch (Exception JavaDoc ignore) {
667                     // just ignore
668
}
669             }
670             if (conn != null) {
671                 try {
672                     conn.close();
673                 } catch (Exception JavaDoc ignore) {
674                     // just ignore
675
}
676             }
677         }
678     }
679
680
681     /*
682      * ejbRemove() method
683      */

684     public void ejbRemove()
685         throws javax.ejb.RemoveException JavaDoc {
686
687         super.ejbRemove();
688         Connection JavaDoc conn = null;
689         PreparedStatement JavaDoc pStmt = null;
690         try {
691             String JavaDoc pk = (String JavaDoc)entityContext.getPrimaryKey();;
692             conn = getConnection();
693             pStmt = conn.prepareStatement("delete from "+tableName+" where c_accno=?");
694             pStmt.setString(1, pk);
695             pStmt.executeUpdate();
696         } catch (Exception JavaDoc e) {
697             throw new RemoveException JavaDoc(e.toString());
698         } finally {
699             if (pStmt != null) {
700                 try {
701                     pStmt.close();
702                 } catch (Exception JavaDoc ignore) {
703                     // just ignore
704
}
705             }
706             if (conn != null) {
707                 try {
708                     conn.close();
709                 } catch (Exception JavaDoc ignore) {
710                     // just ignore
711
}
712             }
713         }
714     }
715
716
717
718     public void setEntityContext(EntityContext JavaDoc ctx) {
719         this.entityContext = ctx;
720     }
721
722     public void unsetEntityContext() {
723         this.entityContext = null;
724     }
725
726
727     /*
728      * commonFind is the common part of the ejbFinder_XX methods
729      *
730      */

731
732     private java.lang.String JavaDoc commonFind(String JavaDoc p1) throws FinderException JavaDoc {
733         java.lang.String JavaDoc pk;
734         Connection JavaDoc conn = null;
735         PreparedStatement JavaDoc pStmt = null;
736         try {
737             conn = getConnection();
738             pStmt = conn.prepareStatement("select c_accno from "+tableName+" where c_customer = "+"'"+p1+"'");
739             ResultSet JavaDoc rs = pStmt.executeQuery();
740             if (rs.next() == false) {
741                 throw new ObjectNotFoundException JavaDoc("Object not found in database ");
742             }
743             pk = rs.getString(1);
744         } catch (ObjectNotFoundException JavaDoc oe) {
745             throw oe;
746         } catch (Exception JavaDoc e) {
747             throw new FinderException JavaDoc(e.toString());
748         } finally {
749             if (pStmt != null) {
750                 try {
751                     pStmt.close();
752                 } catch (Exception JavaDoc ignore) {
753                     // just ignore
754
}
755             }
756             if (conn != null) {
757                 try {
758                     conn.close();
759                 } catch (Exception JavaDoc ignore) {
760                     // just ignore
761
}
762             }
763         }
764         return pk;
765     }
766
767
768     private Connection JavaDoc getConnection() throws java.sql.SQLException JavaDoc {
769         if (dataSource == null) {
770             // Finds DataSource from JNDI
771
Context JavaDoc initialContext = null;
772             try {
773                 initialContext = new InitialContext JavaDoc();
774                 dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/BMP");
775             } catch (Exception JavaDoc e) {
776                 throw new javax.ejb.EJBException JavaDoc("Pb with naming context "+e);
777             }
778         }
779         Connection JavaDoc ret = dataSource.getConnection();
780         if (ret == null) {
781             throw new javax.ejb.EJBException JavaDoc("dataSource.getConnection() returned null");
782         }
783         return ret;
784     }
785
786 }
787
Popular Tags