1 23 24 package org.apache.slide.transaction; 25 26 import java.util.Hashtable ; 27 28 import javax.transaction.HeuristicMixedException ; 29 import javax.transaction.HeuristicRollbackException ; 30 import javax.transaction.InvalidTransactionException ; 31 import javax.transaction.NotSupportedException ; 32 import javax.transaction.RollbackException ; 33 import javax.transaction.Status ; 34 import javax.transaction.SystemException ; 35 import javax.transaction.Transaction ; 36 import javax.transaction.TransactionManager ; 37 38 import org.apache.slide.util.Messages; 39 import org.apache.slide.util.logger.Logger; 40 import org.apache.slide.util.logger.SimpleLogger; 41 42 53 public final class SlideTransactionManager implements TransactionManager { 54 55 56 58 59 protected static final String LOG_CHANNEL = 60 SlideTransactionManager.class.getName(); 61 62 63 public static final int DEFAULT_TRANSACTION_TIMEOUT = 30; 64 65 66 68 69 71 72 75 private Hashtable bindings = new Hashtable (); 76 77 78 81 private Hashtable timeouts = new Hashtable (); 82 83 84 87 private Logger logger = new SimpleLogger(); 88 89 90 92 93 95 96 99 public Logger getLogger() { 100 return logger; 101 } 102 103 104 107 public void setLogger(Logger logger) { 108 this.logger = logger; 109 } 110 111 112 114 115 124 public void begin() 125 throws NotSupportedException , SystemException { 126 127 Transaction currentTransaction = getTransaction(); 128 if (currentTransaction != null) 129 throw new NotSupportedException (); 130 131 currentTransaction = new SlideTransaction(this); 132 bindings.put(Thread.currentThread(), currentTransaction); 133 134 if (logger.isEnabled(LOG_CHANNEL, Logger.DEBUG)) { 135 String logMessage = Messages.format 136 (SlideTransactionManager.class.getName() + ".begin", 137 currentTransaction.toString()); 138 logger.log(logMessage, LOG_CHANNEL, Logger.DEBUG); 139 } 140 141 } 142 143 144 165 public void commit() 166 throws RollbackException , HeuristicMixedException , 167 HeuristicRollbackException , SecurityException , IllegalStateException , 168 SystemException { 169 170 Thread currentThread = Thread.currentThread(); 171 Transaction currentTransaction = 172 (Transaction ) bindings.get(currentThread); 173 if (currentTransaction == null) 174 throw new IllegalStateException (); 175 176 timeouts.remove(currentThread); 177 178 if (logger.isEnabled(LOG_CHANNEL, Logger.DEBUG)) { 179 String logMessage = Messages.format 180 (SlideTransactionManager.class.getName() + ".commit", 181 currentTransaction.toString()); 182 logger.log(logMessage, LOG_CHANNEL, Logger.DEBUG); 183 } 184 185 try { 186 currentTransaction.commit(); 187 } finally { 188 bindings.remove(currentThread); 189 } 190 191 } 192 193 194 206 public void rollback() 207 throws SecurityException , IllegalStateException , SystemException { 208 209 Thread currentThread = Thread.currentThread(); 210 Transaction currentTransaction = 211 (Transaction ) bindings.remove(currentThread); 212 if (currentTransaction == null) 213 throw new IllegalStateException (); 214 215 timeouts.remove(currentThread); 216 217 String logMessage = Messages.format 218 (SlideTransactionManager.class.getName() + ".rollback", 219 currentTransaction.toString()); 220 logger.log(logMessage, LOG_CHANNEL, Logger.DEBUG); 221 222 currentTransaction.rollback(); 223 224 } 225 226 227 237 public void setRollbackOnly() 238 throws IllegalStateException , SystemException { 239 240 Transaction currentTransaction = getTransaction(); 241 if (currentTransaction == null) 242 throw new IllegalStateException (); 243 244 String logMessage = Messages.format 245 (SlideTransactionManager.class.getName() + ".rollbackOnly", 246 currentTransaction.toString()); 247 logger.log(logMessage, LOG_CHANNEL, Logger.INFO); 248 249 currentTransaction.setRollbackOnly(); 250 251 } 252 253 254 262 public int getStatus() 263 throws SystemException { 264 265 Transaction currentTransaction = getTransaction(); 266 if (currentTransaction == null) 267 return Status.STATUS_NO_TRANSACTION; 268 269 return currentTransaction.getStatus(); 270 271 } 272 273 274 283 public Transaction getTransaction() 284 throws SystemException { 285 return (Transaction ) bindings.get(Thread.currentThread()); 286 } 287 288 289 304 public void resume(Transaction tobj) 305 throws InvalidTransactionException , IllegalStateException , 306 SystemException { 307 308 if (getTransaction() != null) 309 throw new IllegalStateException (); 310 311 if (tobj == null) 312 throw new InvalidTransactionException (); 313 314 bindings.put(Thread.currentThread(), tobj); 315 316 } 317 318 319 330 public Transaction suspend() 331 throws SystemException { 332 333 Transaction currentTransaction = getTransaction(); 334 335 if (currentTransaction != null) { 336 Thread currentThread = Thread.currentThread(); 337 bindings.remove(currentThread); 338 timeouts.remove(currentThread); 339 } 340 341 return currentTransaction; 342 343 } 344 345 346 358 public void setTransactionTimeout(int seconds) 359 throws SystemException { 360 361 timeouts.put(Thread.currentThread(), new Integer (seconds)); 362 363 } 364 365 366 } 367 | Popular Tags |