001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.stateful;
017:
018: import java.rmi.RemoteException;
019: import java.sql.Connection;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022:
023: import javax.ejb.CreateException;
024: import javax.ejb.EJBException;
025: import javax.ejb.SessionContext;
026: import javax.naming.InitialContext;
027: import javax.sql.DataSource;
028: import javax.transaction.RollbackException;
029: import javax.transaction.UserTransaction;
030:
031: import org.apache.openejb.test.object.Account;
032: import org.apache.openejb.test.object.Transaction;
033:
034: /**
035: *
036: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
037: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
038: */
039: public class BeanTxStatefulBean implements javax.ejb.SessionBean {
040:
041: private String name;
042: private SessionContext ejbContext;
043: private InitialContext jndiContext;
044: public final String jndiDatabaseEntry = "jdbc/stateful/beanManagedTransaction/database";
045:
046: //=============================
047: // Home interface methods
048: //
049: /**
050: * Maps to BasicStatefulHome.create
051: *
052: * @param name
053: * @exception javax.ejb.CreateException
054: * @see BasicStatefulHome#createObject
055: */
056: public void ejbCreate(String name) throws javax.ejb.CreateException {
057: this .name = name;
058: try {
059: jndiContext = new InitialContext();
060: } catch (Exception e) {
061: throw new CreateException(
062: "Can not get the initial context: "
063: + e.getMessage());
064: }
065: }
066:
067: //
068: // Home interface methods
069: //=============================
070:
071: //=============================
072: // Remote interface methods
073: //
074:
075: /**
076: * Maps to BasicStatefulObject.businessMethod
077: *
078: * @return
079: * @see BasicStatefulObject#businessMethod
080: */
081: public Transaction getUserTransaction() throws RemoteException {
082:
083: UserTransaction ut = null;
084: try {
085: ut = ejbContext.getUserTransaction();
086: } catch (IllegalStateException ise) {
087: throw new RemoteException(ise.getMessage());
088: }
089: if (ut == null)
090: return null;
091: return new Transaction(ut);
092: }
093:
094: public Transaction jndiUserTransaction() throws RemoteException {
095: UserTransaction ut = null;
096: try {
097: ut = (UserTransaction) jndiContext
098: .lookup("java:comp/UserTransaction");
099: } catch (Exception e) {
100: throw new RemoteException(e.getMessage());
101: }
102: if (ut == null)
103: return null;
104: return new Transaction(ut);
105: }
106:
107: public void openAccount(Account acct, Boolean rollback)
108: throws RemoteException, RollbackException {
109:
110: try {
111:
112: DataSource ds = (DataSource) javax.rmi.PortableRemoteObject
113: .narrow(jndiContext
114: .lookup("java:comp/env/datasource"),
115: DataSource.class);
116: Connection con = ds.getConnection();
117:
118: try {
119: UserTransaction ut = ejbContext.getUserTransaction();
120: /*[1] Begin the transaction */
121: ut.begin();
122:
123: /*[2] Update the table */
124: PreparedStatement stmt = con
125: .prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
126: try {
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: } finally {
133: stmt.close();
134: }
135:
136: /*[3] Commit or Rollback the transaction */
137: if (rollback.booleanValue())
138: ut.setRollbackOnly();
139:
140: /*[4] Commit or Rollback the transaction */
141: ut.commit();
142:
143: } finally {
144: con.close();
145: }
146: } catch (RollbackException re) {
147: throw re;
148: } catch (Exception e) {
149: e.printStackTrace();
150: throw new RemoteException("[Bean] "
151: + e.getClass().getName() + " : " + e.getMessage());
152: }
153: }
154:
155: public Account retreiveAccount(String ssn) throws RemoteException {
156: Account acct = new Account();
157: try {
158: DataSource ds = (DataSource) javax.rmi.PortableRemoteObject
159: .narrow(jndiContext
160: .lookup("java:comp/env/datasource"),
161: DataSource.class);
162: Connection con = ds.getConnection();
163:
164: try {
165: PreparedStatement stmt = con
166: .prepareStatement("select * from Account where SSN = ?");
167: try {
168: stmt.setString(1, ssn);
169: ResultSet rs = stmt.executeQuery();
170: if (!rs.next())
171: return null;
172:
173: acct.setSsn(rs.getString(1));
174: acct.setFirstName(rs.getString(2));
175: acct.setLastName(rs.getString(3));
176: acct.setBalance(rs.getInt(4));
177: } finally {
178: stmt.close();
179: }
180: } finally {
181: con.close();
182: }
183: } catch (Exception e) {
184: e.printStackTrace();
185: throw new RemoteException("[Bean] "
186: + e.getClass().getName() + " : " + e.getMessage());
187: }
188: return acct;
189: }
190:
191: public String remove(String arg) {
192: return arg;
193: }
194:
195: //
196: // Remote interface methods
197: //=============================
198:
199: //=================================
200: // SessionBean interface methods
201: //
202: /**
203: * Set the associated session context. The container calls this method
204: * after the instance creation.
205: */
206: public void setSessionContext(SessionContext ctx)
207: throws EJBException, RemoteException {
208: ejbContext = ctx;
209: }
210:
211: /**
212: * A container invokes this method before it ends the life of the session
213: * object. This happens as a result of a client's invoking a remove
214: * operation, or when a container decides to terminate the session object
215: * after a timeout.
216: */
217: public void ejbRemove() throws EJBException, RemoteException {
218: }
219:
220: /**
221: * The activate method is called when the instance is activated
222: * from its "passive" state. The instance should acquire any resource
223: * that it has released earlier in the ejbPassivate() method.
224: */
225: public void ejbActivate() throws EJBException, RemoteException {
226: }
227:
228: /**
229: * The passivate method is called before the instance enters
230: * the "passive" state. The instance should release any resources that
231: * it can re-acquire later in the ejbActivate() method.
232: */
233: public void ejbPassivate() throws EJBException, RemoteException {
234: }
235: //
236: // SessionBean interface methods
237: //==================================
238:
239: }
|