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.jca.ejb;
023:
024: import java.sql.Connection;
025: import java.sql.ResultSet;
026: import java.sql.Statement;
027:
028: import javax.ejb.EJBException;
029: import javax.ejb.SessionBean;
030: import javax.ejb.SessionContext;
031: import javax.naming.InitialContext;
032: import javax.sql.DataSource;
033: import javax.transaction.UserTransaction;
034:
035: import org.jboss.logging.Logger;
036: import org.jboss.resource.adapter.jdbc.WrappedConnection;
037:
038: /**
039: * LocalWrapperCleanupTestSessionBean.java
040: *
041: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
042: * @version $Revision: 57211 $
043: *
044: * @ejb:bean name="LocalWrapperCleanupTestSession"
045: * jndi-name="LocalWrapperCleanupTestSession"
046: * view-type="remote"
047: * type="Stateless"
048: *
049: */
050: public class LocalWrapperCleanupTestSessionBean implements SessionBean {
051: /** The serialVersionUID */
052: private static final long serialVersionUID = 1L;
053:
054: /** The log */
055: private static final Logger log = Logger
056: .getLogger(LocalWrapperCleanupTestSessionBean.class);
057:
058: public void testAutoCommitInReturnedConnection() {
059: Connection c = null;
060: try {
061: DataSource ds = (DataSource) new InitialContext()
062: .lookup("java:/SingleConnectionDS");
063: c = ds.getConnection();
064: if (c.getAutoCommit() == false) {
065: throw new EJBException(
066: "Initial autocommit state false!");
067: }
068: c.setAutoCommit(false);
069: c.commit();
070: c.close();
071: c = null;
072: c = ds.getConnection();
073: if (c.getAutoCommit() == false) {
074: throw new EJBException(
075: "Returned and reaccessed autocommit state false!");
076: }
077: c.close();
078: c = null;
079: } catch (EJBException e) {
080: throw e;
081: } catch (Exception e) {
082: log.error("Error", e);
083: throw new EJBException("Untested problem in test: " + e);
084: } finally {
085: try {
086: if (c != null)
087: c.close();
088: } catch (Throwable ignored) {
089: }
090: }
091: }
092:
093: public void testAutoCommit() {
094: Connection c1 = null;
095: Connection c2 = null;
096: try {
097: DataSource ds = (DataSource) new InitialContext()
098: .lookup("java:/DefaultDS");
099: c1 = ds.getConnection();
100: Connection uc1 = ((WrappedConnection) c1)
101: .getUnderlyingConnection();
102: if (c1.getAutoCommit() == false) {
103: throw new EJBException(
104: "Initial autocommit state false!");
105: }
106:
107: c2 = ds.getConnection();
108: if (c2.getAutoCommit() == false) {
109: throw new EJBException(
110: "Initial autocommit state false!");
111: }
112: Statement s1 = c1.createStatement();
113: Statement s2 = c2.createStatement();
114: s1.execute("create table autocommittest (id integer)");
115: try {
116: s1.execute("insert into autocommittest values (1)");
117: uc1.rollback();
118: ResultSet rs2 = s2
119: .executeQuery("select * from autocommittest where id = 1");
120: if (!rs2.next()) {
121: throw new EJBException(
122: "Row not visible to other connection, autocommit failed");
123: }
124: rs2.close();
125:
126: } finally {
127: s1.execute("drop table autocommittest");
128: }
129: } catch (EJBException e) {
130: throw e;
131: } catch (Exception e) {
132: log.error("Error", e);
133: throw new EJBException("Untested problem in test: " + e);
134: } finally {
135: try {
136: if (c1 != null)
137: c1.close();
138: } catch (Throwable ignored) {
139: }
140: try {
141: if (c2 != null)
142: c2.close();
143: } catch (Throwable ignored) {
144: }
145: }
146: }
147:
148: public void testAutoCommitOffInUserTx() {
149: Connection c1 = null;
150: try {
151: DataSource ds = (DataSource) new InitialContext()
152: .lookup("java:/DefaultDS");
153: c1 = ds.getConnection();
154: Connection uc1 = ((WrappedConnection) c1)
155: .getUnderlyingConnection();
156: if (c1.getAutoCommit() == false) {
157: throw new EJBException(
158: "Initial autocommit state false!");
159: }
160:
161: Statement s1 = c1.createStatement();
162: s1.execute("create table autocommittest (id integer)");
163: try {
164: UserTransaction ut = (UserTransaction) new InitialContext()
165: .lookup("UserTransaction");
166: ut.begin();
167: s1.execute("insert into autocommittest values (1)");
168: if (uc1.getAutoCommit()) {
169: throw new EJBException(
170: "Underlying autocommit is true in user tx!");
171: }
172:
173: ut.rollback();
174: ResultSet rs1 = s1
175: .executeQuery("select * from autocommittest where id = 1");
176: if (rs1.next()) {
177: throw new EJBException(
178: "Row committed, autocommit still on!");
179: }
180: rs1.close();
181:
182: } finally {
183: s1.execute("drop table autocommittest");
184: }
185: } catch (EJBException e) {
186: throw e;
187: } catch (Exception e) {
188: log.error("Error", e);
189: throw new EJBException("Untested problem in test: " + e);
190: } finally {
191: try {
192: if (c1 != null)
193: c1.close();
194: } catch (Throwable ignored) {
195: }
196: }
197: }
198:
199: public void testAutoCommitOffInUserTx2() {
200: try {
201: createTable();
202: UserTransaction ut = (UserTransaction) new InitialContext()
203: .lookup("UserTransaction");
204: ut.begin();
205: insertAndCheckAutoCommit();
206: ut.rollback();
207: } catch (EJBException e) {
208: throw e;
209: } catch (Exception e) {
210: log.error("Error", e);
211: throw new EJBException("Untested problem in test: " + e);
212: } finally {
213: checkRowAndDropTable();
214: }
215:
216: }
217:
218: public void testReadOnly() {
219: Connection c1 = null;
220: try {
221: DataSource ds = (DataSource) new InitialContext()
222: .lookup("java:/DefaultDS");
223: c1 = ds.getConnection();
224: Connection uc1 = ((WrappedConnection) c1)
225: .getUnderlyingConnection();
226:
227: if (uc1.isReadOnly() == true)
228: throw new EJBException(
229: "Initial underlying readonly true!");
230: if (c1.isReadOnly() == true)
231: throw new EJBException("Initial readonly true!");
232:
233: c1.setReadOnly(true);
234:
235: if (uc1.isReadOnly() == true)
236: throw new EJBException("Read Only should be lazy!");
237: if (c1.isReadOnly() == false)
238: throw new EJBException("Changed readonly false!");
239:
240: c1.createStatement();
241:
242: if (uc1.isReadOnly() == false)
243: throw new EJBException("Lazy read only failed!");
244: if (c1.isReadOnly() == false)
245: throw new EJBException(
246: "Read only changed unexpectedly!");
247: } catch (EJBException e) {
248: throw e;
249: } catch (Exception e) {
250: log.error("Error", e);
251: throw new EJBException("Untested problem in test: " + e);
252: } finally {
253: try {
254: if (c1 != null)
255: c1.close();
256: } catch (Throwable ignored) {
257: }
258: }
259: }
260:
261: public void createTable() {
262: try {
263: DataSource ds = (DataSource) new InitialContext()
264: .lookup("java:/DefaultDS");
265: Connection c1 = ds.getConnection();
266: try {
267: if (c1.getAutoCommit() == false)
268: throw new EJBException(
269: "Initial autocommit state false!");
270: Statement s1 = c1.createStatement();
271: s1.execute("create table autocommittest (id integer)");
272: } finally {
273: c1.close();
274: }
275: } catch (EJBException e) {
276: throw e;
277: } catch (Exception e) {
278: log.error("Error", e);
279: throw new EJBException("Untested problem in test: " + e);
280: }
281: }
282:
283: public void insertAndCheckAutoCommit() {
284: try {
285: DataSource ds = (DataSource) new InitialContext()
286: .lookup("java:/DefaultDS");
287: Connection c1 = ds.getConnection();
288: Connection uc1 = ((WrappedConnection) c1)
289: .getUnderlyingConnection();
290: try {
291: Statement s1 = c1.createStatement();
292: s1.execute("insert into autocommittest values (1)");
293: if (uc1.getAutoCommit())
294: throw new EJBException(
295: "Underlying autocommit is true in user tx!");
296: } finally {
297: c1.close();
298: }
299: } catch (EJBException e) {
300: throw e;
301: } catch (Exception e) {
302: log.error("Error", e);
303: throw new EJBException("Untested problem in test: " + e);
304: }
305:
306: }
307:
308: public void testManualNoCommitRollback() {
309: try {
310: DataSource ds = (DataSource) new InitialContext()
311: .lookup("java:/DefaultDS");
312: Connection c1 = ds.getConnection();
313: try {
314: c1.setAutoCommit(false);
315: Statement s1 = c1.createStatement();
316: s1.execute("insert into autocommittest values (1)");
317: } finally {
318: c1.close();
319: }
320: } catch (EJBException e) {
321: throw e;
322: } catch (Exception e) {
323: log.error("Error", e);
324: throw new EJBException("Untested problem in test: " + e);
325: }
326: }
327:
328: public void testManualSecondNoCommitRollback() {
329: try {
330: DataSource ds = (DataSource) new InitialContext()
331: .lookup("java:/DefaultDS");
332: Connection c1 = ds.getConnection();
333: try {
334: c1.setAutoCommit(false);
335: Statement s1 = c1.createStatement();
336: s1.execute("insert into autocommittest values (0)");
337: c1.commit();
338: s1.execute("insert into autocommittest values (1)");
339: } finally {
340: c1.close();
341: }
342: } catch (EJBException e) {
343: throw e;
344: } catch (Exception e) {
345: log.error("Error", e);
346: throw new EJBException("Untested problem in test: " + e);
347: }
348: }
349:
350: public void checkRowAndDropTable() {
351: try {
352: DataSource ds = (DataSource) new InitialContext()
353: .lookup("java:/DefaultDS");
354: Connection c1 = ds.getConnection();
355: try {
356: if (c1.getAutoCommit() == false)
357: throw new EJBException(
358: "Initial autocommit state false!");
359:
360: Statement s1 = c1.createStatement();
361: ResultSet rs1 = s1
362: .executeQuery("select * from autocommittest where id = 1");
363: if (rs1.next())
364: throw new EJBException(
365: "Row committed, autocommit still on!");
366: } finally {
367: try {
368: Statement s1 = c1.createStatement();
369: s1.execute("drop table autocommittest");
370: } catch (Throwable t) {
371: log.warn("Ignored", t);
372: }
373: c1.close();
374: }
375:
376: } catch (EJBException e) {
377: throw e;
378: } catch (Exception e) {
379: log.error("Error", e);
380: throw new EJBException("Untested problem in test: " + e);
381: }
382:
383: }
384:
385: public void addRowCheckAndDropTable() {
386: try {
387: DataSource ds = (DataSource) new InitialContext()
388: .lookup("java:/DefaultDS");
389: Connection c1 = ds.getConnection();
390: try {
391: if (c1.getAutoCommit() == false)
392: throw new EJBException(
393: "Initial autocommit state false!");
394: c1.setAutoCommit(false);
395:
396: Statement s1 = c1.createStatement();
397: s1.execute("insert into autocommittest values (2)");
398: c1.commit();
399:
400: ResultSet rs1 = s1
401: .executeQuery("select * from autocommittest where id = 1");
402: if (rs1.next())
403: throw new EJBException(
404: "Row committed, didn't rollback!");
405: } finally {
406: try {
407: Statement s1 = c1.createStatement();
408: s1.execute("drop table autocommittest");
409: } catch (Throwable ignored) {
410: }
411: c1.close();
412: }
413:
414: } catch (EJBException e) {
415: throw e;
416: } catch (Exception e) {
417: log.error("Error", e);
418: throw new EJBException("Untested problem in test: " + e);
419: }
420: }
421:
422: public void ejbCreate() {
423: }
424:
425: public void ejbActivate() {
426: }
427:
428: public void ejbPassivate() {
429: }
430:
431: public void ejbRemove() {
432: }
433:
434: public void setSessionContext(SessionContext ctx) {
435: }
436:
437: public void unsetSessionContext() {
438: }
439: }
|