1 22 package org.jboss.test.testbean.bean; 23 24 import java.rmi.*; 25 import javax.ejb.*; 26 import javax.naming.InitialContext ; 27 28 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil; 29 30 import javax.transaction.UserTransaction ; 31 import javax.transaction.Status ; 32 import java.sql.Connection ; 33 import java.sql.Statement ; 34 import java.sql.ResultSet ; 35 import javax.sql.DataSource ; 36 import java.sql.SQLException ; 37 38 public class BMTStatefulBean implements SessionBean 39 { 40 static org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(BMTStatefulBean.class); 41 42 private SessionContext sessionContext; 43 44 public void ejbCreate() throws RemoteException, CreateException 45 { 46 log.debug("BMTStatefulBean.ejbCreate() called"); 47 } 48 49 50 public void ejbCreate(String caca) throws RemoteException, CreateException 51 { 52 }; 53 54 public void ejbCreate(String caca, String cacaprout) throws RemoteException, CreateException 55 { 56 }; 57 58 public void ejbActivate() throws RemoteException 59 { 60 log.debug("BMTStatefulBean.ejbActivate() called"); 61 } 62 63 public void ejbPassivate() throws RemoteException 64 { 65 log.debug("BMTStatefulBean.ejbPassivate() called"); 66 } 67 68 public void ejbRemove() throws RemoteException 69 { 70 log.debug("BMTStatefulBean.ejbRemove() called"); 71 } 72 73 public void setSessionContext(SessionContext context) throws RemoteException 74 { 75 sessionContext = context; 76 } 77 78 public String txExists() throws RemoteException 79 { 80 String result = ""; 81 try 82 { 83 UserTransaction ut1 = sessionContext.getUserTransaction(); 84 result += "Got UserTransaction via sessionContext.getUserTransaction(): " + statusName(ut1.getStatus()) + "\n"; 85 86 UserTransaction ut2 = (UserTransaction ) new InitialContext ().lookup("java:comp/UserTransaction"); 87 result += "Got UserTransaction via lookup(java:comp/UserTransaction): " + statusName(ut2.getStatus()) + "\n"; 88 89 return result; 90 } 91 catch(Exception e) 92 { 93 log.debug("failed", e); 94 throw new RemoteException(e.getMessage()); 95 } 96 } 97 98 99 public String txCommit() throws RemoteException 100 { 101 try 102 { 103 UserTransaction tx = sessionContext.getUserTransaction(); 104 105 String result = "Got transaction : " + statusName(tx.getStatus()) + "\n"; 106 tx.begin(); 107 result += "tx.begin(): " + statusName(tx.getStatus()) + "\n"; 108 tx.commit(); 109 result += "tx.commit(): " + statusName(tx.getStatus()) + "\n"; 110 111 return result; 112 113 } 114 catch(Exception e) 115 { 116 log.debug("failed", e); 117 throw new RemoteException(e.getMessage()); 118 } 119 120 } 121 122 public String txRollback() throws RemoteException 123 { 124 try 125 { 126 UserTransaction tx = sessionContext.getUserTransaction(); 127 128 String result = "Got transaction : " + statusName(tx.getStatus()) + "\n"; 129 tx.begin(); 130 result += "tx.begin(): " + statusName(tx.getStatus()) + "\n"; 131 tx.rollback(); 132 result += "tx.rollback(): " + statusName(tx.getStatus()) + "\n"; 133 134 return result; 135 136 } 137 catch(Exception e) 138 { 139 log.debug("failed", e); 140 throw new RemoteException(e.getMessage()); 141 } 142 } 143 144 145 public String txBegin() throws RemoteException 146 { 147 try 148 { 149 UserTransaction tx = sessionContext.getUserTransaction(); 150 151 tx.begin(); 152 return "status: " + statusName(tx.getStatus()); 153 } 154 catch(Exception e) 155 { 156 log.debug("failed", e); 157 throw new RemoteException(e.getMessage()); 158 } 159 160 } 161 162 public String txEnd() throws RemoteException 163 { 164 try 165 { 166 UserTransaction tx = sessionContext.getUserTransaction(); 167 168 tx.commit(); 169 return "status: " + statusName(tx.getStatus()); 170 } 171 catch(Exception e) 172 { 173 log.debug("failed", e); 174 throw new RemoteException(e.getMessage()); 175 } 176 177 } 178 179 public void createTable() throws RemoteException 180 { 181 Connection connection = null; 182 Statement stm = null; 183 try 184 { 185 connection = ((DataSource ) new InitialContext ().lookup("java:comp/env/jdbc/myDatabase")).getConnection(); 186 stm = connection.createStatement(); 187 try 188 { 189 stm.executeUpdate("CREATE TABLE bmttest (field VARCHAR(256))"); 190 } 191 catch(SQLException e) 192 { 193 } 195 finally 196 { 197 JDBCUtil.safeClose(stm); 198 } 199 200 stm = connection.createStatement(); 201 stm.executeUpdate("INSERT INTO bmttest VALUES ('initial value')"); 202 } 203 catch(Exception e) 204 { 205 log.debug("failed", e); 206 throw new RemoteException(e.getMessage()); 207 } 208 finally 209 { 210 JDBCUtil.safeClose(stm); 211 JDBCUtil.safeClose(connection); 212 } 213 } 214 215 public void dropTable() throws RemoteException 216 { 217 Connection connection = null; 218 Statement stm = null; 219 try 220 { 221 connection = ((DataSource ) new InitialContext ().lookup("java:comp/env/jdbc/myDatabase")).getConnection(); 222 stm = connection.createStatement(); 223 stm.executeUpdate("DROP TABLE bmttest"); 224 } 225 catch(Exception e) 226 { 227 log.debug("failed", e); 228 throw new RemoteException(e.getMessage()); 229 } 230 finally 231 { 232 JDBCUtil.safeClose(stm); 233 JDBCUtil.safeClose(connection); 234 } 235 } 236 237 238 public String dbCommit() throws RemoteException 239 { 240 try 241 { 242 UserTransaction tx = sessionContext.getUserTransaction(); 243 244 tx.begin(); 245 246 Connection connection = null; 247 Statement stm = null; 248 try 249 { 250 connection = ((DataSource ) new InitialContext ().lookup("java:comp/env/jdbc/myDatabase")).getConnection(); 251 stm = connection.createStatement(); 252 stm.executeUpdate("UPDATE bmttest SET field = 'updated via dbCommit'"); 253 } 254 finally 255 { 256 JDBCUtil.safeClose(stm); 257 JDBCUtil.safeClose(connection); 258 } 259 260 tx.commit(); 261 return statusName(tx.getStatus()); 262 263 } 264 catch(Exception e) 265 { 266 log.debug("failed", e); 267 throw new RemoteException(e.getMessage()); 268 } 269 } 270 271 272 public String dbRollback() throws RemoteException 273 { 274 try 275 { 276 UserTransaction tx = sessionContext.getUserTransaction(); 277 278 tx.begin(); 279 280 Connection connection = null; 281 Statement stm = null; 282 try 283 { 284 connection = ((DataSource ) new InitialContext ().lookup("java:comp/env/jdbc/myDatabase")).getConnection(); 285 stm = connection.createStatement(); 286 stm.executeUpdate("UPDATE bmttest SET field = 'updated via dbRollback'"); 287 } 288 finally 289 { 290 JDBCUtil.safeClose(stm); 291 JDBCUtil.safeClose(connection); 292 } 293 294 tx.rollback(); 295 return statusName(tx.getStatus()); 296 297 } 298 catch(Exception e) 299 { 300 log.debug("failed", e); 301 throw new RemoteException(e.getMessage()); 302 } 303 } 304 305 306 public String getDbField() throws RemoteException 307 { 308 Connection connection = null; 309 Statement stm = null; 310 ResultSet rs = null; 311 try 312 { 313 connection = ((DataSource ) new InitialContext ().lookup("java:comp/env/jdbc/myDatabase")).getConnection(); 314 stm = connection.createStatement(); 315 rs = stm.executeQuery("SELECT field FROM bmttest "); 316 String result = "not found"; 317 if(rs.next()) 318 { 319 result = rs.getString(1); 320 } 321 return result; 322 } 323 catch(Exception e) 324 { 325 log.debug("failed", e); 326 throw new RemoteException(e.getMessage()); 327 } 328 finally 329 { 330 JDBCUtil.safeClose(rs); 331 JDBCUtil.safeClose(stm); 332 JDBCUtil.safeClose(connection); 333 } 334 } 335 336 337 private String statusName(int s) 338 { 339 switch(s) 340 { 341 case Status.STATUS_ACTIVE: 342 return "STATUS_ACTIVE"; 343 case Status.STATUS_COMMITTED: 344 return "STATUS_COMMITED"; 345 case Status.STATUS_COMMITTING: 346 return "STATUS_COMMITTING"; 347 case Status.STATUS_MARKED_ROLLBACK: 348 return "STATUS_MARKED_ROLLBACK"; 349 case Status.STATUS_NO_TRANSACTION: 350 return "STATUS_NO_TRANSACTION"; 351 case Status.STATUS_PREPARED: 352 return "STATUS_PREPARED"; 353 case Status.STATUS_PREPARING: 354 return "STATUS_PREPARING"; 355 case Status.STATUS_ROLLEDBACK: 356 return "STATUS_ROLLEDBACK"; 357 case Status.STATUS_ROLLING_BACK: 358 return "STATUS_ROLLING_BACK"; 359 case Status.STATUS_UNKNOWN: 360 return "STATUS_UNKNOWN"; 361 } 362 return "REALLY_UNKNOWN"; 363 } 364 } 365 | Popular Tags |