1 4 package org.ofbiz.minerva.pool.jdbc.xa.wrapper; 5 6 import javax.sql.ConnectionEvent ; 7 import javax.sql.ConnectionEventListener ; 8 import javax.sql.XAConnection ; 9 import javax.transaction.xa.XAResource ; 10 import java.sql.Connection ; 11 import java.sql.SQLException ; 12 import java.util.ArrayList ; 13 import java.util.Vector ; 14 15 import org.ofbiz.minerva.pool.PoolEvent; 16 import org.ofbiz.minerva.pool.PoolEventListener; 17 import org.ofbiz.minerva.pool.PooledObject; 18 import org.ofbiz.minerva.pool.cache.ObjectCache; 19 import org.ofbiz.minerva.pool.jdbc.ConnectionInPool; 20 21 53 public class XAConnectionImpl implements XAConnection , PooledObject { 54 55 private final static String CLOSED = "Connection has been closed!"; 56 private Connection con; 57 private XAResourceImpl resource; 58 private Vector listeners, poolListeners; 59 private ArrayList clientConnections; 60 private TransactionListener transListener; 61 private int preparedStatementCacheSize = 0; 62 private int clientConnectionCount = 0; 63 64 private String user; 65 66 private String password; 67 private boolean saveStackTrace; 68 69 75 public XAConnectionImpl(Connection con, XAResourceImpl resource, boolean saveStackTrace) { 76 this.con = con; 77 this.resource = resource; 78 listeners = new Vector (); 79 poolListeners = new Vector (); 80 clientConnections = new ArrayList (); 81 this.saveStackTrace = saveStackTrace; 82 } 83 84 87 public void setTransactionListener(TransactionListener tl) { 88 transListener = tl; 89 } 90 91 94 public void clearTransactionListener() { 95 transListener = null; 96 } 97 98 103 public void setPSCacheSize(int maxSize) { 104 preparedStatementCacheSize = maxSize; 105 } 106 107 111 public int getPSCacheSize() { 112 return preparedStatementCacheSize; 113 } 114 115 116 public void setTransactionIsolation(int iso) throws SQLException { 117 con.setTransactionIsolation(iso); 118 } 119 120 123 public void close() { 124 try { 125 con.close(); 126 } catch (SQLException e) { 127 } 128 ObjectCache cache = (ObjectCache) ConnectionInPool.psCaches.remove(con); 129 if (cache != null) 130 cache.close(); 131 con = null; 132 resource = null; 133 listeners.clear(); 134 listeners = null; 135 } 136 137 142 public void clientConnectionClosed(XAClientConnection clientCon) { 143 synchronized(clientConnections) { 144 clientConnections.remove(clientCon); 145 } 146 if (clientConnections.size() > 0) 147 return; 149 boolean trans = resource.isTransaction(); Vector local = (Vector ) listeners.clone(); 151 for (int i = local.size() - 1; i >= 0; i--) 152 ((ConnectionEventListener ) local.elementAt(i)).connectionClosed(new ConnectionEvent (this)); 153 } 156 157 163 public void transactionFinished() { 164 if (transListener != null) 165 transListener.transactionFinished(this); 166 } 167 168 174 public void transactionFailed() { 175 if (transListener != null) 176 transListener.transactionFailed(this); 177 } 178 179 184 public void setConnectionError(SQLException e) { 185 Vector local = (Vector ) listeners.clone(); 186 for (int i = local.size() - 1; i >= 0; i--) { 187 try { 188 ((ConnectionEventListener ) local.elementAt(i)).connectionErrorOccurred(new ConnectionEvent (this, e)); 189 } catch (RuntimeException ex) { 190 ex.printStackTrace(); 193 } 194 } 195 } 196 197 204 public void rollback() throws SQLException { 205 if (con.getAutoCommit() == false) 206 con.rollback(); 207 } 208 209 211 public XAResource getXAResource() { 212 return resource; 213 } 214 215 public void addConnectionEventListener(ConnectionEventListener listener) { 216 listeners.addElement(listener); 217 } 218 219 public void removeConnectionEventListener(ConnectionEventListener listener) { 220 if (!listeners.remove(listener)) 221 throw new IllegalArgumentException (); 222 } 223 224 public Connection getConnection() { 225 XAClientConnection xaCon; 226 synchronized (clientConnections) { 227 xaCon = new XAClientConnection(this, con, saveStackTrace); 228 xaCon.setPSCacheSize(preparedStatementCacheSize); 229 clientConnections.add(xaCon); 230 } 231 return xaCon; 232 } 233 234 236 public void addPoolEventListener(PoolEventListener listener) { 237 poolListeners.addElement(listener); 238 } 239 240 public void removePoolEventListener(PoolEventListener listener) { 241 poolListeners.removeElement(listener); 242 } 243 244 247 void firePoolEvent(PoolEvent evt) { 248 Vector local = (Vector ) poolListeners.clone(); 249 for (int i = local.size() - 1; i >= 0; i--) 250 if (evt.getType() == PoolEvent.OBJECT_CLOSED) 251 ((PoolEventListener) local.elementAt(i)).objectClosed(evt); 252 else if (evt.getType() == PoolEvent.OBJECT_ERROR) 253 ((PoolEventListener) local.elementAt(i)).objectError(evt); 254 else 255 ((PoolEventListener) local.elementAt(i)).objectUsed(evt); 256 } 257 258 261 public java.lang.String getPassword() { 262 return password; 263 } 264 265 268 public void setPassword(java.lang.String password) { 269 this.password = password; 270 } 271 272 275 public java.lang.String getUser() { 276 return user; 277 } 278 279 282 public void setUser(java.lang.String user) { 283 this.user = user; 284 } 285 286 public void forceClientConnectionsClose() { 287 for (int i = 0; i < clientConnections.size(); i++) { 288 XAClientConnection client = (XAClientConnection) clientConnections.get(i); 289 try { 290 client.forcedClose(); 291 } catch (SQLException ignored) { 292 } 293 } 294 clientConnections.clear(); 295 } 296 } 297 | Popular Tags |