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.stateless;
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 BeanTxStatelessBean implements javax.ejb.SessionBean {
040:
041: private String name;
042: private SessionContext ejbContext;
043: private InitialContext jndiContext;
044: public final String jndiDatabaseEntry = "jdbc/stateless/beanManagedTransaction/database";
045:
046: //=============================
047: // Home interface methods
048: //
049:
050: //
051: // Home interface methods
052: //=============================
053:
054: //=============================
055: // Remote interface methods
056: //
057:
058: public Transaction getUserTransaction() throws RemoteException {
059:
060: UserTransaction ut = null;
061: try {
062: ut = ejbContext.getUserTransaction();
063: } catch (IllegalStateException ise) {
064: throw new RemoteException(ise.getMessage());
065: }
066: if (ut == null)
067: return null;
068: return new Transaction(ut);
069: }
070:
071: public Transaction jndiUserTransaction() throws RemoteException {
072: UserTransaction ut = null;
073: try {
074: ut = (UserTransaction) jndiContext
075: .lookup("java:comp/UserTransaction");
076: } catch (Exception e) {
077: throw new RemoteException(e.getMessage());
078: }
079: if (ut == null)
080: return null;
081: return new Transaction(ut);
082: }
083:
084: public void openAccount(Account acct, Boolean rollback)
085: throws RemoteException, RollbackException {
086:
087: try {
088: DataSource ds = (DataSource) javax.rmi.PortableRemoteObject
089: .narrow(jndiContext
090: .lookup("java:comp/env/database"),
091: DataSource.class);
092: Connection con = ds.getConnection();
093:
094: try {
095: UserTransaction ut = ejbContext.getUserTransaction();
096: /*[1] Begin the transaction */
097: ut.begin();
098:
099: /*[2] Update the table */
100: PreparedStatement stmt = con
101: .prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
102: try {
103: stmt.setString(1, acct.getSsn());
104: stmt.setString(2, acct.getFirstName());
105: stmt.setString(3, acct.getLastName());
106: stmt.setInt(4, acct.getBalance());
107: stmt.executeUpdate();
108: } finally {
109: stmt.close();
110: }
111:
112: /*[3] Commit or Rollback the transaction */
113: if (rollback.booleanValue())
114: ut.setRollbackOnly();
115:
116: /*[4] Commit or Rollback the transaction */
117: ut.commit();
118: } finally {
119: con.close();
120: }
121: } catch (RollbackException re) {
122: throw re;
123: } catch (Exception e) {
124: e.printStackTrace();
125: throw new RemoteException("[Bean] "
126: + e.getClass().getName() + " : " + e.getMessage());
127: }
128: }
129:
130: public Account retreiveAccount(String ssn) throws RemoteException {
131: Account acct = new Account();
132: try {
133: DataSource ds = (DataSource) javax.rmi.PortableRemoteObject
134: .narrow(jndiContext
135: .lookup("java:comp/env/database"),
136: DataSource.class);
137: Connection con = ds.getConnection();
138:
139: try {
140: PreparedStatement stmt = con
141: .prepareStatement("select * from Account where SSN = ?");
142: try {
143: stmt.setString(1, ssn);
144: ResultSet rs = stmt.executeQuery();
145: if (!rs.next())
146: return null;
147:
148: acct.setSsn(rs.getString(1));
149: acct.setFirstName(rs.getString(2));
150: acct.setLastName(rs.getString(3));
151: acct.setBalance(rs.getInt(4));
152: } finally {
153: stmt.close();
154: }
155: } finally {
156: con.close();
157: }
158: } catch (Exception e) {
159: e.printStackTrace();
160: throw new RemoteException("[Bean] "
161: + e.getClass().getName() + " : " + e.getMessage());
162: }
163: return acct;
164: }
165:
166: //
167: // Remote interface methods
168: //=============================
169:
170: //=================================
171: // SessionBean interface methods
172: //
173: /**
174: *
175: * @param name
176: * @exception javax.ejb.CreateException
177: */
178: public void ejbCreate() throws javax.ejb.CreateException {
179: try {
180: jndiContext = new InitialContext();
181: } catch (Exception e) {
182: throw new CreateException(
183: "Can not get the initial context: "
184: + e.getMessage());
185: }
186: }
187:
188: /**
189: * Set the associated session context. The container calls this method
190: * after the instance creation.
191: */
192: public void setSessionContext(SessionContext ctx)
193: throws EJBException, RemoteException {
194: ejbContext = ctx;
195: }
196:
197: /**
198: * A container invokes this method before it ends the life of the session
199: * object. This happens as a result of a client's invoking a remove
200: * operation, or when a container decides to terminate the session object
201: * after a timeout.
202: */
203: public void ejbRemove() throws EJBException, RemoteException {
204: }
205:
206: /**
207: * The activate method is called when the instance is activated
208: * from its "passive" state. The instance should acquire any resource
209: * that it has released earlier in the ejbPassivate() method.
210: */
211: public void ejbActivate() throws EJBException, RemoteException {
212: }
213:
214: /**
215: * The passivate method is called before the instance enters
216: * the "passive" state. The instance should release any resources that
217: * it can re-acquire later in the ejbActivate() method.
218: */
219: public void ejbPassivate() throws EJBException, RemoteException {
220: }
221:
222: //
223: // SessionBean interface methods
224: //==================================
225:
226: public String remove(String arg) {
227: return arg;
228: }
229: }
|