1 31 32 33 package org.objectweb.jonas.dbm; 34 35 import javax.sql.XAConnection ; 36 import javax.transaction.Synchronization ; 37 import javax.transaction.Transaction ; 38 39 import org.objectweb.jonas.common.Log; 40 import org.objectweb.transaction.jta.ResourceManagerEvent; 41 import org.objectweb.util.monolog.api.BasicLevel; 42 import org.objectweb.util.monolog.api.Logger; 43 44 53 public class PoolItem implements ResourceManagerEvent, Synchronization { 54 55 58 private static int count = 0; 59 60 private XAConnection xaConn; private String user; private int open; private Transaction tx; private Transaction enlistedInTx; private boolean rme = false; private long deathTime; private long closeTime; private int number; 69 private Pool pool; 70 private Logger logger = null; 71 72 75 public PoolItem(Pool pool, XAConnection xac, String user, Logger logger) { 76 this.pool = pool; 77 this.xaConn = xac; 78 this.user = user; 79 open = 0; 80 deathTime = System.currentTimeMillis() + pool.getMaxAgeMilli(); 81 number = count++; 82 this.logger = logger; 83 } 84 85 88 public String toString() { 89 Integer i = new Integer (number); 90 return i.toString(); 91 } 92 93 96 public void enlistConnection(Transaction transaction) throws javax.transaction.SystemException { 97 try { 98 if (rme) { 99 xaConn.getConnection().setAutoCommit(false); 100 enlistedInTx = transaction; 102 if (logger.isLoggable(BasicLevel.DEBUG)) { 104 logger.log(BasicLevel.DEBUG, "enlist XAResource on " + transaction); 105 } 106 transaction.enlistResource(xaConn.getXAResource()); 107 } 108 } catch (javax.transaction.RollbackException e) { 109 javax.transaction.SystemException se = new javax.transaction.SystemException ("Unexpected RollbackException exception"); 110 se.initCause(e); 111 throw se; 112 } catch (java.sql.SQLException e) { 113 javax.transaction.SystemException se = new javax.transaction.SystemException ("Unexpected SQL exception"); 114 se.initCause(e); 115 throw se; 116 } 117 } 118 119 122 public void beforeCompletion() { 123 } 125 126 129 public void afterCompletion(int status) { 130 if (tx == null ) { 131 logger.log(BasicLevel.ERROR, "NO TX!"); 132 } 133 pool.freeConnections(tx != null ? tx : enlistedInTx); 134 } 135 136 139 public boolean isAged() { 140 return (deathTime < System.currentTimeMillis()); 141 } 142 143 146 public boolean isOpen() { 147 return (open > 0); 148 } 149 150 153 public int getOpenCount() { 154 return open; 155 } 156 157 162 public boolean inactive() { 163 return (open > 0 && tx == null && enlistedInTx == null && closeTime < System.currentTimeMillis()); 164 } 165 166 169 public boolean isClosed() { 170 return (open <= 0); 171 } 172 173 176 public void open() { 177 open++; 178 closeTime = System.currentTimeMillis() + pool.getMaxOpenTimeMilli(); 179 } 180 181 185 public boolean close() { 186 open--; 187 if (open < 0) { 188 logger.log(BasicLevel.WARN, "connection was already closed"); 189 open = 0; 190 return false; 191 } 192 if (tx == null && open > 0) { 193 logger.log(BasicLevel.ERROR, "connection-open counter overflow"); 194 open = 0; 195 } 196 return true; 197 } 198 199 202 public XAConnection getXACon() { 203 return xaConn; 204 } 205 206 210 public void setTx(Transaction tx) { 211 this.tx = tx; 212 } 213 214 217 public Transaction getTx() { 218 return tx; 219 } 220 221 224 public boolean isRME() { 225 return rme; 226 } 227 228 231 public void setRME(boolean rme) { 232 this.rme = rme; 233 } 234 235 238 public void remove() { 239 try { 241 xaConn.close(); 242 } catch (java.sql.SQLException ign) { 243 logger.log(BasicLevel.ERROR, "Could not close Connection: ", ign); 244 } 245 246 xaConn = null; 248 tx = null; 249 enlistedInTx = null; 250 } 251 } 252 | Popular Tags |