KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > ejb > JDBCStatementTestsConnectionSessionBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.ejb;
23
24 import javax.ejb.SessionBean JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.sql.DataSource JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Statement JavaDoc;
30
31 import javax.ejb.EJBException JavaDoc;
32 import javax.ejb.SessionContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34
35 import org.jboss.logging.Logger;
36 import org.jboss.resource.adapter.jdbc.WrappedConnection;
37 import org.jboss.test.jca.jdbc.TestConnection;
38
39 /**
40  * JDBCStatementTestsConnectionSessionBean
41  *
42  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
43  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 37406 $
45  *
46  * @ejb:bean name="JDBCStatementTestsConnectionSession"
47  * jndi-name="JDBCStatementTestsConnectionSession"
48  * local-jndi-name="JDBCStatementTestsConnectionSessionLocal"
49  * view-type="both"
50  * type="Stateless"
51  */

52 public class JDBCStatementTestsConnectionSessionBean implements SessionBean JavaDoc
53 {
54    /** The serialVersionUID */
55    private static final long serialVersionUID = 1L;
56
57    private static final Logger log = Logger.getLogger(JDBCStatementTestsConnectionSessionBean.class);
58    
59    private SessionContext JavaDoc sessionContext;
60    
61    public JDBCStatementTestsConnectionSessionBean()
62    {
63
64    }
65
66    /**
67     * The <code>testConnectionObtainable</code> method gets
68     * connections from the TestDriver after setting fail to true.
69     * This causes the test sql to throw an exception when the
70     * connection is retrieved from a pool, which closes the
71     * connection, forcing the connectionmanager to get a new one. We
72     * check this by counting how many connections have been closed.
73     *
74     *
75     * @ejb:interface-method
76     * @ejb:transaction type="NotSupported"
77     */

78    public void testConnectionObtainable()
79    {
80       TestConnection tc = null;
81       try
82       {
83          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:StatementTestsConnectionDS");
84          Connection JavaDoc c = ds.getConnection();
85          WrappedConnection wc = (WrappedConnection) c;
86          Connection JavaDoc uc = wc.getUnderlyingConnection();
87          tc = (TestConnection) uc;
88          c.close();
89          tc.setFail(true);
90          int closeCount1 = tc.getClosedCount();
91          c = ds.getConnection();
92          if (closeCount1 == tc.getClosedCount())
93          {
94             throw new EJBException JavaDoc("no connections closed!, closedCount: " + closeCount1);
95          }
96          c.close();
97          for (int i = 0; i < 10; i++)
98          {
99
100             int closeCount = tc.getClosedCount();
101             c = ds.getConnection();
102             if (closeCount == tc.getClosedCount())
103             {
104                throw new EJBException JavaDoc("no connections closed! at iteration: " + i + ", closedCount: " + closeCount);
105             }
106             c.close();
107          }
108
109       }
110       catch (SQLException JavaDoc e)
111       {
112          throw new EJBException JavaDoc(e);
113       }
114       catch (NamingException JavaDoc e)
115       {
116          throw new EJBException JavaDoc(e);
117       }
118       finally
119       {
120          tc.setFail(false);
121       }
122    }
123
124    /**
125     * @ejb:interface-method
126     * @ejb:transaction type="NotSupported"
127     */

128    public void testConfiguredQueryTimeout()
129    {
130       try
131       {
132          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:StatementTestsConnectionDS");
133          Connection JavaDoc c = ds.getConnection();
134          try
135          {
136             Statement JavaDoc s = c.createStatement();
137             s.execute("blah");
138             if (s.getQueryTimeout() != 100)
139                throw new EJBException JavaDoc("Configured query timeout not set");
140          }
141          finally
142          {
143             c.close();
144          }
145       }
146       catch (SQLException JavaDoc e)
147       {
148          throw new EJBException JavaDoc(e);
149       }
150       catch (NamingException JavaDoc e)
151       {
152          throw new EJBException JavaDoc(e);
153       }
154    }
155
156    /**
157     * @ejb:interface-method
158     * @ejb:transaction type="Required"
159     */

160    public void testTransactionQueryTimeout()
161    {
162       try
163       {
164          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:StatementTestsConnectionDS");
165          Connection JavaDoc c = ds.getConnection();
166          try
167          {
168             Statement JavaDoc s = c.createStatement();
169             s.execute("blah");
170             if (s.getQueryTimeout() == 0)
171                throw new EJBException JavaDoc("Tranaction query timeout not set");
172          }
173          finally
174          {
175             c.close();
176          }
177       }
178       catch (SQLException JavaDoc e)
179       {
180          throw new EJBException JavaDoc(e);
181       }
182       catch (NamingException JavaDoc e)
183       {
184          throw new EJBException JavaDoc(e);
185       }
186    }
187
188    /**
189     * @ejb:interface-method
190     * @ejb:transaction type="Required"
191     */

192    public void testTransactionQueryTimeoutMarkedRollback()
193    {
194       try
195       {
196          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:StatementTestsConnectionDS");
197          Connection JavaDoc c = ds.getConnection();
198          try
199          {
200             Statement JavaDoc s = c.createStatement();
201             sessionContext.setRollbackOnly();
202             try
203             {
204                s.execute("blah");
205                throw new EJBException JavaDoc("Should not be here!");
206             }
207             catch (SQLException JavaDoc expected)
208             {
209                log.info("Got expected sql exception", expected);
210             }
211          }
212          finally
213          {
214             c.close();
215          }
216       }
217       catch (SQLException JavaDoc e)
218       {
219          throw new EJBException JavaDoc(e);
220       }
221       catch (NamingException JavaDoc e)
222       {
223          throw new EJBException JavaDoc(e);
224       }
225    }
226
227    /**
228     * @ejb:interface-method
229     * @ejb:transaction type="NotSupported"
230     */

231    public void testLazyAutoCommit()
232    {
233       try
234       {
235          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:NoTxStatementTestsConnectionDS");
236          Connection JavaDoc c = ds.getConnection();
237          try
238          {
239             c.setAutoCommit(false);
240             c.rollback();
241          }
242          finally
243          {
244             c.close();
245          }
246          c = ds.getConnection();
247          try
248          {
249             c.setAutoCommit(false);
250             c.commit();
251          }
252          finally
253          {
254             c.close();
255          }
256          c = ds.getConnection();
257          try
258          {
259             c.setAutoCommit(false);
260             c.rollback(null);
261          }
262          finally
263          {
264             c.close();
265          }
266       }
267       catch (SQLException JavaDoc e)
268       {
269          throw new EJBException JavaDoc(e);
270       }
271       catch (NamingException JavaDoc e)
272       {
273          throw new EJBException JavaDoc(e);
274       }
275    }
276
277    public void ejbCreate()
278    {
279    }
280
281    public void ejbActivate()
282    {
283    }
284
285    public void ejbPassivate()
286    {
287    }
288
289    public void ejbRemove()
290    {
291    }
292
293    public void setSessionContext(SessionContext JavaDoc ctx)
294    {
295       sessionContext = ctx;
296    }
297
298    public void unsetSessionContext()
299    {
300    }
301 }
302
Popular Tags