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 javax.ejb.EJBException;
025: import javax.ejb.SessionBean;
026: import javax.ejb.SessionContext;
027: import javax.management.MBeanServer;
028: import javax.management.ObjectName;
029: import javax.naming.InitialContext;
030: import javax.resource.ResourceException;
031: import javax.resource.cci.Connection;
032: import javax.resource.cci.ConnectionFactory;
033: import javax.transaction.xa.XAException;
034:
035: import org.jboss.logging.Logger;
036: import org.jboss.mx.util.MBeanServerLocator;
037: import org.jboss.test.jca.adapter.TestConnection;
038: import org.jboss.test.jca.adapter.TestConnectionFactory;
039:
040: /**
041: * XAExceptionSessionBean.java
042: *
043: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
044: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
045: * @version <tt>$Revision: 57211 $</tt>
046: *
047: * @ejb:bean name="XAExceptionSession"
048: * jndi-name="test/XAExceptionSessionHome"
049: * local-jndi-name="test/XAExceptionSessionLocalHome"
050: * view-type="both"
051: * type="Stateless"
052: * @ejb.transaction type="Required"
053: *
054: */
055: public class XAExceptionSessionBean implements SessionBean {
056: /** The serialVersionUID */
057: private static final long serialVersionUID = 1L;
058:
059: private Logger log = Logger.getLogger(XAExceptionSessionBean.class);
060:
061: private SessionContext sessionContext;
062:
063: /**
064: * Describe <code>ejbCreate</code> method here.
065: * @ejb.interface-method
066: */
067: public void ejbCreate() {
068: }
069:
070: /**
071: * Describe <code>testXAException</code> method here.
072: * @ejb.interface-method
073: */
074: public void testXAExceptionToTransactionRolledbackException() {
075: try {
076:
077: InitialContext ctx = new InitialContext();
078: ConnectionFactory cf1 = (ConnectionFactory) ctx
079: .lookup("java:/JBossTestCF");
080: ConnectionFactory cf2 = (ConnectionFactory) ctx
081: .lookup("java:/JBossTestCF2");
082: Connection c1 = cf1.getConnection();
083: try {
084: TestConnection c2 = (TestConnection) cf2
085: .getConnection();
086: try {
087: c2
088: .setFailInPrepare(true,
089: XAException.XA_RBROLLBACK);
090: } finally {
091: c2.close();
092: }
093: } finally {
094: c1.close();
095: }
096: } catch (Exception e) {
097: log.warn("Unexpected: ", e);
098: throw new EJBException("unexpected exception: " + e);
099: }
100: }
101:
102: /**
103: * Describe <code>testXAException</code> method here.
104: * @ejb.interface-method
105: */
106: public void testRMERRInOnePCToTransactionRolledbackException() {
107: try {
108:
109: InitialContext ctx = new InitialContext();
110: ConnectionFactory cf1 = (ConnectionFactory) ctx
111: .lookup("java:/JBossTestCF");
112: TestConnection c1 = (TestConnection) cf1.getConnection();
113: try {
114: c1.setFailInCommit(true, XAException.XAER_RMERR);
115:
116: } finally {
117: c1.close();
118: }
119:
120: } catch (Exception e) {
121: log.warn("Unexpected: ", e);
122: throw new EJBException("unexpected exception: " + e);
123: }
124: }
125:
126: /**
127: * Similate a connection failure
128: *
129: * @ejb.interface-method
130: */
131: public void simulateConnectionError() {
132: log.info("Simulating connection error");
133: try {
134: InitialContext ctx = new InitialContext();
135: ConnectionFactory cf = (ConnectionFactory) ctx
136: .lookup("java:/JBossTestCF");
137: TestConnection c = (TestConnection) cf.getConnection();
138: try {
139: c.simulateConnectionError();
140: } finally {
141: c.close();
142: }
143: } catch (Exception e) {
144: if (e.getMessage().equals("Simulated exception") == false) {
145: log.warn("Unexpected: ", e);
146: throw new EJBException(e.getMessage());
147: } else {
148: sessionContext.setRollbackOnly();
149: }
150: }
151: }
152:
153: /**
154: * Similate a connection failure
155: *
156: * @ejb.interface-method
157: */
158: public void simulateConnectionErrorWithTwoHandles() {
159: log.info("Simulating connection error with two handles");
160: try {
161: InitialContext ctx = new InitialContext();
162: ConnectionFactory cf = (ConnectionFactory) ctx
163: .lookup("java:/JBossTestCFByTx");
164: TestConnection c1 = (TestConnection) cf.getConnection();
165: TestConnection c2 = (TestConnection) cf.getConnection();
166: try {
167: c2.simulateConnectionError();
168: } finally {
169: try {
170: c1.close();
171: } catch (Throwable ignored) {
172: }
173: try {
174: c2.close();
175: } catch (Throwable ignored) {
176: }
177: }
178: } catch (Exception e) {
179: if (e.getMessage().equals("Simulated exception") == false) {
180: log.warn("Unexpected: ", e);
181: throw new EJBException(e.getMessage());
182: } else {
183: sessionContext.setRollbackOnly();
184: }
185: }
186: }
187:
188: /**
189: * Similate an exception
190: *
191: * @ejb.interface-method
192: */
193: public void simulateError(String failure, int count) {
194: log.info(failure + " teststart");
195: try {
196: long available = getAvailableConnections();
197: InitialContext ctx = new InitialContext();
198: TestConnectionFactory cf = (TestConnectionFactory) ctx
199: .lookup("java:/JBossTestCF");
200: for (int i = 0; i < count; ++i) {
201: try {
202: TestConnection c = (TestConnection) cf
203: .getConnection(failure);
204: c.close();
205: } catch (ResourceException expected) {
206: }
207: }
208: if (available != getAvailableConnections())
209: throw new EJBException("Expected " + available
210: + " got " + getAvailableConnections()
211: + " connections");
212: } catch (Exception e) {
213: log.warn("Unexpected: ", e);
214: throw new EJBException(e.getMessage());
215: }
216: }
217:
218: /**
219: * Similate an exception
220: *
221: * @ejb.interface-method
222: */
223: public void simulateFactoryError(String failure, int count) {
224: log.info(failure + " start");
225: TestConnectionFactory cf = null;
226: try {
227: long available = getAvailableConnections();
228: InitialContext ctx = new InitialContext();
229: cf = (TestConnectionFactory) ctx
230: .lookup("java:/JBossTestCF");
231: cf.setFailure(failure);
232: for (int i = 0; i < count; ++i) {
233: try {
234: TestConnection c = (TestConnection) cf
235: .getConnection(failure);
236: c.close();
237: } catch (ResourceException expected) {
238: }
239: }
240: if (available != getAvailableConnections())
241: throw new EJBException("Expected " + available
242: + " got " + getAvailableConnections()
243: + " connections");
244: } catch (Exception e) {
245: log.warn("Unexpected: ", e);
246: throw new EJBException(e.getMessage());
247: } finally {
248: sessionContext.setRollbackOnly();
249: if (cf != null)
250: cf.setFailure(null);
251: }
252: }
253:
254: public long getAvailableConnections() throws Exception {
255: MBeanServer server = MBeanServerLocator.locateJBoss();
256: return ((Long) server
257: .getAttribute(
258: new ObjectName(
259: "jboss.jca:service=ManagedConnectionPool,name=JBossTestCF"),
260: "AvailableConnectionCount")).longValue();
261: }
262:
263: public void ejbActivate() {
264: }
265:
266: public void ejbPassivate() {
267: }
268:
269: public void ejbRemove() {
270: }
271:
272: public void setSessionContext(SessionContext ctx) {
273: sessionContext = ctx;
274: }
275:
276: public void unsetSessionContext() {
277: sessionContext = null;
278: }
279:
280: }
|