1 22 package org.jboss.test.jca.ejb; 23 24 import javax.ejb.EJBException ; 25 import javax.ejb.SessionBean ; 26 import javax.ejb.SessionContext ; 27 import javax.management.MBeanServer ; 28 import javax.management.ObjectName ; 29 import javax.naming.InitialContext ; 30 import javax.resource.ResourceException ; 31 import javax.resource.cci.Connection ; 32 import javax.resource.cci.ConnectionFactory ; 33 import javax.transaction.xa.XAException ; 34 35 import org.jboss.logging.Logger; 36 import org.jboss.mx.util.MBeanServerLocator; 37 import org.jboss.test.jca.adapter.TestConnection; 38 import org.jboss.test.jca.adapter.TestConnectionFactory; 39 40 55 public class XAExceptionSessionBean implements SessionBean 56 { 57 58 private static final long serialVersionUID = 1L; 59 60 private Logger log = Logger.getLogger(XAExceptionSessionBean.class); 61 62 private SessionContext sessionContext; 63 64 68 public void ejbCreate() 69 { 70 } 71 72 73 77 public void testXAExceptionToTransactionRolledbackException() 78 { 79 try 80 { 81 82 InitialContext ctx = new InitialContext (); 83 ConnectionFactory cf1 = (ConnectionFactory ) ctx.lookup("java:/JBossTestCF"); 84 ConnectionFactory cf2 = (ConnectionFactory ) ctx.lookup("java:/JBossTestCF2"); 85 Connection c1 = cf1.getConnection(); 86 try 87 { 88 TestConnection c2 = (TestConnection) cf2.getConnection(); 89 try 90 { 91 c2.setFailInPrepare(true, XAException.XA_RBROLLBACK); 92 } 93 finally 94 { 95 c2.close(); 96 } 97 } 98 finally 99 { 100 c1.close(); 101 } 102 } 103 catch (Exception e) 104 { 105 log.warn("Unexpected: ", e); 106 throw new EJBException ("unexpected exception: " + e); 107 } 108 } 109 110 114 public void testRMERRInOnePCToTransactionRolledbackException() 115 { 116 try 117 { 118 119 InitialContext ctx = new InitialContext (); 120 ConnectionFactory cf1 = (ConnectionFactory ) ctx.lookup("java:/JBossTestCF"); 121 TestConnection c1 = (TestConnection) cf1.getConnection(); 122 try 123 { 124 c1.setFailInCommit(true, XAException.XAER_RMERR); 125 126 } 127 finally 128 { 129 c1.close(); 130 } 131 132 } 133 catch (Exception e) 134 { 135 log.warn("Unexpected: ", e); 136 throw new EJBException ("unexpected exception: " + e); 137 } 138 } 139 140 145 public void simulateConnectionError() 146 { 147 log.info("Simulating connection error"); 148 try 149 { 150 InitialContext ctx = new InitialContext (); 151 ConnectionFactory cf = (ConnectionFactory ) ctx.lookup("java:/JBossTestCF"); 152 TestConnection c = (TestConnection) cf.getConnection(); 153 try 154 { 155 c.simulateConnectionError(); 156 } 157 finally 158 { 159 c.close(); 160 } 161 } 162 catch (Exception e) 163 { 164 if (e.getMessage().equals("Simulated exception") == false) 165 { 166 log.warn("Unexpected: ", e); 167 throw new EJBException (e.getMessage()); 168 } 169 else 170 { 171 sessionContext.setRollbackOnly(); 172 } 173 } 174 } 175 176 181 public void simulateConnectionErrorWithTwoHandles() 182 { 183 log.info("Simulating connection error with two handles"); 184 try 185 { 186 InitialContext ctx = new InitialContext (); 187 ConnectionFactory cf = (ConnectionFactory ) ctx.lookup("java:/JBossTestCFByTx"); 188 TestConnection c1 = (TestConnection) cf.getConnection(); 189 TestConnection c2 = (TestConnection) cf.getConnection(); 190 try 191 { 192 c2.simulateConnectionError(); 193 } 194 finally 195 { 196 try 197 { 198 c1.close(); 199 } 200 catch (Throwable ignored) 201 { 202 } 203 try 204 { 205 c2.close(); 206 } 207 catch (Throwable ignored) 208 { 209 } 210 } 211 } 212 catch (Exception e) 213 { 214 if (e.getMessage().equals("Simulated exception") == false) 215 { 216 log.warn("Unexpected: ", e); 217 throw new EJBException (e.getMessage()); 218 } 219 else 220 { 221 sessionContext.setRollbackOnly(); 222 } 223 } 224 } 225 226 231 public void simulateError(String failure, int count) 232 { 233 log.info(failure + " teststart"); 234 try 235 { 236 long available = getAvailableConnections(); 237 InitialContext ctx = new InitialContext (); 238 TestConnectionFactory cf = (TestConnectionFactory) ctx.lookup("java:/JBossTestCF"); 239 for (int i = 0; i < count; ++i) 240 { 241 try 242 { 243 TestConnection c = (TestConnection) cf.getConnection(failure); 244 c.close(); 245 } 246 catch (ResourceException expected) 247 { 248 } 249 } 250 if (available != getAvailableConnections()) 251 throw new EJBException ("Expected " + available + " got " + getAvailableConnections() + " connections"); 252 } 253 catch (Exception e) 254 { 255 log.warn("Unexpected: ", e); 256 throw new EJBException (e.getMessage()); 257 } 258 } 259 260 265 public void simulateFactoryError(String failure, int count) 266 { 267 log.info(failure + " start"); 268 TestConnectionFactory cf = null; 269 try 270 { 271 long available = getAvailableConnections(); 272 InitialContext ctx = new InitialContext (); 273 cf = (TestConnectionFactory) ctx.lookup("java:/JBossTestCF"); 274 cf.setFailure(failure); 275 for (int i = 0; i < count; ++i) 276 { 277 try 278 { 279 TestConnection c = (TestConnection) cf.getConnection(failure); 280 c.close(); 281 } 282 catch (ResourceException expected) 283 { 284 } 285 } 286 if (available != getAvailableConnections()) 287 throw new EJBException ("Expected " + available + " got " + getAvailableConnections() + " connections"); 288 } 289 catch (Exception e) 290 { 291 log.warn("Unexpected: ", e); 292 throw new EJBException (e.getMessage()); 293 } 294 finally 295 { 296 sessionContext.setRollbackOnly(); 297 if (cf != null) 298 cf.setFailure(null); 299 } 300 } 301 302 public long getAvailableConnections() throws Exception 303 { 304 MBeanServer server = MBeanServerLocator.locateJBoss(); 305 return ((Long ) server.getAttribute(new ObjectName ("jboss.jca:service=ManagedConnectionPool,name=JBossTestCF"), 306 "AvailableConnectionCount")).longValue(); 307 } 308 309 public void ejbActivate() 310 { 311 } 312 313 public void ejbPassivate() 314 { 315 } 316 317 public void ejbRemove() 318 { 319 } 320 321 public void setSessionContext(SessionContext ctx) 322 { 323 sessionContext = ctx; 324 } 325 326 public void unsetSessionContext() 327 { 328 sessionContext = null; 329 } 330 331 } 332 | Popular Tags |