package org.jboss.test.jca.ejb;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.Statement;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.log4j.Category;
import org.jboss.resource.adapter.jdbc.WrappedConnection;
import java.sql.ResultSet;
import javax.transaction.UserTransaction;
public class LocalWrapperCleanupTestSessionBean
implements SessionBean
{
private Category log = Category.getInstance(getClass().getName());
public void testAutoCommitInReturnedConnection()
{
try
{
DataSource ds = (DataSource)new InitialContext().lookup("java:/SingleConnectionDS");
Connection c = ds.getConnection();
if (c.getAutoCommit() == false)
{
throw new EJBException("Initial autocommit state false!");
} c.setAutoCommit(false);
c.commit();
c.close();
c = ds.getConnection();
if (c.getAutoCommit() == false)
{
throw new EJBException("Returned and reaccessed autocommit state false!");
} c.close();
}
catch (EJBException e)
{
throw e;
} catch (Exception e)
{
throw new EJBException("Untested problem in test: " + e);
} }
public void testAutoCommit()
{
try
{
DataSource ds = (DataSource)new InitialContext().lookup("java:/DefaultDS");
Connection c1 = ds.getConnection();
Connection uc1 = ((WrappedConnection)c1).getUnderlyingConnection();
if (c1.getAutoCommit() == false)
{
throw new EJBException("Initial autocommit state false!");
}
Connection c2 = ds.getConnection();
if (c2.getAutoCommit() == false)
{
throw new EJBException("Initial autocommit state false!");
} Statement s1 = c1.createStatement();
Statement s2 = c2.createStatement();
s1.execute("create table autocommittest (id integer)");
try
{
s1.execute("insert into autocommittest values (1)");
uc1.rollback();
ResultSet rs2 = s2.executeQuery("select * from autocommittest where id = 1");
if (!rs2.next())
{
throw new EJBException("Row not visible to other connection, autocommit failed");
} rs2.close();
}
finally
{
s1.execute("drop table autocommittest");
s1.close();
c1.close();
s2.close();
c2.close();
}
}
catch (EJBException e)
{
throw e;
} catch (Exception e)
{
throw new EJBException("Untested problem in test: " + e);
}
}
public void testAutoCommitOffInUserTx()
{
try
{
DataSource ds = (DataSource)new InitialContext().lookup("java:/DefaultDS");
Connection c1 = ds.getConnection();
Connection uc1 = ((WrappedConnection)c1).getUnderlyingConnection();
if (c1.getAutoCommit() == false)
{
throw new EJBException("Initial autocommit state false!");
}
Statement s1 = c1.createStatement();
s1.execute("create table autocommittest (id integer)");
try
{
UserTransaction ut = (UserTransaction)new InitialContext().lookup("UserTransaction");
ut.begin();
s1.execute("insert into autocommittest values (1)");
if (uc1.getAutoCommit())
{
throw new EJBException("Underlying autocommit is true in user tx!");
}
ut.rollback();
ResultSet rs1 = s1.executeQuery("select * from autocommittest where id = 1");
if (rs1.next())
{
throw new EJBException("Row committed, autocommit still on!");
} rs1.close();
}
finally
{
s1.execute("drop table autocommittest");
s1.close();
c1.close();
}
}
catch (EJBException e)
{
throw e;
} catch (Exception e)
{
throw new EJBException("Untested problem in test: " + e);
}
}
public void testAutoCommitOffInUserTx2()
{
try
{
createTable();
UserTransaction ut = (UserTransaction)new InitialContext().lookup("UserTransaction");
ut.begin();
insertAndCheckAutoCommit();
ut.rollback();
}
catch (EJBException e)
{
throw e;
} catch (Exception e)
{
throw new EJBException("Untested problem in test: " + e);
} finally
{
checkRowAndDropTable();
}
}
public void testReadOnly()
{
try
{
DataSource ds = (DataSource)new InitialContext().lookup("java:/DefaultDS");
Connection c1 = ds.getConnection();
Connection uc1 = ((WrappedConnection)c1).getUnderlyingConnection();
Statement s1 = null;
try
{
if (uc1.isReadOnly() == true)
throw new EJBException("Initial underlying readonly true!");
if (c1.isReadOnly() == true)
throw new EJBException("Initial readonly true!");
c1.setReadOnly(true);
if (uc1.isReadOnly() == true)
throw new EJBException("Read Only should be lazy!");
if (c1.isReadOnly() == false)
throw new EJBException("Changed readonly false!");
s1 = c1.createStatement();
if (uc1.isReadOnly() == false)
throw new EJBException("Lazy read only failed!");
if (c1.isReadOnly() == false)
throw new EJBException("Read only changed unexpectedly!");
}
finally
{
if (s1 != null)
s1.close();
c1.close();
} }
catch (EJBException e)
{
throw e;
}
catch (Exception e)
{
throw new EJBException("Untested problem in test: " + e);
}
}
public void createTable()
{
try
{
DataSource ds = (DataSource)new InitialContext().lookup("java:/DefaultDS");
Connection c1 = ds.getConnection();
Connection uc1 = ((WrappedConnection)c1).getUnderlyingConnection();
if (c1.getAutoCommit() == false)
{
throw new EJBException("Initial autocommit state false!");
}
Statement s1 = c1.createStatement();
try
{
s1.execute("create table autocommittest (id integer)");
}
finally
{
s1.close();
c1.close();
} }
catch (EJBException e)
{
throw e;
} catch (Exception e)
{
throw new EJBException("Untested problem in test: " + e);
}
}
public void insertAndCheckAutoCommit()
{
try
{
DataSource ds = (DataSource)new InitialContext().lookup("java:/DefaultDS");
Connection c1 = ds.getConnection();
Connection uc1 = ((WrappedConnection)c1).getUnderlyingConnection();
Statement s1 = c1.createStatement();
try
{
s1.execute("insert into autocommittest values (1)");
if (uc1.getAutoCommit())
{
throw new EJBException("Underlying autocommit is true in user tx!");
}
}
finally
{
s1.close();
c1.close();
}
}
catch (EJBException e)
{
throw e;
} catch (Exception e)
{
throw new EJBException("Untested problem in test: " + e);
}
}
public void checkRowAndDropTable()
{
try
{
DataSource ds = (DataSource)new InitialContext().lookup("java:/DefaultDS");
Connection c1 = ds.getConnection();
Connection uc1 = ((WrappedConnection)c1).getUnderlyingConnection();
if (c1.getAutoCommit() == false)
{
throw new EJBException("Initial autocommit state false!");
}
Statement s1 = c1.createStatement();
try
{
ResultSet rs1 = s1.executeQuery("select * from autocommittest where id = 1");
if (rs1.next())
{
throw new EJBException("Row committed, autocommit still on!");
} rs1.close();
}
finally
{
s1.execute("drop table autocommittest");
s1.close();
c1.close();
}
}
catch (EJBException e)
{
throw e;
} catch (Exception e)
{
throw new EJBException("Untested problem in test: " + e);
}
}
public void ejbCreate()
{
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
}
public void ejbRemove()
{
}
public void setSessionContext(SessionContext ctx)
{
}
public void unsetSessionContext()
{
}
}