package org.jboss.test.jca.ejb;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.management.ObjectName;
import javax.naming.NamingException;
import org.apache.log4j.Category;
import javax.transaction.UserTransaction;
import javax.ejb.EJBException;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
import org.jboss.test.jca.adapter.TestConnectionFactory;
import org.jboss.test.jca.adapter.TestConnection;
public class RollbackOnlyReleaseConnectionSessionBean implements SessionBean {
private SessionContext bean_context;
private Category log = Category.getInstance(getClass().getName());
public void ejbCreate() throws CreateException
{
}
public RollbackOnlyReleaseConnectionSessionBean() {super();}
public boolean testConnectionRelease() throws java.rmi.RemoteException
{
Connection conn = null;
long pre_connection_number=0;
long post_connection_number=0;
boolean result=false;
try {
this.bean_context.setRollbackOnly();
Thread.sleep(500);
javax.sql.DataSource ds = (javax.sql.DataSource)(new InitialContext()).lookup("java:DefaultDS");
pre_connection_number = getConnectionInUseNumber();
conn = ds.getConnection();
conn.createStatement().execute("select 1");
conn.close();
} catch (org.jboss.util.NestedSQLException ignore) { } catch (Exception e) {
e.printStackTrace();
throw new EJBException("unexpected exception: " + e);
}
finally {
try {
conn.close();
} catch (Exception ignore) {}
}
try {
post_connection_number = getConnectionInUseNumber();
log.debug("Pre # = " + pre_connection_number + " ; Post #" + post_connection_number);
if (pre_connection_number == post_connection_number)
{
log.debug("Test is OK ");
result = true;
}
else
{
log.debug("Test is *NOT* OK ");
result = false;
}
} catch (Exception e) {
e.printStackTrace();
throw new EJBException("unexpected exception: " + e);
}
return result;
}
private long getConnectionInUseNumber() throws Exception
{
long result=0;
result = ((Long)((RMIAdaptor)(new InitialContext()).lookup("jmx/invoker/RMIAdaptor")).getAttribute(new ObjectName("jboss.jca:name=DefaultDS,service=ManagedConnectionPool"), "InUseConnectionCount")).longValue();
return result;
}
public void ejbActivate() throws EJBException, RemoteException {}
public void ejbPassivate() throws EJBException, RemoteException {}
public void ejbRemove() throws EJBException, RemoteException {}
public void setSessionContext(SessionContext ctx)
throws EJBException,
RemoteException {
bean_context=ctx;
}
}