1 4 package org.ofbiz.minerva.pool.jdbc.xa; 5 6 7 import javax.naming.Context ; 8 import javax.naming.InitialContext ; 9 import javax.naming.Name ; 10 import javax.naming.NamingException ; 11 import javax.naming.RefAddr ; 12 import javax.naming.Reference ; 13 import javax.naming.Referenceable ; 14 import javax.naming.StringRefAddr ; 15 import javax.naming.spi.ObjectFactory ; 16 import javax.sql.DataSource ; 17 import javax.sql.XAConnection ; 18 import javax.sql.XADataSource ; 19 import javax.transaction.TransactionManager ; 20 import java.io.InvalidObjectException ; 21 import java.io.ObjectStreamException ; 22 import java.io.PrintWriter ; 23 import java.io.Serializable ; 24 import java.sql.Connection ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Hashtable ; 29 30 import org.apache.log4j.Logger; 31 import org.ofbiz.base.util.Log4jLoggerWriter; 32 import org.ofbiz.minerva.pool.ObjectPool; 33 34 52 public class XAPoolDataSource implements DataSource , Referenceable , ObjectFactory , Serializable 53 { 55 56 private transient static Logger log = Logger.getLogger(XAPoolDataSource.class); 57 private transient static HashMap sources = new HashMap (); 58 59 62 public static Collection getDataSources() { 63 return new HashSet (sources.values()); 64 } 65 66 69 public static XAPoolDataSource getDataSource(String poolName) { 70 return (XAPoolDataSource) sources.get(poolName); 71 } 72 73 private transient ObjectPool pool; 74 private transient XAConnectionFactory factory; 75 private transient PrintWriter logWriter; 76 private transient int timeout; 77 private transient boolean initialized = false; 78 private transient String jndiName; 80 private String name; 82 83 87 public XAPoolDataSource() { 88 log.debug("Creating XA Pool"); 89 pool = new ObjectPool(); 90 factory = new XAConnectionFactory(); 91 log.debug("Created factory"); 92 93 XAPoolDriver.instance(); 94 log.debug("got driver instance"); 95 } 96 97 103 public void setJNDIName(String name) throws NamingException { 104 if (log.isDebugEnabled()) 105 log.debug("Binding to JNDI name " + name); 106 107 InitialContext ctx = new InitialContext (); 108 if (jndiName != null && !jndiName.equals(name)) 109 ctx.unbind(jndiName); 110 if (name != null) 111 ctx.bind(name, this); 112 jndiName = name; 113 } 114 115 120 public String getJNDIName() { 121 return jndiName; 122 } 123 124 public void setDataSource(XADataSource ds) { 126 factory.setDataSource(ds); 127 } 128 129 public XADataSource getDataSource() { 130 return factory.getDataSource(); 131 } 132 133 public void setTransactionManager(TransactionManager tm) { 134 factory.setTransactionManager(tm); 135 } 136 137 public void setJDBCUser(String user) { 139 factory.setUser(user); 140 } 141 142 public String getJDBCUser() { 143 return factory.getUser(); 144 } 145 146 public void setJDBCPassword(String password) { 147 factory.setPassword(password); 148 } 149 150 public String getJDBCPassword() { 151 return factory.getPassword(); 152 } 153 154 public int getTransactionIsolation() { 155 return factory.getTransactionIsolation(); 156 } 157 158 public void setTransactionIsolation(int iso) { 159 factory.setTransactionIsolation(iso); 160 } 161 162 public void setTransactionIsolation(String iso) { 163 factory.setTransactionIsolation(iso); 164 } 165 166 public int getPSCacheSize() { 167 return factory.getPSCacheSize(); 168 } 169 170 public void setPSCacheSize(int size) { 171 factory.setPSCacheSize(size); 172 } 173 174 public boolean getReleaseOnCommit() { 175 return factory.getReleaseOnCommit(); 176 } 177 178 public void setReleaseOnCommit(boolean rel) { 179 factory.setReleaseOnCommit(rel); 180 } 181 182 187 public boolean getSaveStackTrace() { 188 return factory.getSaveStackTrace(); 189 } 190 191 public void setSaveStackTrace(boolean save) { 192 factory.setSaveStackTrace(save); 193 } 194 195 196 public void setPoolName(String name) { 198 this.name = name; 200 pool.setName(name); 201 sources.put(pool.getName(), this); 202 } 203 204 public String getPoolName() { 205 return name; 206 } 207 208 public void setMinSize(int size) { 209 pool.setMinSize(size); 210 } 211 212 public int getMinSize() { 213 return pool.getMinSize(); 214 } 215 216 public void setMaxSize(int size) { 217 pool.setMaxSize(size); 218 } 219 220 public int getMaxSize() { 221 return pool.getMaxSize(); 222 } 223 224 public void setBlocking(boolean blocking) { 225 pool.setBlocking(blocking); 226 } 227 228 public boolean isBlocking() { 229 return pool.isBlocking(); 230 } 231 232 public void setBlockingTimeout(int blockingTimeout) { 233 pool.setBlockingTimeout(blockingTimeout); 234 } 235 236 public int getBlockingTimeout() { 237 return pool.getBlockingTimeout(); 238 } 239 240 public void setIdleTimeoutEnabled(boolean allowShrinking) { 241 pool.setIdleTimeoutEnabled(allowShrinking); 242 } 243 244 public boolean isIdleTimeoutEnabled() { 245 return pool.isIdleTimeoutEnabled(); 246 } 247 248 public void setGCEnabled(boolean allowGC) { 249 pool.setGCEnabled(allowGC); 250 } 251 252 public boolean isGCEnabled() { 253 return pool.isGCEnabled(); 254 } 255 256 public void setMaxIdleTimeoutPercent(float percent) { 257 pool.setMaxIdleTimeoutPercent(percent); 258 } 259 260 public float getMaxIdleTimeoutPercent() { 261 return pool.getMaxIdleTimeoutPercent(); 262 } 263 264 public void setIdleTimeout(long millis) { 265 pool.setIdleTimeout(millis); 266 } 267 268 public long getIdleTimeout() { 269 return pool.getIdleTimeout(); 270 } 271 272 public void setGCMinIdleTime(long millis) { 273 pool.setGCMinIdleTime(millis); 274 } 275 276 public long getGCMinIdleTime() { 277 return pool.getGCMinIdleTime(); 278 } 279 280 public void setGCInterval(long millis) { 281 pool.setGCInterval(millis); 282 } 283 284 public long getGCInterval() { 285 return pool.getGCInterval(); 286 } 287 288 public void setInvalidateOnError(boolean invalidate) { 289 pool.setInvalidateOnError(invalidate); 290 } 291 292 public boolean isInvalidateOnError() { 293 return pool.isInvalidateOnError(); 294 } 295 296 public void setTimestampUsed(boolean timestamp) { 297 pool.setTimestampUsed(timestamp); 298 } 299 300 public boolean isTimestampUsed() { 301 return pool.isTimestampUsed(); 302 } 303 304 306 310 public void initialize() { 311 initialized = true; 312 pool.setObjectFactory(factory); 313 pool.initialize(); 314 } 315 316 320 public String getPoolStatus() { 321 return pool.toString(); 322 } 323 324 328 public void close() { 329 if (log.isDebugEnabled()) 330 log.debug("Closing DataSource"); 331 332 try { 333 setJNDIName(null); 334 } catch (NamingException e) { 335 log.warn("Can't unbind from JNDI", e); 336 } 337 sources.remove(pool.getName()); 338 pool.shutDown(); 339 pool = null; 340 factory = null; 341 } 342 343 348 public Connection getConnection() throws java.sql.SQLException { 349 if (!initialized) initialize(); 350 351 log.debug("Getting a Connection"); 352 String user = factory.getUser(); 353 String password = factory.getPassword(); 354 String [] params = {user, password}; 355 XAConnection xaConn = (XAConnection ) pool.getObject(params); 356 return xaConn.getConnection(); 357 } 358 359 365 public Connection getConnection(String user, String password) throws java.sql.SQLException { 366 if (!initialized) initialize(); 367 368 log.debug("Getting a connection for user " + user + " with password " + password); 369 String [] params = {user, password}; 370 XAConnection xaConn = (XAConnection ) pool.getObject(params); 371 return xaConn.getConnection(); 372 } 373 374 377 public PrintWriter getLogWriter() throws java.sql.SQLException { 378 return logWriter; 379 } 380 381 384 public void setLogWriter(PrintWriter writer) throws java.sql.SQLException { 385 if (writer == null) { 386 logWriter = null; 387 } else { 388 if (logWriter == null) { 389 logWriter = new Log4jLoggerWriter(log); 390 } 391 } 392 } 393 394 397 public int getLoginTimeout() throws java.sql.SQLException { 398 return timeout; 399 } 400 401 404 public void setLoginTimeout(int timeout) throws java.sql.SQLException { 405 this.timeout = timeout; 406 } 407 408 412 public Reference getReference() { 413 return new Reference (getClass().getName(), new StringRefAddr ("XAPool", pool.getName()), getClass().getName(), null); 414 } 415 416 420 public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) { 421 if (obj instanceof Reference ) { 422 Reference ref = (Reference ) obj; 423 if (ref.getClassName().equals(getClass().getName())) { 424 RefAddr addr = ref.get("XAPool"); 425 return sources.get(addr.getContent()); 426 } 427 } 428 return null; 429 } 430 431 433 private Object readResolve() throws ObjectStreamException { 434 try { 435 InitialContext ctx = new InitialContext (); 436 return ctx.lookup("java:/" + name); 437 } catch (NamingException e) { 438 throw new InvalidObjectException ("problem finding correct datasource instance" + e); 439 } 441 } 442 } 443 444 447 | Popular Tags |