1 25 26 27 package org.objectweb.jonas.dbm; 28 29 import java.io.Serializable ; 30 import java.sql.Connection ; 31 import java.sql.SQLException ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import javax.naming.NamingException ; 36 import javax.naming.Reference ; 37 import javax.naming.Referenceable ; 38 import javax.naming.StringRefAddr ; 39 import javax.sql.ConnectionEvent ; 40 import javax.sql.ConnectionEventListener ; 41 import javax.sql.DataSource ; 42 import javax.sql.XAConnection ; 43 import javax.transaction.RollbackException ; 44 import javax.transaction.SystemException ; 45 import javax.transaction.Transaction ; 46 import javax.transaction.xa.XAResource ; 47 48 import org.objectweb.jonas.common.Log; 49 import org.objectweb.jonas.jdbc_xa.XADataSourceImpl; 50 import org.objectweb.jonas.jtm.TransactionService; 51 import org.objectweb.jonas.service.ServiceManager; 52 import org.objectweb.transaction.jta.ResourceManagerEventListener; 53 import org.objectweb.transaction.jta.TransactionManager; 54 import org.objectweb.util.monolog.api.BasicLevel; 55 import org.objectweb.util.monolog.api.Logger; 56 57 86 public class ConnectionManager 87 implements DataSource , 88 Referenceable , 89 Serializable , 90 ConnectionEventListener { 91 92 private Logger logger = null; 93 94 private static Map cmList = new HashMap (); 97 98 102 private TransactionManager tm = null; 103 104 108 private XADataSourceImpl xads = null; 109 110 114 private Pool pool = null; 115 116 119 public Pool getPool() { 120 return pool; 121 } 122 123 127 130 private String dSName = null; 131 132 135 private String datasourceName; 136 137 140 private String url = null; 141 144 private String className = null; 145 148 private String userName = null; 149 152 private String password = null; 153 154 157 private int isolationLevel = -1; 158 private String isolationStr = null; 159 160 163 private String currentMapperName = null; 164 165 169 private boolean isClient = false; 170 171 174 private String xaName = null; 175 private String dsDescription = null; 176 177 private static ResourceManagerEventListener rmListener = null; 178 179 183 186 public ConnectionManager() throws Exception { 187 188 ServiceManager sm = ServiceManager.getInstance(); 191 TransactionService ts = (TransactionService) sm.getTransactionService(); 192 tm = ts.getTransactionManager(); 193 194 195 setRMEListener(tm); 197 198 xads = new XADataSourceImpl(); 202 } 203 204 209 public ConnectionManager(boolean isClient) throws Exception { 210 this.isClient = isClient; 211 xads = new XADataSourceImpl(); 212 } 213 214 219 public boolean isClientCase() { 220 return isClient; 221 } 222 223 227 public void setRMEListener(ResourceManagerEventListener rmel) { 228 rmListener = rmel; 229 } 230 231 234 public String getDSName() { 235 return dSName; 236 } 237 238 241 public void setDSName(String s) { 242 dSName = s; 243 logger = Log.getLogger(Log.JONAS_DBM_PREFIX + "." + dSName); 244 try { 245 pool = new Pool(this, xads, logger); 246 } catch (Exception e) { 247 logger.log(BasicLevel.ERROR, "Cannot create Pool: " + e); 248 } 249 xads.setDataSourceName(s); cmList.put(s, this); 252 } 253 254 public String getUrl() { 255 return url; 256 } 257 public void setUrl(String s) { 258 url = s; 259 xads.setUrl(s); 261 } 262 263 public String getClassName() { 264 return className; 265 } 266 public void setClassName(String s) throws ClassNotFoundException { 267 className = s; 268 xads.setClassName(s); 270 } 271 272 public String getUserName() { 273 return userName; 274 } 275 public void setUserName(String s) { 276 userName = s; 277 xads.setUserName(s); 279 } 280 281 public String getPassword() { 282 return password; 283 } 284 public void setPassword(String s) { 285 password = s; 286 xads.setPassword(s); 288 } 289 290 public void setTransactionIsolation(String level) { 291 if (logger.isLoggable(BasicLevel.DEBUG)) { 292 logger.log(BasicLevel.DEBUG, level); 293 } 294 if (level.equals("serializable")) { 295 isolationLevel = Connection.TRANSACTION_SERIALIZABLE; 296 } else if (level.equals("none")) { 297 isolationLevel = Connection.TRANSACTION_NONE; 298 } else if (level.equals("read_committed")) { 299 isolationLevel = Connection.TRANSACTION_READ_COMMITTED; 300 } else if (level.equals("read_uncommitted")) { 301 isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED; 302 } else if (level.equals("repeatable_read")) { 303 isolationLevel = Connection.TRANSACTION_REPEATABLE_READ; 304 } else { 305 isolationStr = "default"; 306 return; 307 } 308 isolationStr = level; 310 xads.setTransactionIsolation(isolationLevel); 311 } 312 313 public String getTransactionIsolation() { 314 if (logger.isLoggable(BasicLevel.DEBUG)) { 315 logger.log(BasicLevel.DEBUG, isolationStr); 316 } 317 return isolationStr; 318 } 319 320 public void setMapperName(String mappername) { 321 currentMapperName = mappername; 322 } 323 324 public String getMapperName() { 325 return currentMapperName; 326 } 327 328 342 public void poolConfigure(String connchecklevel, 343 String connmaxage, 344 String maxopentime, 345 String connteststmt, 346 String minconpool, 347 String maxconpool, 348 String maxwaittime, 349 String maxwaiters, 350 String samplingperiod) { 351 352 pool.setCheckLevel((new Integer (connchecklevel)).intValue()); 354 pool.setMaxAge((new Integer (connmaxage)).intValue()); 356 pool.setMaxOpenTime((new Integer (maxopentime)).intValue()); 357 pool.setTestStatement(connteststmt); 358 pool.setPoolMin((new Integer (minconpool)).intValue()); 359 pool.setPoolMax((new Integer (maxconpool)).intValue()); 360 pool.setMaxWaitTime((new Integer (maxwaittime)).intValue()); 361 pool.setMaxWaiters((new Integer (maxwaiters)).intValue()); 362 pool.setSamplingPeriod((new Integer (samplingperiod)).intValue()); 363 if (logger.isLoggable(BasicLevel.DEBUG)) { 364 logger.log(BasicLevel.DEBUG, "ConnectionManager configured with:"); 365 logger.log(BasicLevel.DEBUG, " jdbcConnCheckLevel = " + connchecklevel); 366 logger.log(BasicLevel.DEBUG, " jdbcConnMaxAge = " + connmaxage); 367 logger.log(BasicLevel.DEBUG, " jdbcMaxOpenTime = " + maxopentime); 368 logger.log(BasicLevel.DEBUG, " jdbcTestStmt = " + connteststmt); 369 logger.log(BasicLevel.DEBUG, " minConPool = " + pool.getPoolMin()); 370 logger.log(BasicLevel.DEBUG, " maxConPool = " + pool.getPoolMax()); 371 logger.log(BasicLevel.DEBUG, " maxWaitTime = " + pool.getMaxWaitTime()); 372 logger.log(BasicLevel.DEBUG, " maxWaiters = " + pool.getMaxWaiters()); 373 logger.log(BasicLevel.DEBUG, " samplingPeriod = " + pool.getSamplingPeriod()); 374 } 375 } 376 377 381 388 public Connection getConnection() throws SQLException { 389 return getConnection(userName, password); 390 } 391 392 402 public Connection getConnection(String username, String password) throws SQLException { 403 XAConnection xac = null; 404 Transaction tx = null; 405 try { 406 tx = tm.getTransaction(); 407 } catch (NullPointerException n) { 408 logger.log(BasicLevel.ERROR, "ConnectionManager: should not be used outside a JOnAS Server", n); 410 } catch (SystemException e) { 411 logger.log(BasicLevel.ERROR, "ConnectionManager: getTransaction failed", e); 412 } 413 if (logger.isLoggable(BasicLevel.DEBUG)) { 414 logger.log(BasicLevel.DEBUG, "Tx = " + tx); 415 } 416 417 PoolItem pi = pool.openConnection(username, tx); 419 xac = pi.getXACon(); 421 422 Connection ret = xac.getConnection(); 423 if (tx != null) { 425 if (pi.getOpenCount() == 1) { try { 427 if (logger.isLoggable(BasicLevel.DEBUG)) { 428 logger.log(BasicLevel.DEBUG, "enlist XAResource on " + tx); 429 } 430 tx.enlistResource(xac.getXAResource()); 431 ret.setAutoCommit(false); 433 } catch (RollbackException e) { 434 logger.log(BasicLevel.WARN, "XAResource enlisted, but tx is marked rollback", e); 437 } catch (IllegalStateException e) { 438 ret.setAutoCommit(true); 440 } catch (Exception e) { 441 logger.log(BasicLevel.ERROR, "Cannot enlist XAResource", e); 442 logger.log(BasicLevel.ERROR, "Connection will not be enlisted in a transaction"); 443 throw new SQLException ("Cannot enlist XAResource"); 445 } 446 } 447 } else { 448 ret.setAutoCommit(true); } 450 if (!pi.isRME()) { 453 if (logger.isLoggable(BasicLevel.DEBUG)) { 454 logger.log(BasicLevel.DEBUG, "register this connection to the TM."); 455 } 456 pi.setRME(true); 457 rmListener.connectionOpened(pi); 458 } 459 return ret; 461 } 462 463 479 public java.io.PrintWriter getLogWriter() throws SQLException { 480 return xads.getLogWriter(); 481 } 482 483 484 500 public void setLogWriter(java.io.PrintWriter out) throws SQLException { 501 xads.setLogWriter(out); 502 } 503 504 505 517 public void setLoginTimeout(int seconds) throws SQLException { 518 xads.setLoginTimeout(seconds); 519 } 520 521 532 public int getLoginTimeout() throws SQLException { 533 return xads.getLoginTimeout(); 534 } 535 536 540 545 public void connectionClosed(ConnectionEvent event) { 546 if (logger.isLoggable(BasicLevel.DEBUG)) { 547 logger.log(BasicLevel.DEBUG, " "); 548 } 549 550 XAConnection xac = (XAConnection ) event.getSource(); 551 PoolItem pi = pool.closeConnection(xac, XAResource.TMSUCCESS); 552 553 if (pi != null && pi.isRME()) { 556 if (logger.isLoggable(BasicLevel.DEBUG)) { 557 logger.log(BasicLevel.DEBUG, "unregister this connection to the TM."); 558 } 559 pi.setRME(false); 560 rmListener.connectionClosed(pi); 561 } 562 } 563 564 569 public void connectionErrorOccurred(ConnectionEvent event) { 570 if (logger.isLoggable(BasicLevel.DEBUG)) { 571 logger.log(BasicLevel.DEBUG, ""); 572 } 573 574 XAConnection xac = (XAConnection ) event.getSource(); 575 PoolItem pi = pool.closeConnection(xac, XAResource.TMFAIL); 576 577 if (pi != null && pi.isRME()) { 580 if (logger.isLoggable(BasicLevel.DEBUG)) { 581 logger.log(BasicLevel.DEBUG, "unregister this connection to the TM."); 582 } 583 pi.setRME(false); 584 rmListener.connectionErrorOccured(pi); 585 } 586 } 587 588 592 600 public Reference getReference() throws NamingException { 601 602 Reference ref = new Reference (this.getClass().getName(), 603 "org.objectweb.jonas.dbm.DataSourceFactory", 604 null); 605 ref.add(new StringRefAddr ("datasource.name", getDSName())); 607 ref.add(new StringRefAddr ("datasource.url", getUrl())); 608 ref.add(new StringRefAddr ("datasource.classname", getClassName())); 609 ref.add(new StringRefAddr ("datasource.username", getUserName())); 610 ref.add(new StringRefAddr ("datasource.password", getPassword())); 611 ref.add(new StringRefAddr ("datasource.isolationlevel", getTransactionIsolation())); 612 ref.add(new StringRefAddr ("datasource.mapper", getMapperName())); 613 Integer checklevel = new Integer (pool.getCheckLevel()); 614 ref.add(new StringRefAddr ("connchecklevel", checklevel.toString())); 615 Integer maxage = new Integer (pool.getMaxAge()); 616 ref.add(new StringRefAddr ("connmaxage", maxage.toString())); 617 Integer maxopentime = new Integer (pool.getMaxOpenTime()); 618 ref.add(new StringRefAddr ("maxopentime", maxopentime.toString())); 619 ref.add(new StringRefAddr ("connteststmt", pool.getTestStatement())); 620 Integer minpool = new Integer (pool.getPoolMin()); 621 ref.add(new StringRefAddr ("minconpool", minpool.toString())); 622 Integer maxpool = new Integer (pool.getPoolMax()); 623 ref.add(new StringRefAddr ("maxconpool", maxpool.toString())); 624 Integer maxwaittime = new Integer (pool.getMaxWaitTime()); 625 ref.add(new StringRefAddr ("maxwaittime", maxwaittime.toString())); 626 Integer maxwaiters = new Integer (pool.getMaxWaiters()); 627 ref.add(new StringRefAddr ("maxwaiters", maxwaiters.toString())); 628 Integer samplingperiod = new Integer (pool.getSamplingPeriod()); 629 ref.add(new StringRefAddr ("samplingperiod", samplingperiod.toString())); 630 return ref; 631 } 632 633 636 public static ConnectionManager getConnectionManager(String dsname) { 637 638 ConnectionManager cm = (ConnectionManager) cmList.get(dsname); 639 return cm; 640 } 641 642 public void closeAllConnection() { 643 if (logger.isLoggable(BasicLevel.DEBUG)) { 644 logger.log(BasicLevel.DEBUG, ""); 645 } 646 pool.closeAllConnections(); 647 } 648 649 public static ResourceManagerEventListener getResourceManagerEventListener() { 650 return rmListener; 651 } 652 653 656 public String getDatasourceName() { 657 return datasourceName; 658 } 659 660 663 public void setDatasourceName(String s) { 664 datasourceName = s; 665 } 666 667 670 public String getDataSourceDescription() { 671 return dsDescription; 672 } 673 674 677 public void setDataSourceDescription(String dsDesc) { 678 dsDescription = dsDesc; 679 } 680 } | Popular Tags |