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 java.rmi.RemoteException;
025: import java.sql.Connection;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029: import java.sql.Statement;
030: import javax.ejb.EJBException;
031: import javax.ejb.SessionBean;
032: import javax.ejb.SessionContext;
033: import javax.naming.InitialContext;
034: import javax.naming.NamingException;
035: import javax.sql.DataSource;
036: import org.apache.log4j.Category;
037: import org.jboss.test.jca.interfaces.CachedConnectionSessionLocal;
038:
039: /**
040: * CachedConnectionSessionBean.java
041: *
042: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
043: * @version <tt>$Revision: 57211 $</tt>
044: *
045: * @ejb:bean name="CachedConnectionSession"
046: * jndi-name="CachedConnectionSession"
047: * local-jndi-name="CachedConnectionSessionBean"
048: * view-type="both"
049: * type="Stateless"
050: *
051: */
052:
053: public class CachedConnectionSessionBean implements SessionBean {
054:
055: /** The serialVersionUID */
056: private static final long serialVersionUID = 1L;
057: private Connection conn;
058: private Category log = Category.getInstance(getClass().getName());
059: private SessionContext ctx;
060:
061: /**
062: * Describe <code>createTable</code> method here.
063: *
064: * @ejb:interface-method
065: */
066: public void createTable() {
067: try {
068: dropTable();
069: } catch (Exception e) {
070: }
071:
072: try {
073: Statement s = getConn().createStatement();
074: try {
075: s
076: .execute("CREATE TABLE TESTCACHEDCONN (ID NUMERIC(18,0) NOT NULL PRIMARY KEY, VAL VARCHAR(255))");
077: } finally {
078: s.close();
079: }
080: } catch (SQLException e) {
081: log.error("sql exception in create table", e);
082: }
083: }
084:
085: /**
086: * Describe <code>dropTable</code> method here.
087: *
088: * @ejb:interface-method
089: */
090: public void dropTable() {
091: try {
092: Statement s = getConn().createStatement();
093: try {
094: s.execute("DROP TABLE TESTCACHEDCONN");
095: } finally {
096: s.close();
097: }
098: } catch (SQLException e) {
099: log.error("sql exception in drop", e);
100: }
101: }
102:
103: /**
104: * Describe <code>insert</code> method here.
105: *
106: * @param id a <code>String</code> value
107: * @param value a <code>String</code> value
108: *
109: * @ejb:interface-method
110: */
111: public void insert(long id, String value) {
112: try {
113: PreparedStatement p = getConn()
114: .prepareStatement(
115: "INSERT INTO TESTCACHEDCONN (ID, VAL) VALUES (?, ?)");
116: try {
117: p.setLong(1, id);
118: p.setString(2, value);
119: p.execute();
120: } finally {
121: p.close();
122: }
123: } catch (SQLException e) {
124: log.error("sql exception in insert", e);
125: }
126: }
127:
128: /**
129: * Describe <code>fetch</code> method here.
130: *
131: * @param id a <code>String</code> value
132: *
133: * @ejb:interface-method
134: */
135: public String fetch(long id) {
136: try {
137: PreparedStatement p = getConn().prepareStatement(
138: "SELECT VAL FROM TESTCACHEDCONN WHERE ID = ?");
139: ResultSet rs = null;
140: try {
141: p.setLong(1, id);
142: rs = p.executeQuery();
143: if (rs.next()) {
144: return rs.getString(1);
145: }
146: return null;
147: } finally {
148: rs.close();
149: p.close();
150: }
151: } catch (SQLException e) {
152: log.error("sql exception in fetch", e);
153: return null;
154: }
155: }
156:
157: private Connection getConn() {
158: if (conn == null) {
159: log.info("ejb activate never called, conn == null");
160: ejbActivate();
161: }
162: if (conn == null) {
163: throw new IllegalStateException(
164: "could not get a connection");
165: }
166:
167: return conn;
168: }
169:
170: /**
171: * Invoke another bean that opens a thread local connection,
172: * we close it.
173: *
174: * @ejb:interface-method
175: */
176: public void firstTLTest() {
177: try {
178: CachedConnectionSessionLocal other = (CachedConnectionSessionLocal) ctx
179: .getEJBLocalObject();
180: other.secondTLTest();
181: ThreadLocalDB.close();
182: } catch (Exception e) {
183: log.info("Error", e);
184: throw new EJBException(e.toString());
185: }
186: }
187:
188: /**
189: * @ejb:interface-method
190: */
191: public void secondTLTest() {
192: try {
193: Connection c = ThreadLocalDB.open();
194: c.createStatement().close();
195: } catch (Exception e) {
196: log.info("Error", e);
197: throw new EJBException(e.toString());
198: }
199: }
200:
201: public void ejbCreate() {
202: }
203:
204: public void ejbActivate() {
205: log = Category.getInstance(getClass());
206: try {
207: //DataSource ds = (DataSource)new InitialContext().lookup("java:/comp/env/datasource");
208: DataSource ds = (DataSource) new InitialContext()
209: .lookup("java:DefaultDS");
210: conn = ds.getConnection();
211: } catch (NamingException e) {
212: log.error("naming exception in activate", e);
213: } catch (SQLException e) {
214: log.error("sql exception in activate", e);
215: }
216:
217: }
218:
219: public void ejbPassivate() throws RemoteException {
220: try {
221: conn.close();
222: } catch (SQLException e) {
223: log.error("sql exception in passivate", e);
224: }
225: conn = null;
226: log = null;
227: }
228:
229: public void ejbRemove() throws RemoteException {
230: }
231:
232: public void setSessionContext(SessionContext ctx)
233: throws RemoteException {
234: this .ctx = ctx;
235: }
236:
237: public void unsetSessionContext() throws RemoteException {
238: }
239: }
|