1 22 package org.jboss.test.jca.ejb; 23 24 import java.rmi.RemoteException ; 25 import java.sql.Connection ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.sql.Statement ; 30 import javax.ejb.EJBException ; 31 import javax.ejb.SessionBean ; 32 import javax.ejb.SessionContext ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 import javax.sql.DataSource ; 36 import org.jboss.logging.Logger; 37 import org.jboss.test.jca.interfaces.CachedConnectionSessionLocal; 38 39 52 53 public class CachedConnectionSessionBean implements SessionBean { 54 55 56 private static final long serialVersionUID = 1L; 57 private Connection conn; 58 private Logger log = Logger.getLogger(getClass().getName()); 59 private SessionContext ctx; 60 61 66 public void createTable() 67 { 68 try 69 { 70 dropTable(); 71 } 72 catch (Exception e) 73 { 74 } 75 76 try 77 { 78 Statement 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 e) 89 { 90 log.error("sql exception in create table", e); 91 } 92 } 93 94 99 public void dropTable() 100 { 101 try 102 { 103 Statement s = getConn().createStatement(); 104 try 105 { 106 s.execute("DROP TABLE TESTCACHEDCONN"); 107 } 108 finally 109 { 110 s.close(); 111 } 112 } 113 catch (SQLException e) 114 { 115 log.error("sql exception in drop", e); 116 } 117 } 118 119 127 public void insert(long id, String value) 128 { 129 try 130 { 131 PreparedStatement 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 e) 144 { 145 log.error("sql exception in insert", e); 146 } 147 } 148 149 156 public String fetch(long id) 157 { 158 try 159 { 160 PreparedStatement p = getConn().prepareStatement("SELECT VAL FROM TESTCACHEDCONN WHERE ID = ?"); 161 ResultSet 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 e) 179 { 180 log.error("sql exception in fetch", e); 181 return null; 182 } 183 } 184 private Connection 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 ("could not get a connection"); 194 } 195 196 return conn; 197 } 198 199 205 public void firstTLTest() 206 { 207 try 208 { 209 CachedConnectionSessionLocal other = (CachedConnectionSessionLocal) ctx.getEJBLocalObject(); 210 other.secondTLTest(); 211 ThreadLocalDB.close(); 212 } 213 catch (Exception e) 214 { 215 log.info("Error", e); 216 throw new EJBException (e.toString()); 217 } 218 } 219 220 223 public void secondTLTest() 224 { 225 try 226 { 227 Connection c = ThreadLocalDB.open(); 228 c.createStatement().close(); 229 } 230 catch (Exception e) 231 { 232 log.info("Error", e); 233 throw new EJBException (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:DefaultDS"); 248 conn = ds.getConnection(); 249 } 250 catch (NamingException e) 251 { 252 log.error("naming exception in activate", e); 253 } 254 catch (SQLException e) 255 { 256 log.error("sql exception in activate", e); 257 } 258 259 } 260 261 public void ejbPassivate() throws RemoteException 262 { 263 try 264 { 265 conn.close(); 266 } 267 catch (SQLException 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 276 { 277 } 278 279 public void setSessionContext(SessionContext ctx) throws RemoteException 280 { 281 this.ctx = ctx; 282 } 283 284 public void unsetSessionContext() throws RemoteException 285 { 286 } 287 } 288 | Popular Tags |