1 22 package org.jboss.tm; 23 24 import javax.transaction.Status ; 25 import javax.transaction.SystemException ; 26 import javax.transaction.Transaction ; 27 import javax.transaction.TransactionManager ; 28 import javax.transaction.UserTransaction ; 29 import javax.transaction.xa.XAException ; 30 import javax.transaction.xa.XAResource ; 31 32 import org.jboss.util.NestedRuntimeException; 33 34 43 public class TxUtils 44 { 45 46 private static final String [] TxStatusStrings = 47 { 48 "STATUS_ACTIVE", 49 "STATUS_MARKED_ROLLBACK", 50 "STATUS_PREPARED", 51 "STATUS_COMMITTED", 52 "STATUS_ROLLEDBACK", 53 "STATUS_UNKNOWN", 54 "STATUS_NO_TRANSACTION", 55 "STATUS_PREPARING", 56 "STATUS_COMMITTING", 57 "STATUS_ROLLING_BACK" 58 }; 59 60 63 private TxUtils() 64 { 65 66 } 67 68 public static boolean isActive(Transaction tx) 69 { 70 try 71 { 72 return tx != null && (tx.getStatus() == Status.STATUS_ACTIVE); 73 } 74 catch (SystemException error) 75 { 76 throw new NestedRuntimeException(error); 77 } 78 } 79 80 public static boolean isActive(TransactionManager tm) 81 { 82 try 83 { 84 return isActive(tm.getTransaction()); 85 } 86 catch (SystemException error) 87 { 88 throw new NestedRuntimeException(error); 89 } 90 } 91 92 public static boolean isActive(UserTransaction ut) 93 { 94 try 95 { 96 return ut.getStatus() == Status.STATUS_ACTIVE; 97 } 98 catch (SystemException error) 99 { 100 throw new NestedRuntimeException(error); 101 } 102 } 103 104 public static boolean isUncommitted(Transaction tx) 105 { 106 try 107 { 108 if (tx == null) 109 return false; 110 int status = tx.getStatus(); 111 return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK; 112 } 113 catch (SystemException error) 114 { 115 throw new NestedRuntimeException(error); 116 } 117 } 118 119 public static boolean isUncommitted(TransactionManager tm) 120 { 121 try 122 { 123 return isUncommitted(tm.getTransaction()); 124 } 125 catch (SystemException error) 126 { 127 throw new NestedRuntimeException(error); 128 } 129 } 130 131 public static boolean isCompleted(Transaction tx) 132 { 133 try 134 { 135 if (tx == null) 136 return true; 137 int status = tx.getStatus(); 138 return status == Status.STATUS_COMMITTED 139 || status == Status.STATUS_ROLLEDBACK 140 || status == Status.STATUS_NO_TRANSACTION; 141 } 142 catch (SystemException error) 143 { 144 throw new NestedRuntimeException(error); 145 } 146 } 147 148 public static boolean isCompleted(TransactionManager tm) 149 { 150 try 151 { 152 return isCompleted(tm.getTransaction()); 153 } 154 catch (SystemException error) 155 { 156 throw new NestedRuntimeException(error); 157 } 158 } 159 160 public static boolean isCompleted(UserTransaction ut) 161 { 162 try 163 { 164 int status = ut.getStatus(); 165 return status == Status.STATUS_COMMITTED 166 || status == Status.STATUS_ROLLEDBACK 167 || status == Status.STATUS_NO_TRANSACTION; 168 } 169 catch (SystemException error) 170 { 171 throw new NestedRuntimeException(error); 172 } 173 } 174 175 183 public static String getStatusAsString(int status) 184 { 185 if (status >= Status.STATUS_ACTIVE && status <= Status.STATUS_ROLLING_BACK) 186 { 187 return TxStatusStrings[status]; 188 } 189 else 190 { 191 return "STATUS_INVALID(" + status + ")"; 192 } 193 } 194 195 203 public static String getXAResourceFlagsAsString(int flags) 204 { 205 if (flags == XAResource.TMNOFLAGS) 206 { 207 return "|TMNOFLAGS"; 208 } 209 else 210 { 211 StringBuffer sbuf = new StringBuffer (64); 212 213 if ((flags & XAResource.TMONEPHASE) != 0) 214 { 215 sbuf.append("|TMONEPHASE"); 216 } 217 if ((flags & XAResource.TMJOIN) != 0) 218 { 219 sbuf.append("|TMJOIN"); 220 } 221 if ((flags & XAResource.TMRESUME) != 0) 222 { 223 sbuf.append("|TMRESUME"); 224 } 225 if ((flags & XAResource.TMSUCCESS) != 0) 226 { 227 sbuf.append("|TMSUCCESS"); 228 } 229 if ((flags & XAResource.TMFAIL) != 0) 230 { 231 sbuf.append("|TMFAIL"); 232 } 233 if ((flags & XAResource.TMSUSPEND) != 0) 234 { 235 sbuf.append("|TMSUSPEND"); 236 } 237 if ((flags & XAResource.TMSTARTRSCAN) != 0) 238 { 239 sbuf.append("|TMSTARTRSCAN"); 240 } 241 if ((flags & XAResource.TMENDRSCAN) != 0) 242 { 243 sbuf.append("|TMENDRSCAN"); 244 } 245 return sbuf.toString(); 246 } 247 } 248 249 258 public static String getXAErrorCodeAsString(int errorCode) 259 { 260 switch (errorCode) 261 { 262 case XAException.XA_HEURCOM: 263 return "XA_HEURCOM"; 264 case XAException.XA_HEURHAZ: 265 return "XA_HEURHAZ"; 266 case XAException.XA_HEURMIX: 267 return "XA_HEURMIX"; 268 case XAException.XA_HEURRB: 269 return "XA_HEURRB"; 270 case XAException.XA_NOMIGRATE: 271 return "XA_NOMIGRATE"; 272 case XAException.XA_RBCOMMFAIL: 273 return "XA_RBCOMMFAIL"; 274 case XAException.XA_RBDEADLOCK: 275 return "XA_RBDEADLOCK"; 276 case XAException.XA_RBINTEGRITY: 277 return "XA_RBINTEGRITY"; 278 case XAException.XA_RBOTHER: 279 return "XA_RBOTHER"; 280 case XAException.XA_RBPROTO: 281 return "XA_RBPROTO"; 282 case XAException.XA_RBROLLBACK: 283 return "XA_RBROLLBACK"; 284 case XAException.XA_RBTIMEOUT: 285 return "XA_RBTIMEOUT"; 286 case XAException.XA_RBTRANSIENT: 287 return "XA_RBTRANSIENT"; 288 case XAException.XA_RDONLY: 289 return "XA_RDONLY"; 290 case XAException.XA_RETRY: 291 return "XA_RETRY"; 292 293 case XAException.XAER_ASYNC: 294 return "XAER_ASYNC"; 295 case XAException.XAER_DUPID: 296 return "XAER_DUPID"; 297 case XAException.XAER_INVAL: 298 return "XAER_INVAL"; 299 case XAException.XAER_NOTA: 300 return "XAER_NOTA"; 301 case XAException.XAER_OUTSIDE: 302 return "XAER_OUTSIDE"; 303 case XAException.XAER_PROTO: 304 return "XAER_PROTO"; 305 case XAException.XAER_RMERR: 306 return "XAER_RMERR"; 307 case XAException.XAER_RMFAIL: 308 return "XAER_RMFAIL"; 309 default: 310 return "XA_UNKNOWN(" + errorCode + ")"; 311 } 312 } 313 314 } 315 | Popular Tags |