001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.testbean.bean;
023:
024: import java.rmi.*;
025: import javax.ejb.*;
026: import javax.naming.InitialContext;
027:
028: import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
029:
030: import javax.transaction.UserTransaction;
031: import javax.transaction.Status;
032: import java.sql.Connection;
033: import java.sql.Statement;
034: import java.sql.ResultSet;
035: import javax.sql.DataSource;
036: import java.sql.SQLException;
037:
038: public class BMTStatefulBean implements SessionBean {
039: static org.apache.log4j.Category log = org.apache.log4j.Category
040: .getInstance(BMTStatefulBean.class);
041:
042: private SessionContext sessionContext;
043:
044: public void ejbCreate() throws RemoteException, CreateException {
045: log.debug("BMTStatefulBean.ejbCreate() called");
046: }
047:
048: public void ejbCreate(String caca) throws RemoteException,
049: CreateException {
050: };
051:
052: public void ejbCreate(String caca, String cacaprout)
053: throws RemoteException, CreateException {
054: };
055:
056: public void ejbActivate() throws RemoteException {
057: log.debug("BMTStatefulBean.ejbActivate() called");
058: }
059:
060: public void ejbPassivate() throws RemoteException {
061: log.debug("BMTStatefulBean.ejbPassivate() called");
062: }
063:
064: public void ejbRemove() throws RemoteException {
065: log.debug("BMTStatefulBean.ejbRemove() called");
066: }
067:
068: public void setSessionContext(SessionContext context)
069: throws RemoteException {
070: sessionContext = context;
071: }
072:
073: public String txExists() throws RemoteException {
074: String result = "";
075: try {
076: UserTransaction ut1 = sessionContext.getUserTransaction();
077: result += "Got UserTransaction via sessionContext.getUserTransaction(): "
078: + statusName(ut1.getStatus()) + "\n";
079:
080: UserTransaction ut2 = (UserTransaction) new InitialContext()
081: .lookup("java:comp/UserTransaction");
082: result += "Got UserTransaction via lookup(java:comp/UserTransaction): "
083: + statusName(ut2.getStatus()) + "\n";
084:
085: return result;
086: } catch (Exception e) {
087: log.debug("failed", e);
088: throw new RemoteException(e.getMessage());
089: }
090: }
091:
092: public String txCommit() throws RemoteException {
093: try {
094: UserTransaction tx = sessionContext.getUserTransaction();
095:
096: String result = "Got transaction : "
097: + statusName(tx.getStatus()) + "\n";
098: tx.begin();
099: result += "tx.begin(): " + statusName(tx.getStatus())
100: + "\n";
101: tx.commit();
102: result += "tx.commit(): " + statusName(tx.getStatus())
103: + "\n";
104:
105: return result;
106:
107: } catch (Exception e) {
108: log.debug("failed", e);
109: throw new RemoteException(e.getMessage());
110: }
111:
112: }
113:
114: public String txRollback() throws RemoteException {
115: try {
116: UserTransaction tx = sessionContext.getUserTransaction();
117:
118: String result = "Got transaction : "
119: + statusName(tx.getStatus()) + "\n";
120: tx.begin();
121: result += "tx.begin(): " + statusName(tx.getStatus())
122: + "\n";
123: tx.rollback();
124: result += "tx.rollback(): " + statusName(tx.getStatus())
125: + "\n";
126:
127: return result;
128:
129: } catch (Exception e) {
130: log.debug("failed", e);
131: throw new RemoteException(e.getMessage());
132: }
133: }
134:
135: public String txBegin() throws RemoteException {
136: try {
137: UserTransaction tx = sessionContext.getUserTransaction();
138:
139: tx.begin();
140: return "status: " + statusName(tx.getStatus());
141: } catch (Exception e) {
142: log.debug("failed", e);
143: throw new RemoteException(e.getMessage());
144: }
145:
146: }
147:
148: public String txEnd() throws RemoteException {
149: try {
150: UserTransaction tx = sessionContext.getUserTransaction();
151:
152: tx.commit();
153: return "status: " + statusName(tx.getStatus());
154: } catch (Exception e) {
155: log.debug("failed", e);
156: throw new RemoteException(e.getMessage());
157: }
158:
159: }
160:
161: public void createTable() throws RemoteException {
162: Connection connection = null;
163: Statement stm = null;
164: try {
165: connection = ((DataSource) new InitialContext()
166: .lookup("java:comp/env/jdbc/myDatabase"))
167: .getConnection();
168: stm = connection.createStatement();
169: try {
170: stm
171: .executeUpdate("CREATE TABLE bmttest (field VARCHAR(256))");
172: } catch (SQLException e) {
173: // ignore, table probably already exists
174: } finally {
175: JDBCUtil.safeClose(stm);
176: }
177:
178: stm = connection.createStatement();
179: stm
180: .executeUpdate("INSERT INTO bmttest VALUES ('initial value')");
181: } catch (Exception e) {
182: log.debug("failed", e);
183: throw new RemoteException(e.getMessage());
184: } finally {
185: JDBCUtil.safeClose(stm);
186: JDBCUtil.safeClose(connection);
187: }
188: }
189:
190: public void dropTable() throws RemoteException {
191: Connection connection = null;
192: Statement stm = null;
193: try {
194: connection = ((DataSource) new InitialContext()
195: .lookup("java:comp/env/jdbc/myDatabase"))
196: .getConnection();
197: stm = connection.createStatement();
198: stm.executeUpdate("DROP TABLE bmttest");
199: } catch (Exception e) {
200: log.debug("failed", e);
201: throw new RemoteException(e.getMessage());
202: } finally {
203: JDBCUtil.safeClose(stm);
204: JDBCUtil.safeClose(connection);
205: }
206: }
207:
208: public String dbCommit() throws RemoteException {
209: try {
210: UserTransaction tx = sessionContext.getUserTransaction();
211:
212: tx.begin();
213:
214: Connection connection = null;
215: Statement stm = null;
216: try {
217: connection = ((DataSource) new InitialContext()
218: .lookup("java:comp/env/jdbc/myDatabase"))
219: .getConnection();
220: stm = connection.createStatement();
221: stm
222: .executeUpdate("UPDATE bmttest SET field = 'updated via dbCommit'");
223: } finally {
224: JDBCUtil.safeClose(stm);
225: JDBCUtil.safeClose(connection);
226: }
227:
228: tx.commit();
229: return statusName(tx.getStatus());
230:
231: } catch (Exception e) {
232: log.debug("failed", e);
233: throw new RemoteException(e.getMessage());
234: }
235: }
236:
237: public String dbRollback() throws RemoteException {
238: try {
239: UserTransaction tx = sessionContext.getUserTransaction();
240:
241: tx.begin();
242:
243: Connection connection = null;
244: Statement stm = null;
245: try {
246: connection = ((DataSource) new InitialContext()
247: .lookup("java:comp/env/jdbc/myDatabase"))
248: .getConnection();
249: stm = connection.createStatement();
250: stm
251: .executeUpdate("UPDATE bmttest SET field = 'updated via dbRollback'");
252: } finally {
253: JDBCUtil.safeClose(stm);
254: JDBCUtil.safeClose(connection);
255: }
256:
257: tx.rollback();
258: return statusName(tx.getStatus());
259:
260: } catch (Exception e) {
261: log.debug("failed", e);
262: throw new RemoteException(e.getMessage());
263: }
264: }
265:
266: public String getDbField() throws RemoteException {
267: Connection connection = null;
268: Statement stm = null;
269: ResultSet rs = null;
270: try {
271: connection = ((DataSource) new InitialContext()
272: .lookup("java:comp/env/jdbc/myDatabase"))
273: .getConnection();
274: stm = connection.createStatement();
275: rs = stm.executeQuery("SELECT field FROM bmttest ");
276: String result = "not found";
277: if (rs.next()) {
278: result = rs.getString(1);
279: }
280: return result;
281: } catch (Exception e) {
282: log.debug("failed", e);
283: throw new RemoteException(e.getMessage());
284: } finally {
285: JDBCUtil.safeClose(rs);
286: JDBCUtil.safeClose(stm);
287: JDBCUtil.safeClose(connection);
288: }
289: }
290:
291: private String statusName(int s) {
292: switch (s) {
293: case Status.STATUS_ACTIVE:
294: return "STATUS_ACTIVE";
295: case Status.STATUS_COMMITTED:
296: return "STATUS_COMMITED";
297: case Status.STATUS_COMMITTING:
298: return "STATUS_COMMITTING";
299: case Status.STATUS_MARKED_ROLLBACK:
300: return "STATUS_MARKED_ROLLBACK";
301: case Status.STATUS_NO_TRANSACTION:
302: return "STATUS_NO_TRANSACTION";
303: case Status.STATUS_PREPARED:
304: return "STATUS_PREPARED";
305: case Status.STATUS_PREPARING:
306: return "STATUS_PREPARING";
307: case Status.STATUS_ROLLEDBACK:
308: return "STATUS_ROLLEDBACK";
309: case Status.STATUS_ROLLING_BACK:
310: return "STATUS_ROLLING_BACK";
311: case Status.STATUS_UNKNOWN:
312: return "STATUS_UNKNOWN";
313: }
314: return "REALLY_UNKNOWN";
315: }
316: }
|