KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > ejb > XAExceptionSessionBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.ejb;
23
24 import javax.ejb.EJBException JavaDoc;
25 import javax.ejb.SessionBean JavaDoc;
26 import javax.ejb.SessionContext JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.resource.ResourceException JavaDoc;
31 import javax.resource.cci.Connection JavaDoc;
32 import javax.resource.cci.ConnectionFactory JavaDoc;
33 import javax.transaction.xa.XAException JavaDoc;
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 /**
41  * XAExceptionSessionBean.java
42  *
43  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
44  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
45  * @version <tt>$Revision: 46107 $</tt>
46  *
47  * @ejb:bean name="XAExceptionSession"
48  * jndi-name="test/XAExceptionSessionHome"
49  * local-jndi-name="test/XAExceptionSessionLocalHome"
50  * view-type="both"
51  * type="Stateless"
52  * @ejb.transaction type="Required"
53  *
54  */

55 public class XAExceptionSessionBean implements SessionBean JavaDoc
56 {
57    /** The serialVersionUID */
58    private static final long serialVersionUID = 1L;
59
60    private Logger log = Logger.getLogger(XAExceptionSessionBean.class);
61    
62    private SessionContext JavaDoc sessionContext;
63
64    /**
65     * Describe <code>ejbCreate</code> method here.
66     * @ejb.interface-method
67     */

68    public void ejbCreate()
69    {
70    }
71    
72
73    /**
74     * Describe <code>testXAException</code> method here.
75     * @ejb.interface-method
76     */

77    public void testXAExceptionToTransactionRolledbackException()
78    {
79       try
80       {
81
82          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
83          ConnectionFactory JavaDoc cf1 = (ConnectionFactory JavaDoc) ctx.lookup("java:/JBossTestCF");
84          ConnectionFactory JavaDoc cf2 = (ConnectionFactory JavaDoc) ctx.lookup("java:/JBossTestCF2");
85          Connection JavaDoc 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 JavaDoc e)
104       {
105          log.warn("Unexpected: ", e);
106          throw new EJBException JavaDoc("unexpected exception: " + e);
107       }
108    }
109
110    /**
111     * Describe <code>testXAException</code> method here.
112     * @ejb.interface-method
113     */

114    public void testRMERRInOnePCToTransactionRolledbackException()
115    {
116       try
117       {
118
119          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
120          ConnectionFactory JavaDoc cf1 = (ConnectionFactory JavaDoc) 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 JavaDoc e)
134       {
135          log.warn("Unexpected: ", e);
136          throw new EJBException JavaDoc("unexpected exception: " + e);
137       }
138    }
139
140    /**
141     * Similate a connection failure
142     *
143     * @ejb.interface-method
144     */

145    public void simulateConnectionError()
146    {
147       log.info("Simulating connection error");
148       try
149       {
150          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
151          ConnectionFactory JavaDoc cf = (ConnectionFactory JavaDoc) 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 JavaDoc e)
163       {
164          if (e.getMessage().equals("Simulated exception") == false)
165          {
166             log.warn("Unexpected: ", e);
167             throw new EJBException JavaDoc(e.getMessage());
168          }
169          else
170          {
171             sessionContext.setRollbackOnly();
172          }
173       }
174    }
175
176    /**
177     * Similate a connection failure
178     *
179     * @ejb.interface-method
180     */

181    public void simulateConnectionErrorWithTwoHandles()
182    {
183       log.info("Simulating connection error with two handles");
184       try
185       {
186          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
187          ConnectionFactory JavaDoc cf = (ConnectionFactory JavaDoc) 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 JavaDoc ignored)
201             {
202             }
203             try
204             {
205                c2.close();
206             }
207             catch (Throwable JavaDoc ignored)
208             {
209             }
210          }
211       }
212       catch (Exception JavaDoc e)
213       {
214          if (e.getMessage().equals("Simulated exception") == false)
215          {
216             log.warn("Unexpected: ", e);
217             throw new EJBException JavaDoc(e.getMessage());
218          }
219          else
220          {
221             sessionContext.setRollbackOnly();
222          }
223       }
224    }
225
226    /**
227     * Similate an exception
228     *
229     * @ejb.interface-method
230     */

231    public void simulateError(String JavaDoc failure, int count)
232    {
233       log.info(failure + " teststart");
234       try
235       {
236          long available = getAvailableConnections();
237          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
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 JavaDoc expected)
247             {
248             }
249          }
250          if (available != getAvailableConnections())
251             throw new EJBException JavaDoc("Expected " + available + " got " + getAvailableConnections() + " connections");
252       }
253       catch (Exception JavaDoc e)
254       {
255          log.warn("Unexpected: ", e);
256          throw new EJBException JavaDoc(e.getMessage());
257       }
258    }
259
260    /**
261     * Similate an exception
262     *
263     * @ejb.interface-method
264     */

265    public void simulateFactoryError(String JavaDoc failure, int count)
266    {
267       log.info(failure + " start");
268       TestConnectionFactory cf = null;
269       try
270       {
271          long available = getAvailableConnections();
272          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
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 JavaDoc expected)
283             {
284             }
285          }
286          if (available != getAvailableConnections())
287             throw new EJBException JavaDoc("Expected " + available + " got " + getAvailableConnections() + " connections");
288       }
289       catch (Exception JavaDoc e)
290       {
291          log.warn("Unexpected: ", e);
292          throw new EJBException JavaDoc(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 JavaDoc
303    {
304       MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
305       return ((Long JavaDoc) server.getAttribute(new ObjectName JavaDoc("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 JavaDoc ctx)
322    {
323       sessionContext = ctx;
324    }
325
326    public void unsetSessionContext()
327    {
328       sessionContext = null;
329    }
330
331 }
332
Popular Tags