package org.jboss.test.jca.ejb;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Category;
import org.jboss.test.jca.interfaces.CachedConnectionSessionLocal;
public class CachedConnectionSessionBean implements SessionBean {
private Connection conn;
private Category log = Category.getInstance(getClass().getName());
private SessionContext ctx;
public void createTable()
{
try
{
dropTable();
}
catch (Exception e)
{
}
try
{
Statement s = getConn().createStatement();
try
{
s.execute("CREATE TABLE TESTCACHEDCONN (ID NUMERIC(18,0) NOT NULL PRIMARY KEY, VAL VARCHAR(255))");
}
finally
{
s.close();
} }
catch (SQLException e)
{
log.error("sql exception in create table", e);
} }
public void dropTable()
{
try
{
Statement s = getConn().createStatement();
try
{
s.execute("DROP TABLE TESTCACHEDCONN");
}
finally
{
s.close();
} }
catch (SQLException e)
{
log.error("sql exception in drop", e);
} }
public void insert(long id, String value)
{
try
{
PreparedStatement p = getConn().prepareStatement("INSERT INTO TESTCACHEDCONN (ID, VAL) VALUES (?, ?)");
try
{
p.setLong(1, id);
p.setString(2, value);
p.execute();
}
finally
{
p.close();
} }
catch (SQLException e)
{
log.error("sql exception in insert", e);
} }
public String fetch(long id)
{
try
{
PreparedStatement p = getConn().prepareStatement("SELECT VAL FROM TESTCACHEDCONN WHERE ID = ?");
ResultSet rs = null;
try
{
p.setLong(1, id);
rs = p.executeQuery();
if (rs.next())
{
return rs.getString(1);
} return null;
}
finally
{
rs.close();
p.close();
} }
catch (SQLException e)
{
log.error("sql exception in fetch", e);
return null;
} }
private Connection getConn()
{
if (conn == null)
{
log.info("ejb activate never called, conn == null");
ejbActivate();
} if (conn == null)
{
throw new IllegalStateException("could not get a connection");
}
return conn;
}
public void firstTLTest()
{
try
{
CachedConnectionSessionLocal other = (CachedConnectionSessionLocal) ctx.getEJBLocalObject();
other.secondTLTest();
ThreadLocalDB.close();
}
catch (Exception e)
{
log.info("Error", e);
throw new EJBException(e.toString());
}
}
public void secondTLTest()
{
try
{
Connection c = ThreadLocalDB.open();
c.createStatement().close();
}
catch (Exception e)
{
log.info("Error", e);
throw new EJBException(e.toString());
}
}
public void ejbCreate()
{
}
public void ejbActivate()
{
log = Category.getInstance(getClass());
try
{
DataSource ds = (DataSource)new InitialContext().lookup("java:DefaultDS");
conn = ds.getConnection();
}
catch (NamingException e)
{
log.error("naming exception in activate", e);
} catch (SQLException e)
{
log.error("sql exception in activate", e);
}
}
public void ejbPassivate() throws RemoteException
{
try
{
conn.close();
}
catch (SQLException e)
{
log.error("sql exception in passivate", e);
} conn = null;
log = null;
}
public void ejbRemove() throws RemoteException
{
}
public void setSessionContext(SessionContext ctx) throws RemoteException
{
this.ctx = ctx;
}
public void unsetSessionContext() throws RemoteException
{
}
}