package org.jboss.test.jca.bank.ejb;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
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.test.jca.bank.interfaces.Account;
import org.jboss.test.jca.bank.interfaces.AccountHome;
import org.jboss.test.jca.bank.interfaces.AccountLocal;
import org.jboss.test.jca.bank.interfaces.AccountLocalHome;
public class TellerBean
implements SessionBean
{
static int invocations;
private Connection c;
public void setUp()
{
try
{
tearDown();
}
catch (Exception e)
{
}
try
{
Statement s = getConnection().createStatement();
s.execute("CREATE TABLE CCBMPCUSTOMER (ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(64))");
s.execute("CREATE TABLE CCBMPACCOUNT (ID INTEGER NOT NULL PRIMARY KEY, BALANCE INTEGER NOT NULL, CUSTOMERID INTEGER)");
s.close();
}
catch (Exception e)
{
throw new EJBException(e);
}
}
public void tearDown()
{
try
{
Statement s = getConnection().createStatement();
s.execute("DROP TABLE CCBMPCUSTOMER");
s.execute("DROP TABLE CCBMPACCOUNT");
s.close();
}
catch (Exception e)
{
throw new EJBException(e);
}
}
public boolean equals(Object other)
{
return other.getClass() == this.getClass();
}
public int hashCode()
{
return 1;
}
public void transfer(Account from, Account to, int amount)
{
try
{
Category.getInstance(TellerBean.class.getName()).info("Invocation #"+invocations++);
from.withdraw(amount);
to.deposit(amount);
} catch (Exception e)
{
throw new EJBException("Could not transfer "+amount+" from "+from+" to "+to, e);
}
}
public Account createAccount(Integer id)
{
try
{
AccountHome home = (AccountHome)new InitialContext().lookup("Account");
Account acct = home.create(id, 0, null);
return acct;
} catch (Exception e)
{
throw new EJBException("Could not create account", e);
}
}
public int getAccountBalance(Integer id)
{
try
{
AccountLocalHome home = (AccountLocalHome)new InitialContext().lookup("AccountLocal");
AccountLocal a = home.findByPrimaryKey(id);
return a.getBalance();
} catch (Exception e)
{
Category.getInstance(getClass().getName()).info("getAccountBalance failed", e);
throw new EJBException("Could not get account for id " + id, e);
}
}
public void transferTest(AccountLocal from, AccountLocal to, int amount, int iter)
{
for (int i = 0; i < iter; i++)
{
from.withdraw(amount);
to.deposit(amount);
}
}
public void ejbCreate()
{
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
if (c != null)
{
try
{
c.close();
}
catch (SQLException e)
{
Category.getInstance(getClass().getName()).info("SQLException closing c: " + e);
} c = null;
} }
public void ejbRemove()
{
}
public void setSessionContext(SessionContext ctx)
{
}
public void unsetSessionContext()
{
}
private Connection getConnection() throws Exception
{
if (c == null)
{
DataSource ds = (DataSource)new InitialContext().lookup("java:/DefaultDS");
c = ds.getConnection();
}
return c;
}
}