1 22 package org.jboss.test.jca.bank.ejb; 23 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 import java.util.*; 28 import javax.ejb.EJBException ; 29 import javax.ejb.SessionBean ; 30 import javax.ejb.SessionContext ; 31 import javax.naming.InitialContext ; 32 import javax.sql.DataSource ; 33 import org.jboss.logging.Logger; 34 import org.jboss.test.jca.bank.interfaces.Account; 35 import org.jboss.test.jca.bank.interfaces.AccountHome; 36 import org.jboss.test.jca.bank.interfaces.AccountLocal; 37 import org.jboss.test.jca.bank.interfaces.AccountLocalHome; 38 39 40 54 public class TellerBean 55 implements SessionBean 56 { 57 static int invocations; 58 59 private Connection c; 60 61 67 public void setUp() 68 { 69 try 70 { 71 tearDown(); 72 } 73 catch (Exception e) 74 { 75 } 78 try 79 { 80 Statement s = getConnection().createStatement(); 81 s.execute("CREATE TABLE CCBMPCUSTOMER (ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(64))"); 82 s.execute("CREATE TABLE CCBMPACCOUNT (ID INTEGER NOT NULL PRIMARY KEY, BALANCE INTEGER NOT NULL, CUSTOMERID INTEGER)"); 83 s.close(); 84 85 } 86 catch (Exception e) 87 { 88 throw new EJBException (e); 89 } 91 } 92 93 99 public void tearDown() 100 { 101 try 102 { 103 Statement s = getConnection().createStatement(); 104 s.execute("DROP TABLE CCBMPCUSTOMER"); 105 s.execute("DROP TABLE CCBMPACCOUNT"); 106 s.close(); 107 108 } 109 catch (Exception e) 110 { 111 throw new EJBException (e); 112 } 114 } 115 116 123 public boolean equals(Object other) 124 { 125 return other.getClass() == this.getClass(); 126 } 127 128 public int hashCode() 129 { 130 return 1; 131 } 132 133 142 public void transfer(Account from, Account to, int amount) 143 { 144 try 145 { 146 Logger.getLogger(TellerBean.class.getName()).info("Invocation #"+invocations++); 147 from.withdraw(amount); 148 to.deposit(amount); 149 } catch (Exception e) 150 { 151 throw new EJBException ("Could not transfer "+amount+" from "+from+" to "+to, e); 152 } 153 } 154 155 163 public Account createAccount(Integer id) 164 { 165 try 166 { 167 AccountHome home = (AccountHome)new InitialContext ().lookup("Account"); 168 Account acct = home.create(id, 0, null); 169 170 return acct; 171 } catch (Exception e) 172 { 173 throw new EJBException ("Could not create account", e); 174 } 175 } 176 177 185 public int getAccountBalance(Integer id) 186 { 187 try 188 { 189 AccountLocalHome home = (AccountLocalHome)new InitialContext ().lookup("AccountLocal"); 190 AccountLocal a = home.findByPrimaryKey(id); 191 return a.getBalance(); 192 } catch (Exception e) 193 { 194 Logger.getLogger(getClass().getName()).info("getAccountBalance failed", e); 195 throw new EJBException ("Could not get account for id " + id, e); 196 } 197 } 198 199 200 211 public void transferTest(AccountLocal from, AccountLocal to, int amount, int iter) 212 { 213 for (int i = 0; i < iter; i++) 214 { 215 from.withdraw(amount); 216 to.deposit(amount); 217 } 218 } 219 220 public void ejbCreate() 221 { 222 } 223 224 public void ejbActivate() 225 { 226 } 227 228 public void ejbPassivate() 229 { 230 if (c != null) 231 { 232 try 233 { 234 c.close(); 235 } 236 catch (SQLException e) 237 { 238 Logger.getLogger(getClass().getName()).info("SQLException closing c: " + e); 239 } c = null; 241 } } 243 244 public void ejbRemove() 245 { 246 } 247 248 public void setSessionContext(SessionContext ctx) 249 { 250 } 251 252 public void unsetSessionContext() 253 { 254 } 255 256 private Connection getConnection() throws Exception 257 { 258 if (c == null) 259 { 260 DataSource ds = (DataSource )new InitialContext ().lookup("java:/DefaultDS"); 261 c = ds.getConnection(); 262 263 } 265 return c; 266 } 267 } 268 | Popular Tags |