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 javax.ejb.SessionBean;
025: import javax.naming.InitialContext;
026: import javax.sql.DataSource;
027: import java.sql.Connection;
028: import java.sql.SQLException;
029: import java.sql.Statement;
030:
031: import javax.ejb.EJBException;
032: import javax.ejb.SessionContext;
033: import javax.naming.NamingException;
034:
035: import org.jboss.logging.Logger;
036: import org.jboss.resource.adapter.jdbc.WrappedConnection;
037: import org.jboss.test.jca.jdbc.TestConnection;
038:
039: /**
040: * JDBCStatementTestsConnectionSessionBean
041: *
042: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
043: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
044: * @version $Revision: 57211 $
045: *
046: * @ejb:bean name="JDBCStatementTestsConnectionSession"
047: * jndi-name="JDBCStatementTestsConnectionSession"
048: * local-jndi-name="JDBCStatementTestsConnectionSessionLocal"
049: * view-type="both"
050: * type="Stateless"
051: */
052: public class JDBCStatementTestsConnectionSessionBean implements
053: SessionBean {
054: /** The serialVersionUID */
055: private static final long serialVersionUID = 1L;
056:
057: private static final Logger log = Logger
058: .getLogger(JDBCStatementTestsConnectionSessionBean.class);
059:
060: private SessionContext sessionContext;
061:
062: public JDBCStatementTestsConnectionSessionBean() {
063:
064: }
065:
066: /**
067: * The <code>testConnectionObtainable</code> method gets
068: * connections from the TestDriver after setting fail to true.
069: * This causes the test sql to throw an exception when the
070: * connection is retrieved from a pool, which closes the
071: * connection, forcing the connectionmanager to get a new one. We
072: * check this by counting how many connections have been closed.
073: *
074: *
075: * @ejb:interface-method
076: * @ejb:transaction type="NotSupported"
077: */
078: public void testConnectionObtainable() {
079: TestConnection tc = null;
080: try {
081: DataSource ds = (DataSource) new InitialContext()
082: .lookup("java:StatementTestsConnectionDS");
083: Connection c = ds.getConnection();
084: WrappedConnection wc = (WrappedConnection) c;
085: Connection uc = wc.getUnderlyingConnection();
086: tc = (TestConnection) uc;
087: c.close();
088: tc.setFail(true);
089: int closeCount1 = tc.getClosedCount();
090: c = ds.getConnection();
091: if (closeCount1 == tc.getClosedCount()) {
092: throw new EJBException(
093: "no connections closed!, closedCount: "
094: + closeCount1);
095: }
096: c.close();
097: for (int i = 0; i < 10; i++) {
098:
099: int closeCount = tc.getClosedCount();
100: c = ds.getConnection();
101: if (closeCount == tc.getClosedCount()) {
102: throw new EJBException(
103: "no connections closed! at iteration: " + i
104: + ", closedCount: " + closeCount);
105: }
106: c.close();
107: }
108:
109: } catch (SQLException e) {
110: throw new EJBException(e);
111: } catch (NamingException e) {
112: throw new EJBException(e);
113: } finally {
114: tc.setFail(false);
115: }
116: }
117:
118: /**
119: * @ejb:interface-method
120: * @ejb:transaction type="NotSupported"
121: */
122: public void testConfiguredQueryTimeout() {
123: try {
124: DataSource ds = (DataSource) new InitialContext()
125: .lookup("java:StatementTestsConnectionDS");
126: Connection c = ds.getConnection();
127: try {
128: Statement s = c.createStatement();
129: s.execute("blah");
130: if (s.getQueryTimeout() != 100)
131: throw new EJBException(
132: "Configured query timeout not set");
133: } finally {
134: c.close();
135: }
136: } catch (SQLException e) {
137: throw new EJBException(e);
138: } catch (NamingException e) {
139: throw new EJBException(e);
140: }
141: }
142:
143: /**
144: * @ejb:interface-method
145: * @ejb:transaction type="Required"
146: */
147: public void testTransactionQueryTimeout() {
148: try {
149: DataSource ds = (DataSource) new InitialContext()
150: .lookup("java:StatementTestsConnectionDS");
151: Connection c = ds.getConnection();
152: try {
153: Statement s = c.createStatement();
154: s.execute("blah");
155: if (s.getQueryTimeout() == 0)
156: throw new EJBException(
157: "Tranaction query timeout not set");
158: } finally {
159: c.close();
160: }
161: } catch (SQLException e) {
162: throw new EJBException(e);
163: } catch (NamingException e) {
164: throw new EJBException(e);
165: }
166: }
167:
168: /**
169: * @ejb:interface-method
170: * @ejb:transaction type="Required"
171: */
172: public void testTransactionQueryTimeoutMarkedRollback() {
173: try {
174: DataSource ds = (DataSource) new InitialContext()
175: .lookup("java:StatementTestsConnectionDS");
176: Connection c = ds.getConnection();
177: try {
178: Statement s = c.createStatement();
179: sessionContext.setRollbackOnly();
180: try {
181: s.execute("blah");
182: throw new EJBException("Should not be here!");
183: } catch (SQLException expected) {
184: log.info("Got expected sql exception", expected);
185: }
186: } finally {
187: c.close();
188: }
189: } catch (SQLException e) {
190: throw new EJBException(e);
191: } catch (NamingException e) {
192: throw new EJBException(e);
193: }
194: }
195:
196: /**
197: * @ejb:interface-method
198: * @ejb:transaction type="NotSupported"
199: */
200: public void testLazyAutoCommit() {
201: try {
202: DataSource ds = (DataSource) new InitialContext()
203: .lookup("java:NoTxStatementTestsConnectionDS");
204: Connection c = ds.getConnection();
205: try {
206: c.setAutoCommit(false);
207: c.rollback();
208: } finally {
209: c.close();
210: }
211: c = ds.getConnection();
212: try {
213: c.setAutoCommit(false);
214: c.commit();
215: } finally {
216: c.close();
217: }
218: c = ds.getConnection();
219: try {
220: c.setAutoCommit(false);
221: c.rollback(null);
222: } finally {
223: c.close();
224: }
225: } catch (SQLException e) {
226: throw new EJBException(e);
227: } catch (NamingException e) {
228: throw new EJBException(e);
229: }
230: }
231:
232: public void ejbCreate() {
233: }
234:
235: public void ejbActivate() {
236: }
237:
238: public void ejbPassivate() {
239: }
240:
241: public void ejbRemove() {
242: }
243:
244: public void setSessionContext(SessionContext ctx) {
245: sessionContext = ctx;
246: }
247:
248: public void unsetSessionContext() {
249: }
250: }
|