1 22 package org.jboss.test.jca.ejb; 23 24 import javax.ejb.SessionBean ; 25 import javax.naming.InitialContext ; 26 import javax.sql.DataSource ; 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.sql.Statement ; 30 31 import javax.ejb.EJBException ; 32 import javax.ejb.SessionContext ; 33 import javax.naming.NamingException ; 34 35 import org.jboss.logging.Logger; 36 import org.jboss.resource.adapter.jdbc.WrappedConnection; 37 import org.jboss.test.jca.jdbc.TestConnection; 38 39 52 public class JDBCStatementTestsConnectionSessionBean implements SessionBean 53 { 54 55 private static final long serialVersionUID = 1L; 56 57 private static final Logger log = Logger.getLogger(JDBCStatementTestsConnectionSessionBean.class); 58 59 private SessionContext sessionContext; 60 61 public JDBCStatementTestsConnectionSessionBean() 62 { 63 64 } 65 66 78 public void testConnectionObtainable() 79 { 80 TestConnection tc = null; 81 try 82 { 83 DataSource ds = (DataSource ) new InitialContext ().lookup("java:StatementTestsConnectionDS"); 84 Connection c = ds.getConnection(); 85 WrappedConnection wc = (WrappedConnection) c; 86 Connection uc = wc.getUnderlyingConnection(); 87 tc = (TestConnection) uc; 88 c.close(); 89 tc.setFail(true); 90 int closeCount1 = tc.getClosedCount(); 91 c = ds.getConnection(); 92 if (closeCount1 == tc.getClosedCount()) 93 { 94 throw new EJBException ("no connections closed!, closedCount: " + closeCount1); 95 } 96 c.close(); 97 for (int i = 0; i < 10; i++) 98 { 99 100 int closeCount = tc.getClosedCount(); 101 c = ds.getConnection(); 102 if (closeCount == tc.getClosedCount()) 103 { 104 throw new EJBException ("no connections closed! at iteration: " + i + ", closedCount: " + closeCount); 105 } 106 c.close(); 107 } 108 109 } 110 catch (SQLException e) 111 { 112 throw new EJBException (e); 113 } 114 catch (NamingException e) 115 { 116 throw new EJBException (e); 117 } 118 finally 119 { 120 tc.setFail(false); 121 } 122 } 123 124 128 public void testConfiguredQueryTimeout() 129 { 130 try 131 { 132 DataSource ds = (DataSource ) new InitialContext ().lookup("java:StatementTestsConnectionDS"); 133 Connection c = ds.getConnection(); 134 try 135 { 136 Statement s = c.createStatement(); 137 s.execute("blah"); 138 if (s.getQueryTimeout() != 100) 139 throw new EJBException ("Configured query timeout not set"); 140 } 141 finally 142 { 143 c.close(); 144 } 145 } 146 catch (SQLException e) 147 { 148 throw new EJBException (e); 149 } 150 catch (NamingException e) 151 { 152 throw new EJBException (e); 153 } 154 } 155 156 160 public void testTransactionQueryTimeout() 161 { 162 try 163 { 164 DataSource ds = (DataSource ) new InitialContext ().lookup("java:StatementTestsConnectionDS"); 165 Connection c = ds.getConnection(); 166 try 167 { 168 Statement s = c.createStatement(); 169 s.execute("blah"); 170 if (s.getQueryTimeout() == 0) 171 throw new EJBException ("Tranaction query timeout not set"); 172 } 173 finally 174 { 175 c.close(); 176 } 177 } 178 catch (SQLException e) 179 { 180 throw new EJBException (e); 181 } 182 catch (NamingException e) 183 { 184 throw new EJBException (e); 185 } 186 } 187 188 192 public void testTransactionQueryTimeoutMarkedRollback() 193 { 194 try 195 { 196 DataSource ds = (DataSource ) new InitialContext ().lookup("java:StatementTestsConnectionDS"); 197 Connection c = ds.getConnection(); 198 try 199 { 200 Statement s = c.createStatement(); 201 sessionContext.setRollbackOnly(); 202 try 203 { 204 s.execute("blah"); 205 throw new EJBException ("Should not be here!"); 206 } 207 catch (SQLException expected) 208 { 209 log.info("Got expected sql exception", expected); 210 } 211 } 212 finally 213 { 214 c.close(); 215 } 216 } 217 catch (SQLException e) 218 { 219 throw new EJBException (e); 220 } 221 catch (NamingException e) 222 { 223 throw new EJBException (e); 224 } 225 } 226 227 231 public void testLazyAutoCommit() 232 { 233 try 234 { 235 DataSource ds = (DataSource ) new InitialContext ().lookup("java:NoTxStatementTestsConnectionDS"); 236 Connection c = ds.getConnection(); 237 try 238 { 239 c.setAutoCommit(false); 240 c.rollback(); 241 } 242 finally 243 { 244 c.close(); 245 } 246 c = ds.getConnection(); 247 try 248 { 249 c.setAutoCommit(false); 250 c.commit(); 251 } 252 finally 253 { 254 c.close(); 255 } 256 c = ds.getConnection(); 257 try 258 { 259 c.setAutoCommit(false); 260 c.rollback(null); 261 } 262 finally 263 { 264 c.close(); 265 } 266 } 267 catch (SQLException e) 268 { 269 throw new EJBException (e); 270 } 271 catch (NamingException e) 272 { 273 throw new EJBException (e); 274 } 275 } 276 277 public void ejbCreate() 278 { 279 } 280 281 public void ejbActivate() 282 { 283 } 284 285 public void ejbPassivate() 286 { 287 } 288 289 public void ejbRemove() 290 { 291 } 292 293 public void setSessionContext(SessionContext ctx) 294 { 295 sessionContext = ctx; 296 } 297 298 public void unsetSessionContext() 299 { 300 } 301 } 302 | Popular Tags |