1 51 52 package org.objectweb.jass.examples.travelagency.ejbs; 53 54 import org.objectweb.jass.examples.travelagency.exceptions.DBException; 55 import org 56 .objectweb 57 .jass 58 .examples 59 .travelagency 60 .exceptions 61 .NotEnoughRoomsException; 62 import org 63 .objectweb 64 .jass 65 .examples 66 .travelagency 67 .ejbs 68 .compensations 69 .ReserveRoomsCompensation; 70 71 import javax.ejb.EJBException ; 72 import javax.ejb.SessionBean ; 73 import javax.ejb.SessionContext ; 74 import javax.naming.Context ; 75 import javax.naming.InitialContext ; 76 import javax.naming.NamingException ; 77 import javax.sql.DataSource ; 78 import javax.activity.opennested.UserOpenNested; 79 import java.sql.Connection ; 80 import java.sql.Statement ; 81 import java.sql.ResultSet ; 82 import java.sql.SQLException ; 83 84 92 public class HotelBean implements SessionBean { 93 94 private UserOpenNested uon = null; 95 private DataSource ds = null; 96 97 public void ejbCreate() throws javax.ejb.CreateException { 98 uon = getUserOpenNested(); 99 ds = getDataSource(); 100 System.out.println("Hotel stateless EJB created!!!"); 101 } 102 103 public void ejbActivate() throws EJBException { 104 } 105 106 public void ejbPassivate() throws EJBException { 107 } 108 109 public void ejbRemove() throws EJBException { 110 uon = null; 111 ds = null; 112 System.out.println("Hotel stateless EJB removed!!!"); 113 } 114 115 public void setSessionContext(SessionContext sessionContext) 116 throws EJBException { 117 } 118 119 123 public int getFreeRooms() throws DBException { 124 Connection c = null; 125 Statement stmt = null; 126 ResultSet rs = null; 127 int rooms = -1; 128 129 try { 130 c = ds.getConnection(); 131 stmt = c.createStatement(); 132 rs = stmt.executeQuery("SELECT rooms FROM departments"); 133 if (rs.next()) { 134 rooms = rs.getInt(1); 135 } 136 rs.close(); 137 stmt.close(); 138 c.close(); 139 } catch (SQLException e) { 140 throw new DBException(); 141 } 142 return rooms; 143 } 144 145 152 public int reserveRooms(int nRooms) 153 throws NotEnoughRoomsException, DBException { 154 Connection c = null; 155 Statement stmt = null; 156 ResultSet rs = null; 157 int result = -1; 158 159 try { 160 uon.activityBegin(0); 161 c = ds.getConnection(); 162 stmt = c.createStatement(); 163 rs = stmt.executeQuery("SELECT rooms FROM departments"); 164 rs.next(); 165 int freeRooms = rs.getInt(1); 166 if ((freeRooms - nRooms) < 0) { 167 rs.close(); 168 stmt.close(); 169 c.close(); 170 uon.activityRollback(); 171 throw new NotEnoughRoomsException(); 172 } else { 173 stmt.executeUpdate( 174 "UPDATE departments SET rooms = " + (freeRooms - nRooms)); 175 ReserveRoomsCompensation compensation = 176 new ReserveRoomsCompensation(this, nRooms); 177 uon.activityCommit(compensation); 178 } 179 rs.close(); 180 stmt.close(); 181 c.close(); 182 result = nRooms; 183 } catch (SQLException e) { 184 try { 185 uon.activityRollback(); 186 } catch (Exception e1) { 187 e1.printStackTrace(); 188 } 189 throw new DBException(); 190 } catch (NotEnoughRoomsException e) { 191 throw e; 192 } catch (Exception e) { 193 e.printStackTrace(); 194 } 195 return result; 196 } 197 198 203 public int unreserveRooms(int nRooms) throws DBException { 204 Connection c = null; 205 Statement stmt = null; 206 ResultSet rs = null; 207 int result = -1; 208 209 try { 210 uon.activityBegin(0); 211 c = ds.getConnection(); 212 stmt = c.createStatement(); 213 rs = stmt.executeQuery("SELECT rooms FROM departments"); 214 rs.next(); 215 int freeRooms = rs.getInt(1); 216 stmt.executeUpdate( 217 "UPDATE departments SET rooms = " + (freeRooms + nRooms)); 218 uon.activityCommit(null); 219 rs.close(); 220 stmt.close(); 221 c.close(); 222 result = nRooms; 223 } catch (SQLException e) { 224 try { 225 uon.activityRollback(); 226 } catch (Exception e1) { 227 e1.printStackTrace(); 228 } 229 throw new DBException(); 230 } catch (Exception e) { 231 e.printStackTrace(); 232 } 233 return result; 234 } 235 236 239 private UserOpenNested getUserOpenNested() { 240 UserOpenNested uon = null; 241 Context ctx = null; 242 243 try { 244 ctx = new InitialContext (); 245 uon = (UserOpenNested) ctx.lookup("UserOpenNested"); 246 } catch (Exception e) { 247 e.printStackTrace(); 248 } 249 return uon; 250 } 251 252 255 private DataSource getDataSource() { 256 Context ctx = null; 257 DataSource ds = null; 258 try { 259 ctx = new InitialContext (); 260 ds = (DataSource ) ctx.lookup("java:/AdaptHotelDS"); 261 } catch (NamingException e) { 262 e.printStackTrace(); 263 } 264 return ds; 265 } 266 } 267 | Popular Tags |