KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > stateless > BeanTxStatelessBean


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: BeanTxStatelessBean.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test.stateless;
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 BeanTxStatelessBean 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/stateless/beanManagedTransaction/database";
75
76
77     
78     //=============================
79
// Home interface methods
80
//
81

82     //
83
// Home interface methods
84
//=============================
85

86
87     //=============================
88
// Remote interface methods
89
//
90

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

181
182     //=================================
183
// SessionBean interface methods
184
//
185
/**
186      *
187      * @param name
188      * @exception javax.ejb.CreateException
189      */

190     public void ejbCreate() throws javax.ejb.CreateException JavaDoc{
191         try {
192             jndiContext = new InitialContext JavaDoc();
193         } catch (Exception JavaDoc e){
194             throw new CreateException JavaDoc("Can not get the initial context: "+e.getMessage());
195         }
196     }
197     /**
198      * Set the associated session context. The container calls this method
199      * after the instance creation.
200      */

201     public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc,RemoteException JavaDoc {
202         ejbContext = ctx;
203     }
204     /**
205      * A container invokes this method before it ends the life of the session
206      * object. This happens as a result of a client's invoking a remove
207      * operation, or when a container decides to terminate the session object
208      * after a timeout.
209      */

210     public void ejbRemove() throws EJBException JavaDoc,RemoteException JavaDoc {
211     }
212     /**
213      * The activate method is called when the instance is activated
214      * from its "passive" state. The instance should acquire any resource
215      * that it has released earlier in the ejbPassivate() method.
216      */

217     public void ejbActivate() throws EJBException JavaDoc,RemoteException JavaDoc {
218     }
219     /**
220      * The passivate method is called before the instance enters
221      * the "passive" state. The instance should release any resources that
222      * it can re-acquire later in the ejbActivate() method.
223      */

224     public void ejbPassivate() throws EJBException JavaDoc,RemoteException JavaDoc {
225     }
226     //
227
// SessionBean interface methods
228
//==================================
229

230 }
231
Popular Tags