1 22 package org.jboss.test.jca.ejb; 23 24 import java.rmi.RemoteException ; 25 import java.sql.Connection ; 26 import java.sql.SQLException ; 27 28 import javax.ejb.CreateException ; 29 import javax.ejb.EJBException ; 30 import javax.ejb.SessionBean ; 31 import javax.ejb.SessionContext ; 32 import javax.management.MBeanServer ; 33 import javax.management.ObjectName ; 34 import javax.naming.InitialContext ; 35 36 import org.jboss.logging.Logger; 37 import org.jboss.mx.util.MBeanServerLocator; 38 39 54 public class RollbackOnlyReleaseConnectionSessionBean implements SessionBean 55 { 56 57 58 private static final long serialVersionUID = 1L; 59 60 private SessionContext bean_context; 61 62 private Logger log = Logger.getLogger(getClass()); 63 64 public void ejbCreate() throws CreateException 65 { 66 } 67 68 71 public RollbackOnlyReleaseConnectionSessionBean() 72 { 73 super(); 74 } 75 76 80 public boolean testConnectionRelease() throws java.rmi.RemoteException 81 { 82 Connection conn = null; 83 long pre_connection_number = 0; 84 long post_connection_number = 0; 85 boolean result = false; 86 try 87 { 88 this.bean_context.setRollbackOnly(); 90 91 Thread.sleep(500); 93 javax.sql.DataSource ds = (javax.sql.DataSource ) (new InitialContext ()).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 e) 101 { } 103 catch (Exception e) 104 { 105 log.warn("Unexpected ", e); 106 throw new EJBException ("unexpected exception: " + e); 107 } 108 finally 109 { 110 try 111 { 112 conn.close(); 113 } 114 catch (Exception ignore) 115 { 116 } 117 } 118 119 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 e) 136 { 137 log.warn("Unexpected: ", e); 138 throw new EJBException ("unexpected exception: " + e); 139 } 140 141 return result; 142 143 } 144 145 private long getConnectionInUseNumber() throws Exception 146 { 147 long result = 0; 148 MBeanServer server = MBeanServerLocator.locateJBoss(); 149 result = ((Long ) server.getAttribute( 150 new ObjectName ("jboss.jca:name=DefaultDS,service=ManagedConnectionPool"), "InUseConnectionCount")) 151 .longValue(); 152 return result; 153 } 154 155 public void ejbActivate() throws EJBException , RemoteException 156 { 157 } 158 159 public void ejbPassivate() throws EJBException , RemoteException 160 { 161 } 162 163 public void ejbRemove() throws EJBException , RemoteException 164 { 165 } 166 167 public void setSessionContext(SessionContext ctx) throws EJBException , RemoteException 168 { 169 170 bean_context = ctx; 171 } 172 173 } 174 | Popular Tags |