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 59 import org.openejb.test.object.Account; 60 61 65 public class ContainerTxStatelessBean implements javax.ejb.SessionBean { 66 67 68 private String name; 69 private SessionContext ejbContext; 70 private InitialContext jndiContext; 71 public final String jndiDatabaseEntry = "jdbc/stateless/containerManagedTransaction/database"; 72 73 74 75 79 83 84 88 public String txMandatoryMethod(String message) { 89 return message; 90 } 91 92 public String txNeverMethod(String message) { 93 return message; 94 } 95 96 public String txNotSupportedMethod(String message) { 97 return message; 98 } 99 100 public String txRequiredMethod(String message) { 101 return message; 102 } 103 104 public String txRequiresNewMethod(String message) { 105 return message; 106 } 107 108 public String txSupportsMethod(String message) { 109 return message; 110 } 111 112 public void openAccount(Account acct, Boolean rollback) throws RollbackException { 113 114 try{ 115 DataSource ds = (DataSource )jndiContext.lookup("java:comp/env/database"); 116 Connection con = ds.getConnection(); 117 118 119 PreparedStatement stmt = con.prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)"); 120 stmt.setString(1, acct.getSsn()); 121 stmt.setString(2, acct.getFirstName()); 122 stmt.setString(3, acct.getLastName()); 123 stmt.setInt(4, acct.getBalance()); 124 stmt.executeUpdate(); 125 126 127 stmt.close(); 128 con.close(); 129 } catch (Exception e){ 130 } 132 } 133 134 public Account retreiveAccount(String ssn) { 135 Account acct = new Account(); 136 try{ 137 DataSource ds = (DataSource ) jndiContext.lookup("java:comp/env/database"); 138 Connection con = ds.getConnection(); 139 140 PreparedStatement stmt = con.prepareStatement("select * from Account where SSN = ?"); 141 stmt.setString(1, ssn); 142 ResultSet rs = stmt.executeQuery(); 143 if (!rs.next()) return null; 144 145 acct.setSsn( rs.getString(1) ); 146 acct.setFirstName( rs.getString(2) ); 147 acct.setLastName( rs.getString(3) ); 148 acct.setBalance( rs.getInt(4) ); 149 150 stmt.close(); 151 con.close(); 152 } catch (Exception e){ 153 } 155 return acct; 156 } 157 158 159 163 164 172 public void ejbCreate() throws javax.ejb.CreateException { 173 try { 174 jndiContext = new InitialContext (); 175 } catch (Exception e){ 176 throw new CreateException ("Can not get the initial context: "+e.getMessage()); 177 } 178 } 179 183 public void setSessionContext(SessionContext ctx) throws EJBException ,RemoteException { 184 ejbContext = ctx; 185 } 186 192 public void ejbRemove() throws EJBException ,RemoteException { 193 } 194 199 public void ejbActivate() throws EJBException ,RemoteException { 200 } 201 206 public void ejbPassivate() throws EJBException ,RemoteException { 207 } 208 212 } 213 | Popular Tags |