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