1 45 package org.openejb.test.stateful; 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 BeanTxStatefulBean implements javax.ejb.SessionBean { 69 70 71 private String name; 72 private SessionContext ejbContext; 73 private InitialContext jndiContext; 74 public final String jndiDatabaseEntry = "jdbc/stateful/beanManagedTransaction/database"; 75 76 77 78 88 public void ejbCreate(String name) 89 throws javax.ejb.CreateException { 90 this.name = name; 91 try { 92 jndiContext = new InitialContext (); 93 } catch (Exception e){ 94 throw new CreateException ("Can not get the initial context: "+e.getMessage()); 95 } 96 } 97 101 102 106 112 public Transaction getUserTransaction() throws RemoteException { 113 114 UserTransaction ut = null; 115 try{ 116 ut = ejbContext.getUserTransaction(); 117 } catch (IllegalStateException ise){ 118 throw new RemoteException (ise.getMessage()); 119 } 120 if (ut == null) return null; 121 return new Transaction (ut); 122 } 123 124 public Transaction jndiUserTransaction() throws RemoteException { 125 UserTransaction ut = null; 126 try{ 127 ut = (UserTransaction )jndiContext.lookup("java:comp/UserTransaction"); 128 } catch (Exception e){ 129 throw new RemoteException (e.getMessage()); 130 } 131 if (ut == null) return null; 132 return new Transaction (ut); 133 } 134 135 public void openAccount(Account acct, Boolean rollback) throws RemoteException , RollbackException { 136 137 try{ 138 139 DataSource ds = (DataSource )javax.rmi.PortableRemoteObject.narrow( jndiContext.lookup("java:comp/env/datasource"), DataSource .class); 140 Connection con = ds.getConnection(); 141 142 UserTransaction ut = ejbContext.getUserTransaction(); 143 144 ut.begin(); 145 146 147 148 PreparedStatement stmt = con.prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)"); 149 stmt.setString(1, acct.getSsn()); 150 stmt.setString(2, acct.getFirstName()); 151 stmt.setString(3, acct.getLastName()); 152 stmt.setInt(4, acct.getBalance()); 153 stmt.executeUpdate(); 154 155 156 if (rollback.booleanValue()) ut.setRollbackOnly(); 157 158 159 ut.commit(); 160 161 162 163 stmt.close(); 164 con.close(); 165 } catch (RollbackException re){ 166 throw re; 167 } catch (Exception e){ 168 e.printStackTrace(); 169 throw new RemoteException ("[Bean] "+e.getClass().getName()+" : "+e.getMessage()); 170 } 171 } 172 173 public Account retreiveAccount(String ssn) throws RemoteException { 174 Account acct = new Account(); 175 try{ 176 DataSource ds = (DataSource )javax.rmi.PortableRemoteObject.narrow( jndiContext.lookup("java:comp/env/datasource"), DataSource .class); 177 Connection con = ds.getConnection(); 178 179 PreparedStatement stmt = con.prepareStatement("select * from Account where SSN = ?"); 180 stmt.setString(1, ssn); 181 ResultSet rs = stmt.executeQuery(); 182 if (!rs.next()) return null; 183 184 acct.setSsn( rs.getString(1) ); 185 acct.setFirstName( rs.getString(2) ); 186 acct.setLastName( rs.getString(3) ); 187 acct.setBalance( rs.getInt(4) ); 188 189 stmt.close(); 190 con.close(); 191 } catch (Exception e){ 192 e.printStackTrace(); 193 throw new RemoteException ("[Bean] "+e.getClass().getName()+" : "+e.getMessage()); 194 } 195 return acct; 196 } 197 198 199 203 204 211 public void setSessionContext(SessionContext ctx) throws EJBException ,RemoteException { 212 ejbContext = ctx; 213 } 214 220 public void ejbRemove() throws EJBException ,RemoteException { 221 } 222 227 public void ejbActivate() throws EJBException ,RemoteException { 228 } 229 234 public void ejbPassivate() throws EJBException ,RemoteException { 235 } 236 240 } 241 | Popular Tags |