1 45 package org.openejb.test.stateless; 46 47 import java.rmi.RemoteException ; 48 import java.sql.Connection ; 49 import java.sql.PreparedStatement ; 50 import java.sql.ResultSet ; 51 52 import javax.ejb.CreateException ; 53 import javax.ejb.EJBException ; 54 import javax.ejb.SessionContext ; 55 import javax.naming.InitialContext ; 56 import javax.sql.DataSource ; 57 import javax.transaction.RollbackException ; 58 import javax.transaction.UserTransaction ; 59 60 import org.openejb.test.object.Account; 61 import org.openejb.test.object.Transaction; 62 63 68 public class BeanTxStatelessBean implements javax.ejb.SessionBean { 69 70 71 private String name; 72 private SessionContext ejbContext; 73 private InitialContext jndiContext; 74 public final String jndiDatabaseEntry = "jdbc/stateless/beanManagedTransaction/database"; 75 76 77 78 82 86 87 91 public Transaction getUserTransaction() throws RemoteException { 92 93 UserTransaction ut = null; 94 try{ 95 ut = ejbContext.getUserTransaction(); 96 } catch (IllegalStateException ise){ 97 throw new RemoteException (ise.getMessage()); 98 } 99 if (ut == null) return null; 100 return new Transaction (ut); 101 } 102 103 public Transaction jndiUserTransaction() throws RemoteException { 104 UserTransaction ut = null; 105 try{ 106 ut = (UserTransaction )jndiContext.lookup("java:comp/UserTransaction"); 107 } catch (Exception e){ 108 throw new RemoteException (e.getMessage()); 109 } 110 if (ut == null) return null; 111 return new Transaction (ut); 112 } 113 114 public void openAccount(Account acct, Boolean rollback) throws RemoteException , RollbackException { 115 116 try{ 117 DataSource ds = (DataSource )javax.rmi.PortableRemoteObject.narrow( jndiContext.lookup("java:comp/env/database"), DataSource .class); 118 Connection con = ds.getConnection(); 119 120 UserTransaction ut = ejbContext.getUserTransaction(); 121 122 ut.begin(); 123 124 125 126 PreparedStatement stmt = con.prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)"); 127 stmt.setString(1, acct.getSsn()); 128 stmt.setString(2, acct.getFirstName()); 129 stmt.setString(3, acct.getLastName()); 130 stmt.setInt(4, acct.getBalance()); 131 stmt.executeUpdate(); 132 133 134 if (rollback.booleanValue()) ut.setRollbackOnly(); 135 136 137 ut.commit(); 138 139 140 141 stmt.close(); 142 con.close(); 143 } catch (RollbackException re){ 144 throw re; 145 } catch (Exception e){ 146 e.printStackTrace(); 147 throw new RemoteException ("[Bean] "+e.getClass().getName()+" : "+e.getMessage()); 148 } 149 } 150 151 public Account retreiveAccount(String ssn) throws RemoteException { 152 Account acct = new Account(); 153 try{ 154 DataSource ds = (DataSource )javax.rmi.PortableRemoteObject.narrow( jndiContext.lookup("java:comp/env/database"), DataSource .class); 155 Connection con = ds.getConnection(); 156 157 PreparedStatement stmt = con.prepareStatement("select * from Account where SSN = ?"); 158 stmt.setString(1, ssn); 159 ResultSet rs = stmt.executeQuery(); 160 if (!rs.next()) return null; 161 162 acct.setSsn( rs.getString(1) ); 163 acct.setFirstName( rs.getString(2) ); 164 acct.setLastName( rs.getString(3) ); 165 acct.setBalance( rs.getInt(4) ); 166 167 stmt.close(); 168 con.close(); 169 } catch (Exception e){ 170 e.printStackTrace(); 171 throw new RemoteException ("[Bean] "+e.getClass().getName()+" : "+e.getMessage()); 172 } 173 return acct; 174 } 175 176 177 181 182 190 public void ejbCreate() throws javax.ejb.CreateException { 191 try { 192 jndiContext = new InitialContext (); 193 } catch (Exception e){ 194 throw new CreateException ("Can not get the initial context: "+e.getMessage()); 195 } 196 } 197 201 public void setSessionContext(SessionContext ctx) throws EJBException ,RemoteException { 202 ejbContext = ctx; 203 } 204 210 public void ejbRemove() throws EJBException ,RemoteException { 211 } 212 217 public void ejbActivate() throws EJBException ,RemoteException { 218 } 219 224 public void ejbPassivate() throws EJBException ,RemoteException { 225 } 226 230 } 231 | Popular Tags |