KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > stateful > BeanTxStatefulBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: BeanTxStatefulBean.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test.stateful;
46
47 import java.rmi.RemoteException JavaDoc;
48 import java.sql.Connection JavaDoc;
49 import java.sql.PreparedStatement JavaDoc;
50 import java.sql.ResultSet JavaDoc;
51
52 import javax.ejb.CreateException JavaDoc;
53 import javax.ejb.EJBException JavaDoc;
54 import javax.ejb.SessionContext JavaDoc;
55 import javax.naming.InitialContext JavaDoc;
56 import javax.sql.DataSource JavaDoc;
57 import javax.transaction.RollbackException JavaDoc;
58 import javax.transaction.UserTransaction JavaDoc;
59
60 import org.openejb.test.object.Account;
61 import org.openejb.test.object.Transaction;
62
63 /**
64  *
65  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
66  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
67  */

68 public class BeanTxStatefulBean implements javax.ejb.SessionBean JavaDoc{
69
70     
71     private String JavaDoc name;
72     private SessionContext JavaDoc ejbContext;
73     private InitialContext JavaDoc jndiContext;
74     public final String JavaDoc jndiDatabaseEntry = "jdbc/stateful/beanManagedTransaction/database";
75
76
77     
78     //=============================
79
// Home interface methods
80
//
81
/**
82      * Maps to BasicStatefulHome.create
83      *
84      * @param name
85      * @exception javax.ejb.CreateException
86      * @see BasicStatefulHome.create
87      */

88     public void ejbCreate(String JavaDoc name)
89     throws javax.ejb.CreateException JavaDoc{
90         this.name = name;
91         try {
92             jndiContext = new InitialContext JavaDoc();
93         } catch (Exception JavaDoc e){
94             throw new CreateException JavaDoc("Can not get the initial context: "+e.getMessage());
95         }
96     }
97     //
98
// Home interface methods
99
//=============================
100

101
102     //=============================
103
// Remote interface methods
104
//
105

106     /**
107      * Maps to BasicStatefulObject.businessMethod
108      *
109      * @return
110      * @see BasicStatefulObject.businessMethod
111      */

112     public Transaction JavaDoc getUserTransaction() throws RemoteException JavaDoc{
113         
114         UserTransaction JavaDoc ut = null;
115         try{
116             ut = ejbContext.getUserTransaction();
117         } catch (IllegalStateException JavaDoc ise){
118             throw new RemoteException JavaDoc(ise.getMessage());
119         }
120         if (ut == null) return null;
121         return new Transaction JavaDoc(ut);
122     }
123     
124     public Transaction JavaDoc jndiUserTransaction() throws RemoteException JavaDoc{
125         UserTransaction JavaDoc ut = null;
126         try{
127             ut = (UserTransaction JavaDoc)jndiContext.lookup("java:comp/UserTransaction");
128         } catch (Exception JavaDoc e){
129             throw new RemoteException JavaDoc(e.getMessage());
130         }
131         if (ut == null) return null;
132         return new Transaction JavaDoc(ut);
133     }
134
135     public void openAccount(Account acct, Boolean JavaDoc rollback) throws RemoteException JavaDoc, RollbackException JavaDoc{
136         
137         try{
138             
139             DataSource JavaDoc ds = (DataSource JavaDoc)javax.rmi.PortableRemoteObject.narrow( jndiContext.lookup("java:comp/env/datasource"), DataSource JavaDoc.class);
140             Connection JavaDoc con = ds.getConnection();
141             
142             UserTransaction JavaDoc ut = ejbContext.getUserTransaction();
143             /*[1] Begin the transaction */
144             ut.begin();
145
146
147             /*[2] Update the table */
148             PreparedStatement JavaDoc stmt = con.prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
149             stmt.setString(1, acct.getSsn());
150             stmt.setString(2, acct.getFirstName());
151             stmt.setString(3, acct.getLastName());
152             stmt.setInt(4, acct.getBalance());
153             stmt.executeUpdate();
154
155             /*[3] Commit or Rollback the transaction */
156             if (rollback.booleanValue()) ut.setRollbackOnly();
157             
158             /*[4] Commit or Rollback the transaction */
159             ut.commit();
160             
161
162             /*[4] Clean up */
163             stmt.close();
164             con.close();
165         } catch (RollbackException JavaDoc re){
166             throw re;
167         } catch (Exception JavaDoc e){
168             e.printStackTrace();
169             throw new RemoteException JavaDoc("[Bean] "+e.getClass().getName()+" : "+e.getMessage());
170         }
171     }
172
173     public Account retreiveAccount(String JavaDoc ssn) throws RemoteException JavaDoc {
174         Account acct = new Account();
175         try{
176             DataSource JavaDoc ds = (DataSource JavaDoc)javax.rmi.PortableRemoteObject.narrow( jndiContext.lookup("java:comp/env/datasource"), DataSource JavaDoc.class);
177             Connection JavaDoc con = ds.getConnection();
178
179             PreparedStatement JavaDoc stmt = con.prepareStatement("select * from Account where SSN = ?");
180             stmt.setString(1, ssn);
181             ResultSet JavaDoc rs = stmt.executeQuery();
182             if (!rs.next()) return null;
183             
184             acct.setSsn( rs.getString(1) );
185             acct.setFirstName( rs.getString(2) );
186             acct.setLastName( rs.getString(3) );
187             acct.setBalance( rs.getInt(4) );
188
189             stmt.close();
190             con.close();
191         } catch (Exception JavaDoc e){
192             e.printStackTrace();
193             throw new RemoteException JavaDoc("[Bean] "+e.getClass().getName()+" : "+e.getMessage());
194         }
195         return acct;
196     }
197
198
199     //
200
// Remote interface methods
201
//=============================
202

203
204     //=================================
205
// SessionBean interface methods
206
//
207
/**
208      * Set the associated session context. The container calls this method
209      * after the instance creation.
210      */

211     public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc,RemoteException JavaDoc {
212         ejbContext = ctx;
213     }
214     /**
215      * A container invokes this method before it ends the life of the session
216      * object. This happens as a result of a client's invoking a remove
217      * operation, or when a container decides to terminate the session object
218      * after a timeout.
219      */

220     public void ejbRemove() throws EJBException JavaDoc,RemoteException JavaDoc {
221     }
222     /**
223      * The activate method is called when the instance is activated
224      * from its "passive" state. The instance should acquire any resource
225      * that it has released earlier in the ejbPassivate() method.
226      */

227     public void ejbActivate() throws EJBException JavaDoc,RemoteException JavaDoc {
228     }
229     /**
230      * The passivate method is called before the instance enters
231      * the "passive" state. The instance should release any resources that
232      * it can re-acquire later in the ejbActivate() method.
233      */

234     public void ejbPassivate() throws EJBException JavaDoc,RemoteException JavaDoc {
235     }
236     //
237
// SessionBean interface methods
238
//==================================
239

240 }
241
Popular Tags