KickJava   Java API By Example, From Geeks To Geeks.

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


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 "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP 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  * THE OPENEJB GROUP 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 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: ContainerTxStatelessBean.java 1912 2005-06-16 22:29:56Z jlaskowski $
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
59 import org.openejb.test.object.Account;
60
61 /**
62  *
63  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
64  */

65 public class ContainerTxStatelessBean implements javax.ejb.SessionBean JavaDoc{
66
67     
68     private String JavaDoc name;
69     private SessionContext JavaDoc ejbContext;
70     private InitialContext JavaDoc jndiContext;
71     public final String JavaDoc jndiDatabaseEntry = "jdbc/stateless/containerManagedTransaction/database";
72
73
74     
75     //=============================
76
// Home interface methods
77
//
78

79     //
80
// Home interface methods
81
//=============================
82

83
84     //=============================
85
// Remote interface methods
86
//
87

88     public String JavaDoc txMandatoryMethod(String JavaDoc message) {
89         return message;
90     }
91     
92     public String JavaDoc txNeverMethod(String JavaDoc message) {
93         return message;
94     }
95     
96     public String JavaDoc txNotSupportedMethod(String JavaDoc message) {
97         return message;
98     }
99     
100     public String JavaDoc txRequiredMethod(String JavaDoc message) {
101         return message;
102     }
103     
104     public String JavaDoc txRequiresNewMethod(String JavaDoc message) {
105         return message;
106     }
107     
108     public String JavaDoc txSupportsMethod(String JavaDoc message) {
109         return message;
110     }
111
112     public void openAccount(Account acct, Boolean JavaDoc rollback) throws RollbackException JavaDoc{
113         
114         try{
115             DataSource JavaDoc ds = (DataSource JavaDoc)jndiContext.lookup("java:comp/env/database");
116             Connection JavaDoc con = ds.getConnection();
117             
118             /*[2] Update the table */
119             PreparedStatement JavaDoc stmt = con.prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
120             stmt.setString(1, acct.getSsn());
121             stmt.setString(2, acct.getFirstName());
122             stmt.setString(3, acct.getLastName());
123             stmt.setInt(4, acct.getBalance());
124             stmt.executeUpdate();
125
126             /*[4] Clean up */
127             stmt.close();
128             con.close();
129         } catch (Exception JavaDoc e){
130             //throw new RemoteException("[Bean] "+e.getClass().getName()+" : "+e.getMessage());
131
}
132     }
133
134     public Account retreiveAccount(String JavaDoc ssn) {
135         Account acct = new Account();
136         try{
137             DataSource JavaDoc ds = (DataSource JavaDoc) jndiContext.lookup("java:comp/env/database");
138             Connection JavaDoc con = ds.getConnection();
139
140             PreparedStatement JavaDoc stmt = con.prepareStatement("select * from Account where SSN = ?");
141             stmt.setString(1, ssn);
142             ResultSet JavaDoc rs = stmt.executeQuery();
143             if (!rs.next()) return null;
144             
145             acct.setSsn( rs.getString(1) );
146             acct.setFirstName( rs.getString(2) );
147             acct.setLastName( rs.getString(3) );
148             acct.setBalance( rs.getInt(4) );
149
150             stmt.close();
151             con.close();
152         } catch (Exception JavaDoc e){
153             //throw new RemoteException("[Bean] "+e.getClass().getName()+" : "+e.getMessage());
154
}
155         return acct;
156     }
157
158
159     //
160
// Remote interface methods
161
//=============================
162

163
164     //=================================
165
// SessionBean interface methods
166
//
167
/**
168      *
169      * @param name
170      * @exception javax.ejb.CreateException
171      */

172     public void ejbCreate() throws javax.ejb.CreateException JavaDoc{
173         try {
174             jndiContext = new InitialContext JavaDoc();
175         } catch (Exception JavaDoc e){
176             throw new CreateException JavaDoc("Can not get the initial context: "+e.getMessage());
177         }
178     }
179     /**
180      * Set the associated session context. The container calls this method
181      * after the instance creation.
182      */

183     public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc,RemoteException JavaDoc {
184         ejbContext = ctx;
185     }
186     /**
187      * A container invokes this method before it ends the life of the session
188      * object. This happens as a result of a client's invoking a remove
189      * operation, or when a container decides to terminate the session object
190      * after a timeout.
191      */

192     public void ejbRemove() throws EJBException JavaDoc,RemoteException JavaDoc {
193     }
194     /**
195      * The activate method is called when the instance is activated
196      * from its "passive" state. The instance should acquire any resource
197      * that it has released earlier in the ejbPassivate() method.
198      */

199     public void ejbActivate() throws EJBException JavaDoc,RemoteException JavaDoc {
200     }
201     /**
202      * The passivate method is called before the instance enters
203      * the "passive" state. The instance should release any resources that
204      * it can re-acquire later in the ejbActivate() method.
205      */

206     public void ejbPassivate() throws EJBException JavaDoc,RemoteException JavaDoc {
207     }
208     //
209
// SessionBean interface methods
210
//==================================
211

212 }
213
Popular Tags