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 .NotEnoughSeatsException; 62 import org 63 .objectweb 64 .jass 65 .examples 66 .travelagency 67 .ejbs 68 .compensations 69 .ReserveSeatsCompensation; 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 AirlineBean implements SessionBean { 93 private UserOpenNested uon = null; 94 private DataSource ds = null; 95 96 public void ejbCreate() throws javax.ejb.CreateException { 97 uon = getUserOpenNested(); 98 ds = getDataSource(); 99 System.out.println("Airline stateless EJB created!!!"); 100 } 101 102 public void ejbActivate() throws EJBException { 103 } 104 105 public void ejbPassivate() throws EJBException { 106 } 107 108 public void ejbRemove() throws EJBException { 109 uon = null; 110 ds = null; 111 System.out.println("Airline stateless EJB removed!!!"); 112 } 113 114 public void setSessionContext(SessionContext sessionContext) 115 throws EJBException { 116 } 117 118 122 public int getFreeSeats() throws DBException { 123 Connection c = null; 124 Statement stmt = null; 125 ResultSet rs = null; 126 int seats = -1; 127 128 try { 129 c = ds.getConnection(); 130 stmt = c.createStatement(); 131 rs = stmt.executeQuery("SELECT seats FROM flights"); 132 if (rs.next()) { 133 seats = rs.getInt(1); 134 } 135 rs.close(); 136 stmt.close(); 137 c.close(); 138 } catch (SQLException e) { 139 throw new DBException(); 140 } 141 return seats; 142 } 143 144 151 public int reserveSeats(int nSeats) 152 throws NotEnoughSeatsException, DBException { 153 Connection c = null; 154 Statement stmt = null; 155 ResultSet rs = null; 156 int result = -1; 157 158 try { 159 uon.activityBegin(0); 160 c = ds.getConnection(); 161 stmt = c.createStatement(); 162 rs = stmt.executeQuery("SELECT seats FROM flights"); 163 rs.next(); 164 int freeSeats = rs.getInt(1); 165 if ((freeSeats - nSeats) < 0) { 166 rs.close(); 167 stmt.close(); 168 c.close(); 169 uon.activityRollback(); 170 throw new NotEnoughSeatsException(); 171 } else { 172 stmt.executeUpdate( 173 "UPDATE flights SET seats = " + (freeSeats - nSeats)); 174 ReserveSeatsCompensation compensation = 175 new ReserveSeatsCompensation(this, nSeats); 176 uon.activityCommit(compensation); 177 } 178 rs.close(); 179 stmt.close(); 180 c.close(); 181 result = nSeats; 182 } catch (SQLException e) { 183 try { 184 uon.activityRollback(); 185 } catch (Exception e1) { 186 e1.printStackTrace(); 187 } 188 throw new DBException(); 189 } catch (NotEnoughSeatsException e) { 190 throw e; 191 } catch (Exception e) { 192 e.printStackTrace(); 193 } 194 return result; 195 } 196 197 202 public int unreserveSeats(int nSeats) throws DBException { 203 Connection c = null; 204 Statement stmt = null; 205 ResultSet rs = null; 206 int result = -1; 207 208 try { 209 uon.activityBegin(0); 210 c = ds.getConnection(); 211 stmt = c.createStatement(); 212 rs = stmt.executeQuery("SELECT seats FROM flights"); 213 rs.next(); 214 int freeSeats = rs.getInt(1); 215 stmt.executeUpdate( 216 "UPDATE flights SET seats = " + (freeSeats + nSeats)); 217 uon.activityCommit(null); 218 rs.close(); 219 stmt.close(); 220 c.close(); 221 result = nSeats; 222 } catch (SQLException e) { 223 try { 224 uon.activityRollback(); 225 } catch (Exception e1) { 226 e1.printStackTrace(); 227 } 228 throw new DBException(); 229 } catch (Exception e) { 230 e.printStackTrace(); 231 } 232 return result; 233 } 234 235 238 private UserOpenNested getUserOpenNested() { 239 UserOpenNested uon = null; 240 Context ctx = null; 241 242 try { 243 ctx = new InitialContext (); 244 uon = (UserOpenNested) ctx.lookup("UserOpenNested"); 245 } catch (Exception e) { 246 e.printStackTrace(); 247 } 248 return uon; 249 } 250 251 254 private DataSource getDataSource() { 255 Context ctx = null; 256 DataSource ds = null; 257 try { 258 ctx = new InitialContext (); 259 ds = (DataSource ) ctx.lookup("java:/AdaptAirlineDS"); 260 } catch (NamingException e) { 261 e.printStackTrace(); 262 } 263 return ds; 264 } 265 } 266 | Popular Tags |