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 java.rmi.RemoteException;
025: import java.sql.Connection;
026: import java.sql.SQLException;
027:
028: import javax.ejb.CreateException;
029: import javax.ejb.EJBException;
030: import javax.ejb.SessionBean;
031: import javax.ejb.SessionContext;
032: import javax.management.MBeanServer;
033: import javax.management.ObjectName;
034: import javax.naming.InitialContext;
035:
036: import org.jboss.logging.Logger;
037: import org.jboss.mx.util.MBeanServerLocator;
038:
039: /**
040: * RollbackOnlyReleaseConnectionSessionBean.java
041: *
042: * @author <a href="mailto:noel.rocher@jboss.org">Noel Rocher</a>
043: * @version <tt>$Revision: 57211 $</tt>
044: *
045: * @ejb.bean name="RollbackOnlyReleaseConnectionSession"
046: * display-name="Name for RollbackOnlyReleaseConnectionSession"
047: * description="Description for RollbackOnlyReleaseConnectionSession"
048: * jndi-name="RollbackOnlyReleaseConnectionSession"
049: * type="Stateless"
050: * view-type="remote"
051: * transaction-type = "Container"
052: * @ejb.transaction type = "Required"
053: */
054: public class RollbackOnlyReleaseConnectionSessionBean implements
055: SessionBean {
056:
057: /** The serialVersionUID */
058: private static final long serialVersionUID = 1L;
059:
060: private SessionContext bean_context;
061:
062: private Logger log = Logger.getLogger(getClass());
063:
064: public void ejbCreate() throws CreateException {
065: }
066:
067: /**
068: *
069: */
070: public RollbackOnlyReleaseConnectionSessionBean() {
071: super ();
072: }
073:
074: /**
075: * @ejb.interface-method view-type = "both"
076: * @param in_name
077: */
078: public boolean testConnectionRelease()
079: throws java.rmi.RemoteException {
080: Connection conn = null;
081: long pre_connection_number = 0;
082: long post_connection_number = 0;
083: boolean result = false;
084: try {
085: // set Transaction to Rollback Only
086: this .bean_context.setRollbackOnly();
087:
088: Thread.sleep(500); // simulate some processing
089:
090: javax.sql.DataSource ds = (javax.sql.DataSource) (new InitialContext())
091: .lookup("java:DefaultDS");
092: pre_connection_number = getConnectionInUseNumber();
093: conn = ds.getConnection();
094: conn.createStatement().execute("select 1");
095: conn.close();
096:
097: } catch (SQLException e) {// this is the expected exception}
098: } catch (Exception e) {
099: log.warn("Unexpected ", e);
100: throw new EJBException("unexpected exception: " + e);
101: } finally {
102: try {
103: conn.close();
104: } catch (Exception ignore) {
105: }
106: }
107:
108: // compare in use connection numbers
109: try {
110: post_connection_number = getConnectionInUseNumber();
111: log.debug("Pre # = " + pre_connection_number + " ; Post #"
112: + post_connection_number);
113: if (pre_connection_number == post_connection_number) {
114: log.debug("Test is OK ");
115: result = true;
116: } else {
117: log.debug("Test is *NOT* OK ");
118: result = false;
119: }
120: } catch (Exception e) {
121: log.warn("Unexpected: ", e);
122: throw new EJBException("unexpected exception: " + e);
123: }
124:
125: return result;
126:
127: }
128:
129: private long getConnectionInUseNumber() throws Exception {
130: long result = 0;
131: MBeanServer server = MBeanServerLocator.locateJBoss();
132: result = ((Long) server
133: .getAttribute(
134: new ObjectName(
135: "jboss.jca:name=DefaultDS,service=ManagedConnectionPool"),
136: "InUseConnectionCount")).longValue();
137: return result;
138: }
139:
140: public void ejbActivate() throws EJBException, RemoteException {
141: }
142:
143: public void ejbPassivate() throws EJBException, RemoteException {
144: }
145:
146: public void ejbRemove() throws EJBException, RemoteException {
147: }
148:
149: public void setSessionContext(SessionContext ctx)
150: throws EJBException, RemoteException {
151:
152: bean_context = ctx;
153: }
154:
155: }
|