KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.sql.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.Statement JavaDoc;
27
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.SessionBean JavaDoc;
30 import javax.ejb.SessionContext JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.sql.DataSource JavaDoc;
33 import javax.transaction.UserTransaction JavaDoc;
34
35 import org.jboss.logging.Logger;
36 import org.jboss.resource.adapter.jdbc.WrappedConnection;
37
38 /**
39  * LocalWrapperCleanupTestSessionBean.java
40  *
41  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
42  * @version $Revision: 41624 $
43  *
44  * @ejb:bean name="LocalWrapperCleanupTestSession"
45  * jndi-name="LocalWrapperCleanupTestSession"
46  * view-type="remote"
47  * type="Stateless"
48  *
49  */

50 public class LocalWrapperCleanupTestSessionBean implements SessionBean JavaDoc
51 {
52    /** The serialVersionUID */
53    private static final long serialVersionUID = 1L;
54
55    /** The log */
56    private static final Logger log = Logger.getLogger(LocalWrapperCleanupTestSessionBean.class);
57    
58    public void testAutoCommitInReturnedConnection()
59    {
60       Connection JavaDoc c = null;
61       try
62       {
63          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/SingleConnectionDS");
64          c = ds.getConnection();
65          if (c.getAutoCommit() == false)
66          {
67             throw new EJBException JavaDoc("Initial autocommit state false!");
68          }
69          c.setAutoCommit(false);
70          c.commit();
71          c.close();
72          c = null;
73          c = ds.getConnection();
74          if (c.getAutoCommit() == false)
75          {
76             throw new EJBException JavaDoc("Returned and reaccessed autocommit state false!");
77          }
78          c.close();
79          c = null;
80       }
81       catch (EJBException JavaDoc e)
82       {
83          throw e;
84       }
85       catch (Exception JavaDoc e)
86       {
87          log.error("Error", e);
88          throw new EJBException JavaDoc("Untested problem in test: " + e);
89       }
90       finally
91       {
92          try
93          {
94             if (c != null)
95                c.close();
96          }
97          catch (Throwable JavaDoc ignored)
98          {
99          }
100       }
101    }
102
103    public void testAutoCommit()
104    {
105       Connection JavaDoc c1 = null;
106       Connection JavaDoc c2 = null;
107       try
108       {
109          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
110          c1 = ds.getConnection();
111          Connection JavaDoc uc1 = ((WrappedConnection) c1).getUnderlyingConnection();
112          if (c1.getAutoCommit() == false)
113          {
114             throw new EJBException JavaDoc("Initial autocommit state false!");
115          }
116
117          c2 = ds.getConnection();
118          if (c2.getAutoCommit() == false)
119          {
120             throw new EJBException JavaDoc("Initial autocommit state false!");
121          }
122          Statement JavaDoc s1 = c1.createStatement();
123          Statement JavaDoc s2 = c2.createStatement();
124          s1.execute("create table autocommittest (id integer)");
125          try
126          {
127             s1.execute("insert into autocommittest values (1)");
128             uc1.rollback();
129             ResultSet JavaDoc rs2 = s2.executeQuery("select * from autocommittest where id = 1");
130             if (!rs2.next())
131             {
132                throw new EJBException JavaDoc("Row not visible to other connection, autocommit failed");
133             }
134             rs2.close();
135
136          }
137          finally
138          {
139             s1.execute("drop table autocommittest");
140          }
141       }
142       catch (EJBException JavaDoc e)
143       {
144          throw e;
145       }
146       catch (Exception JavaDoc e)
147       {
148          log.error("Error", e);
149          throw new EJBException JavaDoc("Untested problem in test: " + e);
150       }
151       finally
152       {
153          try
154          {
155             if (c1 != null)
156                c1.close();
157          }
158          catch (Throwable JavaDoc ignored)
159          {
160          }
161          try
162          {
163             if (c2 != null)
164                c2.close();
165          }
166          catch (Throwable JavaDoc ignored)
167          {
168          }
169       }
170    }
171
172    public void testAutoCommitOffInUserTx()
173    {
174       Connection JavaDoc c1 = null;
175       try
176       {
177          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
178          c1 = ds.getConnection();
179          Connection JavaDoc uc1 = ((WrappedConnection) c1).getUnderlyingConnection();
180          if (c1.getAutoCommit() == false)
181          {
182             throw new EJBException JavaDoc("Initial autocommit state false!");
183          }
184
185          Statement JavaDoc s1 = c1.createStatement();
186          s1.execute("create table autocommittest (id integer)");
187          try
188          {
189             UserTransaction JavaDoc ut = (UserTransaction JavaDoc) new InitialContext JavaDoc().lookup("UserTransaction");
190             ut.begin();
191             s1.execute("insert into autocommittest values (1)");
192             if (uc1.getAutoCommit())
193             {
194                throw new EJBException JavaDoc("Underlying autocommit is true in user tx!");
195             }
196
197             ut.rollback();
198             ResultSet JavaDoc rs1 = s1.executeQuery("select * from autocommittest where id = 1");
199             if (rs1.next())
200             {
201                throw new EJBException JavaDoc("Row committed, autocommit still on!");
202             }
203             rs1.close();
204
205          }
206          finally
207          {
208             s1.execute("drop table autocommittest");
209          }
210       }
211       catch (EJBException JavaDoc e)
212       {
213          throw e;
214       }
215       catch (Exception JavaDoc e)
216       {
217          log.error("Error", e);
218          throw new EJBException JavaDoc("Untested problem in test: " + e);
219       }
220       finally
221       {
222          try
223          {
224             if (c1 != null)
225                c1.close();
226          }
227          catch (Throwable JavaDoc ignored)
228          {
229          }
230       }
231    }
232
233    public void testAutoCommitOffInUserTx2()
234    {
235       try
236       {
237          createTable();
238          UserTransaction JavaDoc ut = (UserTransaction JavaDoc) new InitialContext JavaDoc().lookup("UserTransaction");
239          ut.begin();
240          insertAndCheckAutoCommit();
241          ut.rollback();
242       }
243       catch (EJBException JavaDoc e)
244       {
245          throw e;
246       }
247       catch (Exception JavaDoc e)
248       {
249          log.error("Error", e);
250          throw new EJBException JavaDoc("Untested problem in test: " + e);
251       }
252       finally
253       {
254          checkRowAndDropTable();
255       }
256
257    }
258
259    public void testReadOnly()
260    {
261       Connection JavaDoc c1 = null;
262       try
263       {
264          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
265          c1 = ds.getConnection();
266          Connection JavaDoc uc1 = ((WrappedConnection) c1).getUnderlyingConnection();
267
268          if (uc1.isReadOnly() == true)
269             throw new EJBException JavaDoc("Initial underlying readonly true!");
270          if (c1.isReadOnly() == true)
271             throw new EJBException JavaDoc("Initial readonly true!");
272
273          c1.setReadOnly(true);
274
275          if (uc1.isReadOnly() == true)
276             throw new EJBException JavaDoc("Read Only should be lazy!");
277          if (c1.isReadOnly() == false)
278             throw new EJBException JavaDoc("Changed readonly false!");
279
280          c1.createStatement();
281
282          if (uc1.isReadOnly() == false)
283             throw new EJBException JavaDoc("Lazy read only failed!");
284          if (c1.isReadOnly() == false)
285             throw new EJBException JavaDoc("Read only changed unexpectedly!");
286       }
287       catch (EJBException JavaDoc e)
288       {
289          throw e;
290       }
291       catch (Exception JavaDoc e)
292       {
293          log.error("Error", e);
294          throw new EJBException JavaDoc("Untested problem in test: " + e);
295       }
296       finally
297       {
298          try
299          {
300             if (c1 != null)
301                c1.close();
302          }
303          catch (Throwable JavaDoc ignored)
304          {
305          }
306       }
307    }
308
309    public void createTable()
310    {
311       try
312       {
313          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
314          Connection JavaDoc c1 = ds.getConnection();
315          try
316          {
317             if (c1.getAutoCommit() == false)
318                throw new EJBException JavaDoc("Initial autocommit state false!");
319             Statement JavaDoc s1 = c1.createStatement();
320             s1.execute("create table autocommittest (id integer)");
321          }
322          finally
323          {
324             c1.close();
325          }
326       }
327       catch (EJBException JavaDoc e)
328       {
329          throw e;
330       }
331       catch (Exception JavaDoc e)
332       {
333          log.error("Error", e);
334          throw new EJBException JavaDoc("Untested problem in test: " + e);
335       }
336    }
337
338    public void insertAndCheckAutoCommit()
339    {
340       try
341       {
342          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
343          Connection JavaDoc c1 = ds.getConnection();
344          Connection JavaDoc uc1 = ((WrappedConnection) c1).getUnderlyingConnection();
345          try
346          {
347             Statement JavaDoc s1 = c1.createStatement();
348             s1.execute("insert into autocommittest values (1)");
349             if (uc1.getAutoCommit())
350                throw new EJBException JavaDoc("Underlying autocommit is true in user tx!");
351          }
352          finally
353          {
354             c1.close();
355          }
356       }
357       catch (EJBException JavaDoc e)
358       {
359          throw e;
360       }
361       catch (Exception JavaDoc e)
362       {
363          log.error("Error", e);
364          throw new EJBException JavaDoc("Untested problem in test: " + e);
365       }
366
367    }
368
369    public void testManualNoCommitRollback()
370    {
371       try
372       {
373          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
374          Connection JavaDoc c1 = ds.getConnection();
375          try
376          {
377             c1.setAutoCommit(false);
378             Statement JavaDoc s1 = c1.createStatement();
379             s1.execute("insert into autocommittest values (1)");
380          }
381          finally
382          {
383             c1.close();
384          }
385       }
386       catch (EJBException JavaDoc e)
387       {
388          throw e;
389       }
390       catch (Exception JavaDoc e)
391       {
392          log.error("Error", e);
393          throw new EJBException JavaDoc("Untested problem in test: " + e);
394       }
395    }
396
397    public void testManualSecondNoCommitRollback()
398    {
399       try
400       {
401          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
402          Connection JavaDoc c1 = ds.getConnection();
403          try
404          {
405             c1.setAutoCommit(false);
406             Statement JavaDoc s1 = c1.createStatement();
407             s1.execute("insert into autocommittest values (0)");
408             c1.commit();
409             s1.execute("insert into autocommittest values (1)");
410          }
411          finally
412          {
413             c1.close();
414          }
415       }
416       catch (EJBException JavaDoc e)
417       {
418          throw e;
419       }
420       catch (Exception JavaDoc e)
421       {
422          log.error("Error", e);
423          throw new EJBException JavaDoc("Untested problem in test: " + e);
424       }
425    }
426
427    public void checkRowAndDropTable()
428    {
429       try
430       {
431          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
432          Connection JavaDoc c1 = ds.getConnection();
433          try
434          {
435             if (c1.getAutoCommit() == false)
436                throw new EJBException JavaDoc("Initial autocommit state false!");
437
438             Statement JavaDoc s1 = c1.createStatement();
439             ResultSet JavaDoc rs1 = s1.executeQuery("select * from autocommittest where id = 1");
440             if (rs1.next())
441                throw new EJBException JavaDoc("Row committed, autocommit still on!");
442          }
443          finally
444          {
445             try
446             {
447                Statement JavaDoc s1 = c1.createStatement();
448                s1.execute("drop table autocommittest");
449             }
450             catch (Throwable JavaDoc t)
451             {
452                log.warn("Ignored", t);
453             }
454             c1.close();
455          }
456
457       }
458       catch (EJBException JavaDoc e)
459       {
460          throw e;
461       }
462       catch (Exception JavaDoc e)
463       {
464          log.error("Error", e);
465          throw new EJBException JavaDoc("Untested problem in test: " + e);
466       }
467
468    }
469
470    public void addRowCheckAndDropTable()
471    {
472       try
473       {
474          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
475          Connection JavaDoc c1 = ds.getConnection();
476          try
477          {
478             if (c1.getAutoCommit() == false)
479                throw new EJBException JavaDoc("Initial autocommit state false!");
480             c1.setAutoCommit(false);
481
482             Statement JavaDoc s1 = c1.createStatement();
483             s1.execute("insert into autocommittest values (2)");
484             c1.commit();
485
486             ResultSet JavaDoc rs1 = s1.executeQuery("select * from autocommittest where id = 1");
487             if (rs1.next())
488                throw new EJBException JavaDoc("Row committed, didn't rollback!");
489          }
490          finally
491          {
492             try
493             {
494                Statement JavaDoc s1 = c1.createStatement();
495                s1.execute("drop table autocommittest");
496             }
497             catch (Throwable JavaDoc ignored)
498             {
499             }
500             c1.close();
501          }
502
503       }
504       catch (EJBException JavaDoc e)
505       {
506          throw e;
507       }
508       catch (Exception JavaDoc e)
509       {
510          log.error("Error", e);
511          throw new EJBException JavaDoc("Untested problem in test: " + e);
512       }
513    }
514
515    public void ejbCreate()
516    {
517    }
518
519    public void ejbActivate()
520    {
521    }
522
523    public void ejbPassivate()
524    {
525    }
526
527    public void ejbRemove()
528    {
529    }
530
531    public void setSessionContext(SessionContext JavaDoc ctx)
532    {
533    }
534
535    public void unsetSessionContext()
536    {
537    }
538 }
539
Popular Tags