KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.rmi.RemoteException JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.EJBException JavaDoc;
30 import javax.ejb.SessionBean JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35
36 import org.jboss.logging.Logger;
37 import org.jboss.mx.util.MBeanServerLocator;
38
39 /**
40  * RollbackOnlyReleaseConnectionSessionBean.java
41  *
42  * @author <a HREF="mailto:noel.rocher@jboss.org">Noel Rocher</a>
43  * @version <tt>$Revision: 37406 $</tt>
44  *
45  * @ejb.bean name="RollbackOnlyReleaseConnectionSession"
46  * display-name="Name for RollbackOnlyReleaseConnectionSession"
47  * description="Description for RollbackOnlyReleaseConnectionSession"
48  * jndi-name="RollbackOnlyReleaseConnectionSession"
49  * type="Stateless"
50  * view-type="remote"
51  * transaction-type = "Container"
52  * @ejb.transaction type = "Required"
53  */

54 public class RollbackOnlyReleaseConnectionSessionBean implements SessionBean JavaDoc
55 {
56
57    /** The serialVersionUID */
58    private static final long serialVersionUID = 1L;
59
60    private SessionContext JavaDoc bean_context;
61
62    private Logger log = Logger.getLogger(getClass());
63
64    public void ejbCreate() throws CreateException JavaDoc
65    {
66    }
67
68    /**
69     *
70     */

71    public RollbackOnlyReleaseConnectionSessionBean()
72    {
73       super();
74    }
75
76    /**
77     * @ejb.interface-method view-type = "both"
78     * @param in_name
79     */

80    public boolean testConnectionRelease() throws java.rmi.RemoteException JavaDoc
81    {
82       Connection JavaDoc conn = null;
83       long pre_connection_number = 0;
84       long post_connection_number = 0;
85       boolean result = false;
86       try
87       {
88          // set Transaction to Rollback Only
89
this.bean_context.setRollbackOnly();
90
91          Thread.sleep(500); // simulate some processing
92

93          javax.sql.DataSource JavaDoc ds = (javax.sql.DataSource JavaDoc) (new InitialContext JavaDoc()).lookup("java:DefaultDS");
94          pre_connection_number = getConnectionInUseNumber();
95          conn = ds.getConnection();
96          conn.createStatement().execute("select 1");
97          conn.close();
98
99       }
100       catch (SQLException JavaDoc e)
101       {// this is the expected exception}
102
}
103       catch (Exception JavaDoc e)
104       {
105          log.warn("Unexpected ", e);
106          throw new EJBException JavaDoc("unexpected exception: " + e);
107       }
108       finally
109       {
110          try
111          {
112             conn.close();
113          }
114          catch (Exception JavaDoc ignore)
115          {
116          }
117       }
118
119       // compare in use connection numbers
120
try
121       {
122          post_connection_number = getConnectionInUseNumber();
123          log.debug("Pre # = " + pre_connection_number + " ; Post #" + post_connection_number);
124          if (pre_connection_number == post_connection_number)
125          {
126             log.debug("Test is OK ");
127             result = true;
128          }
129          else
130          {
131             log.debug("Test is *NOT* OK ");
132             result = false;
133          }
134       }
135       catch (Exception JavaDoc e)
136       {
137          log.warn("Unexpected: ", e);
138          throw new EJBException JavaDoc("unexpected exception: " + e);
139       }
140
141       return result;
142
143    }
144
145    private long getConnectionInUseNumber() throws Exception JavaDoc
146    {
147       long result = 0;
148       MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
149       result = ((Long JavaDoc) server.getAttribute(
150             new ObjectName JavaDoc("jboss.jca:name=DefaultDS,service=ManagedConnectionPool"), "InUseConnectionCount"))
151             .longValue();
152       return result;
153    }
154
155    public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc
156    {
157    }
158
159    public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc
160    {
161    }
162
163    public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc
164    {
165    }
166
167    public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc, RemoteException JavaDoc
168    {
169
170       bean_context = ctx;
171    }
172
173 }
174
Popular Tags