KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Statement JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.SessionBean JavaDoc;
32 import javax.ejb.SessionContext JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.sql.DataSource JavaDoc;
36 import org.jboss.logging.Logger;
37 import org.jboss.test.jca.interfaces.CachedConnectionSessionLocal;
38
39 /**
40  * CachedConnectionSessionBean.java
41  *
42  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
43  * @version <tt>$Revision: 58115 $</tt>
44  *
45  * @ejb:bean name="CachedConnectionSession"
46  * jndi-name="CachedConnectionSession"
47  * local-jndi-name="CachedConnectionSessionBean"
48  * view-type="both"
49  * type="Stateless"
50  *
51  */

52
53 public class CachedConnectionSessionBean implements SessionBean JavaDoc {
54
55    /** The serialVersionUID */
56    private static final long serialVersionUID = 1L;
57    private Connection JavaDoc conn;
58    private Logger log = Logger.getLogger(getClass().getName());
59    private SessionContext JavaDoc ctx;
60
61    /**
62     * Describe <code>createTable</code> method here.
63     *
64     * @ejb:interface-method
65     */

66    public void createTable()
67    {
68       try
69       {
70          dropTable();
71       }
72       catch (Exception JavaDoc e)
73       {
74       }
75
76       try
77       {
78          Statement JavaDoc s = getConn().createStatement();
79          try
80          {
81             s.execute("CREATE TABLE TESTCACHEDCONN (ID NUMERIC(18,0) NOT NULL PRIMARY KEY, VAL VARCHAR(255))");
82          }
83          finally
84          {
85             s.close();
86          }
87       }
88       catch (SQLException JavaDoc e)
89       {
90          log.error("sql exception in create table", e);
91       }
92    }
93
94    /**
95     * Describe <code>dropTable</code> method here.
96     *
97     * @ejb:interface-method
98     */

99    public void dropTable()
100    {
101       try
102       {
103          Statement JavaDoc s = getConn().createStatement();
104          try
105          {
106             s.execute("DROP TABLE TESTCACHEDCONN");
107          }
108          finally
109          {
110             s.close();
111          }
112       }
113       catch (SQLException JavaDoc e)
114       {
115          log.error("sql exception in drop", e);
116       }
117    }
118
119    /**
120     * Describe <code>insert</code> method here.
121     *
122     * @param id a <code>String</code> value
123     * @param value a <code>String</code> value
124     *
125     * @ejb:interface-method
126     */

127    public void insert(long id, String JavaDoc value)
128    {
129       try
130       {
131          PreparedStatement JavaDoc p = getConn().prepareStatement("INSERT INTO TESTCACHEDCONN (ID, VAL) VALUES (?, ?)");
132          try
133          {
134             p.setLong(1, id);
135             p.setString(2, value);
136             p.execute();
137          }
138          finally
139          {
140             p.close();
141          }
142       }
143       catch (SQLException JavaDoc e)
144       {
145          log.error("sql exception in insert", e);
146       }
147    }
148
149    /**
150     * Describe <code>fetch</code> method here.
151     *
152     * @param id a <code>String</code> value
153     *
154     * @ejb:interface-method
155     */

156    public String JavaDoc fetch(long id)
157    {
158       try
159       {
160          PreparedStatement JavaDoc p = getConn().prepareStatement("SELECT VAL FROM TESTCACHEDCONN WHERE ID = ?");
161          ResultSet JavaDoc rs = null;
162          try
163          {
164             p.setLong(1, id);
165             rs = p.executeQuery();
166             if (rs.next())
167             {
168                return rs.getString(1);
169             }
170             return null;
171          }
172          finally
173          {
174             rs.close();
175             p.close();
176          }
177       }
178       catch (SQLException JavaDoc e)
179       {
180          log.error("sql exception in fetch", e);
181          return null;
182       }
183    }
184    private Connection JavaDoc getConn()
185    {
186       if (conn == null)
187       {
188          log.info("ejb activate never called, conn == null");
189          ejbActivate();
190       }
191       if (conn == null)
192       {
193          throw new IllegalStateException JavaDoc("could not get a connection");
194       }
195
196       return conn;
197    }
198
199    /**
200     * Invoke another bean that opens a thread local connection,
201     * we close it.
202     *
203     * @ejb:interface-method
204     */

205    public void firstTLTest()
206    {
207       try
208       {
209          CachedConnectionSessionLocal other = (CachedConnectionSessionLocal) ctx.getEJBLocalObject();
210          other.secondTLTest();
211          ThreadLocalDB.close();
212       }
213       catch (Exception JavaDoc e)
214       {
215          log.info("Error", e);
216          throw new EJBException JavaDoc(e.toString());
217       }
218    }
219
220    /**
221     * @ejb:interface-method
222     */

223    public void secondTLTest()
224    {
225       try
226       {
227          Connection JavaDoc c = ThreadLocalDB.open();
228          c.createStatement().close();
229       }
230       catch (Exception JavaDoc e)
231       {
232          log.info("Error", e);
233          throw new EJBException JavaDoc(e.toString());
234       }
235    }
236
237    public void ejbCreate()
238    {
239    }
240
241    public void ejbActivate()
242    {
243       log = Logger.getLogger(getClass());
244       try
245       {
246          //DataSource ds = (DataSource)new InitialContext().lookup("java:/comp/env/datasource");
247
DataSource JavaDoc ds = (DataSource JavaDoc)new InitialContext JavaDoc().lookup("java:DefaultDS");
248          conn = ds.getConnection();
249       }
250       catch (NamingException JavaDoc e)
251       {
252          log.error("naming exception in activate", e);
253       }
254       catch (SQLException JavaDoc e)
255       {
256          log.error("sql exception in activate", e);
257       }
258
259     }
260
261    public void ejbPassivate() throws RemoteException JavaDoc
262    {
263       try
264       {
265          conn.close();
266       }
267       catch (SQLException JavaDoc e)
268       {
269          log.error("sql exception in passivate", e);
270       }
271       conn = null;
272       log = null;
273    }
274
275    public void ejbRemove() throws RemoteException JavaDoc
276    {
277    }
278
279    public void setSessionContext(SessionContext JavaDoc ctx) throws RemoteException JavaDoc
280    {
281       this.ctx = ctx;
282    }
283
284    public void unsetSessionContext() throws RemoteException JavaDoc
285    {
286    }
287 }
288
Popular Tags