1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.SessionBean ; 5 import javax.ejb.SessionContext ; 6 import javax.ejb.FinderException ; 7 import javax.ejb.ObjectNotFoundException ; 8 import javax.ejb.CreateException ; 9 import javax.ejb.RemoveException ; 10 import javax.ejb.EJBException ; 11 import javax.naming.Context ; 12 import javax.naming.InitialContext ; 13 import javax.rmi.PortableRemoteObject ; 14 import javax.sql.DataSource ; 15 import java.sql.Connection ; 16 import java.sql.PreparedStatement ; 17 import java.sql.ResultSet ; 18 import java.sql.SQLException ; 19 import java.io.Serializable ; 20 import javax.transaction.UserTransaction ; 21 22 28 29 public class SB_StoreBidBean implements SessionBean 30 { 31 protected SessionContext sessionContext; 32 protected Context initialContext = null; 33 protected DataSource dataSource = null; 34 private UserTransaction utx = null; 35 36 46 public void createBid(int userId, int itemId, float bid, float maxBid, int qty) throws RemoteException 47 { 48 PreparedStatement stmt = null; 49 ResultSet rs = null; 50 Connection conn = null; 51 52 utx = sessionContext.getUserTransaction(); 53 try 54 { 55 utx.begin(); 56 try 57 { 58 conn = dataSource.getConnection(); 60 String now = TimeManagement.currentDateToString(); 61 stmt = conn.prepareStatement("INSERT INTO bids VALUES (NULL, \""+userId+ 62 "\", \""+itemId+"\", \""+qty+"\", \""+ 63 bid+"\", \""+maxBid+"\", \""+now+"\")"); 64 stmt.executeUpdate(); 65 stmt.close(); 66 } 67 catch (SQLException e) 68 { 69 try { stmt.close(); } catch (Exception ignore) {} 70 try { conn.close(); } catch (Exception ignore) {} 71 throw new RemoteException ("Error while storing the bid (got exception: " +e+")<br>"); 72 } 73 PreparedStatement update = null; 75 try 76 { 77 stmt = conn.prepareStatement("SELECT nb_of_bids, max_bid FROM items WHERE id=?"); 78 stmt.setInt(1, itemId); 79 rs = stmt.executeQuery(); 80 81 if (rs.first()) 82 { 83 int nbOfBids = rs.getInt("nb_of_bids"); 84 nbOfBids++; 85 float oldMaxBid = rs.getFloat("max_bid"); 86 if (bid > oldMaxBid) 87 { 88 oldMaxBid = bid; 89 update = conn.prepareStatement("UPDATE items SET max_bid=?,nb_of_bids=? WHERE id=?"); 90 update.setFloat(1, oldMaxBid); 91 update.setInt(2, nbOfBids); 92 update.setInt(3, itemId); 93 update.executeUpdate(); 94 } 95 else 96 { 97 update = conn.prepareStatement("UPDATE items SET nb_of_bids=? WHERE id=?"); 98 update.setInt(1, nbOfBids); 99 update.setInt(2, itemId); 100 update.executeUpdate(); 101 } 102 stmt.close(); 103 update.close(); 104 } 105 } 106 catch (Exception ex) 107 { 108 try { update.close(); } catch (Exception ignore) {} 109 try { stmt.close(); } catch (Exception ignore) {} 110 try { conn.close(); } catch (Exception ignore) {} 111 throw new RemoteException ("Failed to update nb of bids and max bid: " + ex); 112 } 113 if (conn != null) conn.close(); 114 utx.commit(); 115 } 116 catch (Exception e) 117 { 118 try { conn.close(); } catch (Exception ignore) {} 119 try 120 { 121 utx.rollback(); 122 throw new RemoteException ("Failed to create a new bid (got exception: " +e+")<br>"); 123 } 124 catch (Exception se) 125 { 126 throw new RemoteException ("Transaction rollback failed: " + e +"<br>"); 127 } 128 } 129 } 130 131 132 133 135 138 public void ejbCreate() throws CreateException , RemoteException 139 { 140 } 141 142 143 public void ejbActivate() throws RemoteException {} 144 145 public void ejbPassivate() throws RemoteException {} 146 147 public void ejbRemove() throws RemoteException {} 148 149 150 159 public void setSessionContext(SessionContext sessionContext) throws RemoteException 160 { 161 this.sessionContext = sessionContext; 162 if (dataSource == null) 163 { 164 166 try 167 { 168 initialContext = new InitialContext (); 169 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 170 } 171 catch (Exception e) 172 { 173 throw new RemoteException ("Cannot get JNDI InitialContext"); 174 } 175 } 176 } 177 178 } 179 | Popular Tags |