KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.RemoteException JavaDoc;
25 import java.sql.CallableStatement JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Statement JavaDoc;
31
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.DuplicateKeyException JavaDoc;
34 import javax.ejb.EJBException JavaDoc;
35 import javax.ejb.EntityBean JavaDoc;
36 import javax.ejb.EntityContext JavaDoc;
37 import javax.ejb.FinderException JavaDoc;
38 import javax.ejb.NoSuchEntityException JavaDoc;
39 import javax.ejb.ObjectNotFoundException JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42 import javax.sql.DataSource JavaDoc;
43
44 import org.jboss.ejb.plugins.cmp.jdbc.WrappedStatement;
45 import org.jboss.logging.Logger;
46
47 /** A BMP bean which exercises the prepared statement cache.
48  *
49  * @author Scott.Stark@jboss.org
50  * @author Adrian.Brock@jboss.org
51  * @version $Revision: 40746 $
52  */

53 public class PreparedStatementBean
54    implements EntityBean JavaDoc
55 {
56    /** The serialVersionUID */
57    private static final long serialVersionUID = 6204314647869034863L;
58    static Logger log = Logger.getLogger(PreparedStatementBean.class);
59    private EntityContext JavaDoc ctx = null;
60    private DataSource JavaDoc ds;
61    private String JavaDoc key;
62    private String JavaDoc name;
63
64    public void ejbActivate()
65    {
66    }
67    public void ejbPassivate()
68    {
69    }
70
71    public void ejbLoad()
72    {
73       key = (String JavaDoc) ctx.getPrimaryKey();
74       log.debug("ejbLoad("+key+")");
75       try
76       {
77          Connection JavaDoc con = ds.getConnection();
78          try
79          {
80             PreparedStatement JavaDoc ps = con.prepareStatement("SELECT name FROM BMPTABLE WHERE pk=?");
81             try
82             {
83                ps.setString(1, key);
84                ResultSet JavaDoc rs = ps.executeQuery();
85
86                if (rs.next() == false)
87                   throw new NoSuchEntityException JavaDoc("Instance " +key +" not found in database.");
88                name = rs.getString(1);
89                rs.close();
90             }
91             finally
92             {
93                ps.close();
94             }
95          }
96          finally
97          {
98             con.close();
99          }
100       }
101       catch (SQLException JavaDoc e)
102       {
103          throw new EJBException JavaDoc(e);
104       }
105    }
106    public void ejbStore()
107    {
108       log.debug("ejbStore(" + key + ")");
109       try
110       {
111          Connection JavaDoc con = ds.getConnection();
112          try
113          {
114             PreparedStatement JavaDoc ps = con.prepareStatement("UPDATE BMPTABLE SET name=? WHERE pk=?");
115             try
116             {
117                ps.setString(1, key);
118                ps.setString(2, name);
119                ps.executeUpdate();
120             }
121             finally
122             {
123                ps.close();
124             }
125          }
126          finally
127          {
128             con.close();
129          }
130       }
131       catch (SQLException JavaDoc e)
132       {
133          throw new EJBException JavaDoc(e);
134       }
135    }
136    public void ejbRemove()
137    {
138       log.debug("ejbRemove(" + key + ") called");
139       try
140       {
141          Connection JavaDoc con = ds.getConnection();
142          try
143          {
144             PreparedStatement JavaDoc ps = con.prepareStatement("DELETE FROM BMPTABLE WHERE pk=?");
145             try
146             {
147                ps.setString(1, key);
148                ps.executeUpdate();
149             }
150             finally
151             {
152                ps.close();
153             }
154          }
155          finally
156          {
157             con.close();
158          }
159       }
160       catch (SQLException JavaDoc e)
161       {
162          throw new EJBException JavaDoc(e);
163       }
164
165       log.debug("Removed "+key);
166    }
167
168    public void setEntityContext(EntityContext JavaDoc ctx)
169    {
170       log.debug("setEntityContext() called");
171       this.ctx = ctx;
172       try
173       {
174          InitialContext JavaDoc ic = new InitialContext JavaDoc();
175          ds = (DataSource JavaDoc) ic.lookup("java:comp/env/jdbc/DataSource");
176       }
177       catch (NamingException JavaDoc e)
178       {
179          throw new EJBException JavaDoc("DataSource init failed", e);
180       }
181    }
182    public void unsetEntityContext() throws EJBException JavaDoc, RemoteException JavaDoc
183    {
184       ds = null;
185       
186    }
187
188    public String JavaDoc ejbCreate(String JavaDoc pk, String JavaDoc name)
189       throws CreateException JavaDoc, DuplicateKeyException JavaDoc
190    {
191       log.debug("entry ejbCreate("+ pk + ", " + name + ")");
192       ensureTableExists();
193       try
194       {
195          Connection JavaDoc con = ds.getConnection();
196          try
197          {
198             PreparedStatement JavaDoc ps = con.prepareStatement("INSERT INTO BMPTABLE VALUES (?,?)");
199             try
200             {
201                ps.setString(1, pk);
202                ps.setString(2, name);
203                ps.execute();
204             }
205             finally
206             {
207                ps.close();
208             }
209          }
210          finally
211          {
212             con.close();
213          }
214       }
215       catch (SQLException JavaDoc e)
216       {
217          log.debug("failed", e);
218
219          throw new CreateException JavaDoc("Entity bean creation failure: " +
220             e.getMessage());
221       }
222
223       this.key = pk;
224       this.name = name;
225       log.debug("Created BMP: "+pk);
226       return pk;
227    }
228
229    public void ejbPostCreate(String JavaDoc pk, String JavaDoc name)
230    {
231       log.debug("entry ejbCreate("+ pk + ", " + name + ")");
232    }
233
234   public String JavaDoc ejbFindByPrimaryKey(String JavaDoc pk)
235      throws FinderException JavaDoc
236   {
237      log.debug("ejbFindByPrimaryKey, pk="+pk);
238      ensureTableExists();
239      try
240      {
241         Connection JavaDoc con = ds.getConnection();
242         try
243         {
244            PreparedStatement JavaDoc ps;
245
246            ps = con.prepareStatement("SELECT pk FROM BMPTABLE WHERE pk=?");
247            try
248            {
249               ps.setString(1, pk);
250               ResultSet JavaDoc rs = ps.executeQuery();
251               if (!rs.next())
252                  throw new ObjectNotFoundException JavaDoc("No bean with " +"pk=" +pk+ " found.");
253               rs.close();
254               return pk;
255            }
256            finally
257            {
258               ps.close();
259            }
260         }
261         finally
262         {
263            con.close();
264         }
265      }
266      catch (SQLException JavaDoc e)
267      {
268         throw new FinderException JavaDoc("Could not find pk="+pk+", msg=" + e.getMessage());
269      }
270   }
271
272    public long hashEntityTable()
273       throws RemoteException JavaDoc
274    {
275       long hash = 0;
276       try
277       {
278          Connection JavaDoc con = ds.getConnection();
279          try
280          {
281             PreparedStatement JavaDoc ps = con.prepareStatement("SELECT pk FROM BMPTABLE");
282             try
283             {
284                ResultSet JavaDoc rs = ps.executeQuery();
285                while( rs.next() )
286                {
287                   String JavaDoc pk = rs.getString(1);
288                   PreparedStatement JavaDoc ps2 = con.prepareStatement("SELECT name FROM BMPTABLE WHERE pk=?");
289                   ps2.setString(1, pk);
290                   ResultSet JavaDoc rs2 = ps2.executeQuery();
291                   if( rs2.next() )
292                   {
293                      String JavaDoc pkName = rs2.getString(1);
294                      hash += pkName.hashCode();
295                   }
296                   rs2.close();
297                   ps2.close();
298                }
299                rs.close();
300             }
301             finally
302             {
303                ps.close();
304             }
305          }
306          finally
307          {
308             con.close();
309          }
310       }
311       catch (SQLException JavaDoc e)
312       {
313          throw new RemoteException JavaDoc("Failed to calculate hash", e);
314       }
315       return hash;
316    }
317
318    public void testPreparedStatementCache() throws RemoteException JavaDoc
319    {
320       try
321       {
322          Connection JavaDoc con = ds.getConnection();
323          try
324          {
325             PreparedStatement JavaDoc ps = con.prepareStatement("SELECT pk FROM BMPTABLE");
326             Statement JavaDoc ps1 = getWrappedStatement(ps);
327             ps.close();
328             ps = con.prepareStatement("SELECT pk FROM BMPTABLE");
329             Statement JavaDoc ps2 = getWrappedStatement(ps);
330             if (ps1 != ps2)
331                throw new EJBException JavaDoc("Statement " + ps1 + " was not cached: got " + ps2);
332             ps.close();
333             ps = con.prepareStatement("SELECT pk FROM BMPTABLE", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
334             ps2 = getWrappedStatement(ps);
335             if (ps1 != ps2)
336                throw new EJBException JavaDoc("Statement " + ps1 + " was not cached against default result set parameters: got " + ps2);
337             ps.close();
338             ps = con.prepareStatement("SELECT pk FROM BMPTABLE", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
339             ps2 = getWrappedStatement(ps);
340             if (ps1 == ps2)
341                throw new EJBException JavaDoc("Statement " + ps1 + " should be different with different result set parameters: got " + ps2);
342             ps.close();
343          }
344          finally
345          {
346             con.close();
347          }
348       }
349       catch (SQLException JavaDoc e)
350       {
351          throw new RemoteException JavaDoc("Unexpected sql exception", e);
352       }
353    }
354
355    public void testPreparedStatementCacheDoubleClose() throws RemoteException JavaDoc
356    {
357       try
358       {
359          Connection JavaDoc con = ds.getConnection();
360          try
361          {
362             PreparedStatement JavaDoc ps = con.prepareStatement("SELECT pk FROM BMPTABLE");
363             ps.close();
364             try
365             {
366                ps.close();
367             }
368             catch (SQLException JavaDoc e)
369             {
370                log.debug("Got expected double close exception", e);
371             }
372          }
373          finally
374          {
375             con.close();
376          }
377       }
378       catch (SQLException JavaDoc e)
379       {
380          throw new RemoteException JavaDoc("Unexpected sql exception", e);
381       }
382    }
383
384    public void testCallableStatementCache(String JavaDoc name) throws RemoteException JavaDoc
385    {
386       try
387       {
388          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
389          String JavaDoc query = (String JavaDoc) ctx.lookup("java:comp/env/"+name);
390
391          Connection JavaDoc con = ds.getConnection();
392          try
393          {
394             CallableStatement JavaDoc ps = con.prepareCall(query);
395             Statement JavaDoc ps1 = getWrappedStatement(ps);
396             ps.close();
397             ps = con.prepareCall(query);
398             Statement JavaDoc ps2 = getWrappedStatement(ps);
399             if (ps1 != ps2)
400                throw new EJBException JavaDoc("Statement " + ps1 + " was not cached: got " + ps2);
401             ps.close();
402             ps = con.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
403             ps2 = getWrappedStatement(ps);
404             if (ps1 != ps2)
405                throw new EJBException JavaDoc("Statement " + ps1 + " was not cached against default result set parameters: got " + ps2);
406             ps.close();
407             ps = con.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
408             ps2 = getWrappedStatement(ps);
409             if (ps1 == ps2)
410                throw new EJBException JavaDoc("Statement " + ps1 + " should be different with different result set parameters: got " + ps2);
411             ps.close();
412          }
413          finally
414          {
415             con.close();
416          }
417       }
418       catch (SQLException JavaDoc e)
419       {
420          throw new RemoteException JavaDoc("Unexpected sql exception", e);
421       }
422       catch (NamingException JavaDoc e)
423       {
424          throw new RemoteException JavaDoc("Failed to lookup query", e);
425       }
426    }
427
428    public void testCallableStatementCacheDoubleClose(String JavaDoc name) throws RemoteException JavaDoc
429    {
430       try
431       {
432          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
433          String JavaDoc query = (String JavaDoc) ctx.lookup("java:comp/env/"+name);
434
435          Connection JavaDoc con = ds.getConnection();
436          try
437          {
438             CallableStatement JavaDoc ps = con.prepareCall(query);
439             ps.close();
440             try
441             {
442                ps.close();
443             }
444             catch (SQLException JavaDoc e)
445             {
446                log.debug("Got expected double close exception", e);
447             }
448          }
449          finally
450          {
451             con.close();
452          }
453       }
454       catch (SQLException JavaDoc e)
455       {
456          throw new RemoteException JavaDoc("Unexpected sql exception", e);
457       }
458       catch (NamingException JavaDoc e)
459       {
460          throw new RemoteException JavaDoc("Failed to lookup query", e);
461       }
462    }
463
464    public String JavaDoc executeStoredProc(String JavaDoc name)
465       throws RemoteException JavaDoc
466    {
467       String JavaDoc value = null;
468       try
469       {
470          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
471          String JavaDoc query = (String JavaDoc) ctx.lookup("java:comp/env/"+name);
472
473          Connection JavaDoc con = ds.getConnection();
474          try
475          {
476             CallableStatement JavaDoc ps = con.prepareCall(query);
477             try
478             {
479                ResultSet JavaDoc rs = ps.executeQuery();
480                while( rs.next() )
481                {
482                   value = rs.getString(1);
483                }
484                rs.close();
485             }
486             finally
487             {
488                ps.close();
489             }
490          }
491          finally
492          {
493             con.close();
494          }
495       }
496       catch (SQLException JavaDoc e)
497       {
498          throw new RemoteException JavaDoc("Failed to execuate CallableStatement", e);
499       }
500       catch (NamingException JavaDoc e)
501       {
502          throw new RemoteException JavaDoc("Failed to lookup query", e);
503       }
504       return value;
505    }
506
507    private void ensureTableExists()
508    {
509       boolean exists = true;
510
511       try
512       {
513          Connection JavaDoc con = ds.getConnection();
514          try
515          {
516             Statement JavaDoc s = con.createStatement();
517             try
518             {
519                ResultSet JavaDoc rs = s.executeQuery("SELECT * FROM BMPTABLE");
520                rs.close();
521             }
522             finally
523             {
524                s.close();
525             }
526          }
527          finally
528          {
529             con.close();
530          }
531       }
532       catch (SQLException JavaDoc e)
533       {
534          exists = false;
535       }
536
537       if (!exists)
538       {
539          try
540          {
541             Connection JavaDoc con = ds.getConnection();
542             try
543             {
544                Statement JavaDoc s = con.createStatement();
545                try
546                {
547                   s.executeUpdate("CREATE TABLE BMPTABLE (pk VARCHAR(16), name VARCHAR(32))");
548                }
549                finally
550                {
551                   s.close();
552                }
553             }
554             finally
555             {
556                con.close();
557             }
558          }
559          catch (SQLException JavaDoc e)
560          {
561             throw new EJBException JavaDoc(e);
562          }
563       }
564    }
565
566    public Statement JavaDoc getWrappedStatement(Statement JavaDoc s) throws SQLException JavaDoc
567    {
568       while (s instanceof WrappedStatement)
569          s = ((WrappedStatement) s).getUnderlyingStatement();
570       return s;
571    }
572
573 }
574
Popular Tags