1 22 package org.enhydra.jdbc.pool; 23 24 import java.sql.SQLException ; 25 import java.util.Hashtable ; 26 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 import javax.naming.Name ; 30 import javax.naming.NamingException ; 31 import javax.naming.Reference ; 32 import javax.naming.StringRefAddr ; 33 import javax.sql.ConnectionEvent ; 34 import javax.sql.XAConnection ; 35 import javax.sql.XADataSource ; 36 import javax.transaction.TransactionManager ; 37 import javax.transaction.Transaction ; 38 import javax.transaction.SystemException ; 39 import javax.transaction.xa.XAResource ; 40 import org.enhydra.jdbc.standard.StandardXADataSource; 41 import org.enhydra.jdbc.standard.StandardXAConnection; 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 import java.sql.Connection ; 45 import java.sql.Statement ; 46 import javax.sql.PooledConnection ; 47 import javax.transaction.Status ; 48 49 58 public class StandardXAPoolDataSource extends StandardPoolDataSource { 59 60 public XADataSource xads; public TransactionManager transactionManager; 62 public Log glog = LogFactory.getLog("org.enhydra.jdbc.xapool"); 64 65 68 public StandardXAPoolDataSource() { 69 super(); 70 } 71 72 75 public StandardXAPoolDataSource(int initSize) { 76 super(initSize); 77 } 78 79 82 public StandardXAPoolDataSource(StandardXADataSource ds) { 83 super(ds); 84 setDataSource(ds); 85 } 86 87 90 public StandardXAPoolDataSource(StandardXADataSource ds, int initSize) { 91 super(ds, initSize); 92 setDataSource(ds); 93 } 94 95 public void setTransactionManager(TransactionManager tm) { 96 log.debug("StandardXAPoolDataSource:setTransactionManager"); 97 transactionManager = tm; 98 } 99 100 104 public void connectionClosed(ConnectionEvent event) { 105 Object obj = event.getSource(); 106 log.debug("StandardXAPoolDataSource:connectionClosed"); 107 XAConnection xac = (XAConnection ) obj; 109 Transaction tx = null; 110 try { 111 if (transactionManager == null) { 112 TransactionManager tm = 113 ((StandardXADataSource) xads).getTransactionManager(); 114 if (tm == null) { 115 throw new NullPointerException ("TM is null"); 116 } else 117 tx = tm.getTransaction(); 121 } else { 122 tx = transactionManager.getTransaction(); 123 } 124 log.debug( 125 "StandardXAPoolDataSource:connectionClosed get a transaction"); 126 } catch (NullPointerException n) { 127 log.error( 129 "StandardXAPoolDataSource:connectionClosed should not be used outside an EJBServer"); 130 } catch (SystemException e) { 131 log.error( 132 "StandardXAPoolDataSource:connectionClosed getTransaction failed:" 133 + e); 134 } 135 136 if ((tx != null) 139 && (((StandardXAConnection) xac).connectionHandle.isReallyUsed)) { 140 try { 141 tx.delistResource(xac.getXAResource(), XAResource.TMSUCCESS); 142 log.debug( 144 "StandardXAPoolDataSource:connectionClosed the resourse is delisted"); 145 } catch (Exception e) { 146 log.error( 147 "StandardXAPoolDataSource:connectionClosed Exception in connectionClosed:" 148 + e); 149 } 150 } 151 log.debug( 152 "StandardXAPoolDataSource:connectionClosed checkIn an object to the pool"); 153 pool.checkIn(obj); } 155 156 public GenerationObject create(String _user, String _password) 157 throws SQLException { 158 GenerationObject genObject; 159 XAConnection xaCon = xads.getXAConnection(_user, _password); 160 xaCon.addConnectionEventListener(this); log.debug( 163 "StandardXAPoolDataSource:create create a object for the pool"); 164 genObject = 165 new GenerationObject(xaCon, pool.getGeneration(), _user, _password); 166 167 return genObject; 168 } 169 170 178 public Reference getReference() throws NamingException { 179 log.debug( 180 "StandardXAPoolDataSource:getReference return a reference of the object"); 181 Reference ref = super.getReference(); 182 ref.add( 183 new StringRefAddr ("transactionManagerName", "TransactionManager")); 184 return ref; 185 } 186 187 190 public Object getObjectInstance( 191 Object refObj, 192 Name name, 193 Context nameCtx, 194 Hashtable env) 195 throws Exception { 196 197 super.getObjectInstance(refObj, name, nameCtx, env); 198 Reference ref = (Reference ) refObj; 199 InitialContext ictx = new InitialContext (env); 200 this.setTransactionManager( 201 (TransactionManager ) ictx.lookup( 202 "javax.transaction.TransactionManager")); 203 this.setDataSource((XADataSource ) ictx.lookup(this.dataSourceName)); 204 log.debug("StandardPoolDataSource:getObjectInstance: instance created"); 205 return this; 206 } 207 208 211 public XADataSource getDataSource() { 212 return xads; 213 } 214 215 218 public void setDataSource(XADataSource dataSource) { 219 this.xads = dataSource; 220 if (transactionManager != null) 221 ((StandardXADataSource) dataSource).setTransactionManager( 222 transactionManager); 223 } 224 225 226 public String toString() { 227 StringBuffer sb = new StringBuffer (); 228 sb.append("StandardXAPoolDataSource:\n"); 229 if (this.transactionManager != null) 230 sb.append(" transaction manager=<"+this.transactionManager.toString()+">\n"); 231 if (this.xads != null) 232 sb.append(this.xads.toString()); 233 sb.append(super.toString()); 234 return sb.toString(); 235 } 236 237 245 public boolean testThisObject(Object o) { 246 Connection ret = null; 247 log.debug( 248 "StandardPoolDataSource:testThisObject verify the current object"); 249 Transaction suspended = null; 250 try { 251 Transaction tx = transactionManager == null 252 ? null 253 : transactionManager.getTransaction(); 254 boolean isActive = tx == null 255 ? false 256 : tx.getStatus() == Status.STATUS_ACTIVE; 257 if (isActive) { 258 suspended = transactionManager.suspend(); 259 } 260 261 262 PooledConnection con = (PooledConnection ) o; 263 ret = con.getConnection(); 264 265 266 267 Statement s = ret.createStatement(); 268 s.execute(jdbcTestStmt); 269 s.close(); 270 try { 271 ret.close(); 272 } catch (Exception e) { 273 log.error( 274 "StandardPoolDataSource:checkThisObject can't closed the connection: " 275 + e); 276 } 277 return true; 278 } catch (SQLException e) { 279 log.error( 280 "StandardXAPoolDataSource:checkThisObject Error java.sql.SQLException in StandardXAPoolDataSource:testThisObject"); 281 return false; 282 } catch (SystemException e) { 283 log.error( 284 "StandardXAPoolDataSource:checkThisObject Error java.sql.SystemException in StandardXAPoolDataSource:testThisObject"); 285 return false; 286 } finally { 287 if (suspended != null) { 288 try { 289 transactionManager.resume(suspended); 290 } catch (Exception ex) { 291 log.error( 292 "StandardXAPoolDataSource:checkThisObject Error Exception in StandardXAPoolDataSource:testThisObject"); 293 return false; 294 } 295 } 296 } 297 } 298 299 } | Popular Tags |