KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > SB_StoreBidBean


1 package edu.rice.rubis.beans;
2
3 import java.rmi.RemoteException JavaDoc;
4 import javax.ejb.SessionBean JavaDoc;
5 import javax.ejb.SessionContext JavaDoc;
6 import javax.ejb.FinderException JavaDoc;
7 import javax.ejb.ObjectNotFoundException JavaDoc;
8 import javax.ejb.CreateException JavaDoc;
9 import javax.ejb.RemoveException JavaDoc;
10 import javax.ejb.EJBException JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.InitialContext JavaDoc;
13 import javax.rmi.PortableRemoteObject JavaDoc;
14 import javax.sql.DataSource JavaDoc;
15 import java.sql.Connection JavaDoc;
16 import java.sql.PreparedStatement JavaDoc;
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.SQLException JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import javax.transaction.UserTransaction JavaDoc;
21
22 /**
23  * This is a stateless session bean used to create e new bid for an item.
24  *
25  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
26  * @version 1.1
27  */

28
29 public class SB_StoreBidBean implements SessionBean JavaDoc
30 {
31   protected SessionContext JavaDoc sessionContext;
32   protected Context JavaDoc initialContext = null;
33   protected DataSource JavaDoc dataSource = null;
34   private UserTransaction JavaDoc utx = null;
35
36   /**
37    * Create a new bid and update the number of bids in the item.
38    *
39    * @param userId id of the user who is bidding
40    * @param itemId id of the item related to the bid
41    * @param bid value of the bid
42    * @param maxBid maximum bid
43    * @param qty quantity of items
44    * @since 1.1
45    */

46   public void createBid(int userId, int itemId, float bid, float maxBid, int qty) throws RemoteException JavaDoc
47   {
48     PreparedStatement JavaDoc stmt = null;
49     ResultSet JavaDoc rs = null;
50     Connection JavaDoc conn = null;
51
52     utx = sessionContext.getUserTransaction();
53     try
54     {
55       utx.begin();
56       try
57       {
58         // create new bid
59
conn = dataSource.getConnection();
60         String JavaDoc 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 JavaDoc e)
68       {
69         try { stmt.close(); } catch (Exception JavaDoc ignore) {}
70         try { conn.close(); } catch (Exception JavaDoc ignore) {}
71         throw new RemoteException JavaDoc("Error while storing the bid (got exception: " +e+")<br>");
72       }
73       // update the number of bids and the max bid for the item
74
PreparedStatement JavaDoc 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 JavaDoc ex)
107       {
108         try { update.close(); } catch (Exception JavaDoc ignore) {}
109         try { stmt.close(); } catch (Exception JavaDoc ignore) {}
110         try { conn.close(); } catch (Exception JavaDoc ignore) {}
111         throw new RemoteException JavaDoc("Failed to update nb of bids and max bid: " + ex);
112       }
113       if (conn != null) conn.close();
114       utx.commit();
115     }
116     catch (Exception JavaDoc e)
117     {
118       try { conn.close(); } catch (Exception JavaDoc ignore) {}
119       try
120       {
121         utx.rollback();
122         throw new RemoteException JavaDoc("Failed to create a new bid (got exception: " +e+")<br>");
123       }
124       catch (Exception JavaDoc se)
125       {
126         throw new RemoteException JavaDoc("Transaction rollback failed: " + e +"<br>");
127       }
128     }
129   }
130
131
132
133   // ======================== EJB related methods ============================
134

135   /**
136    * This method is empty for a stateless session bean
137    */

138   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
139   {
140   }
141
142   /** This method is empty for a stateless session bean */
143   public void ejbActivate() throws RemoteException JavaDoc {}
144   /** This method is empty for a stateless session bean */
145   public void ejbPassivate() throws RemoteException JavaDoc {}
146   /** This method is empty for a stateless session bean */
147   public void ejbRemove() throws RemoteException JavaDoc {}
148
149
150   /**
151    * Sets the associated session context. The container calls this method
152    * after the instance creation. This method is called with no transaction context.
153    * We also retrieve the Home interfaces of all RUBiS's beans.
154    *
155    * @param sessionContext - A SessionContext interface for the instance.
156    * @exception RemoteException - Thrown if the instance could not perform the function
157    * requested by the container because of a system-level error.
158    */

159   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
160   {
161     this.sessionContext = sessionContext;
162     if (dataSource == null)
163     {
164       // Finds DataSource from JNDI
165

166       try
167       {
168         initialContext = new InitialContext JavaDoc();
169         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
170       }
171       catch (Exception JavaDoc e)
172       {
173         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
174       }
175     }
176   }
177
178 }
179
Popular Tags