dbpool.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 826 行 · 第 1/2 页

JAVA
826
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.sql;import com.caucho.config.ConfigException;import com.caucho.config.types.InitParam;import com.caucho.config.types.Period;import com.caucho.jca.ConnectionPool;import com.caucho.jca.ResourceManagerImpl;import com.caucho.loader.EnvironmentLocal;import com.caucho.naming.Jndi;import com.caucho.transaction.TransactionManagerImpl;import com.caucho.util.L10N;import com.caucho.webbeans.component.HandleAware;import com.caucho.webbeans.manager.WebBeansContainer;import javax.annotation.PostConstruct;import javax.resource.spi.ManagedConnectionFactory;import javax.sql.ConnectionPoolDataSource;import javax.sql.DataSource;import javax.sql.XADataSource;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.Driver;import java.sql.SQLException;import java.util.logging.Logger;/** * Manages a pool of database connections.  In addition, DBPool configures * the database connection from a configuration file. * * <p>Like JDBC 2.0 pooling, DBPool returns a wrapped Connection. * Applications can use that connection just like an unpooled connection. * It is more important than ever to <code>close()</code> the connection, * because the close returns the connection to the connection pool. * * <h4>Example using DataSource JNDI style (recommended)</h4> * * <pre><code> * Context env = (Context) new InitialContext().lookup("java:comp/env"); * DataSource pool = (DataSource) env.lookup("jdbc/test"); * Connection conn = pool.getConnection(); * try { *   ... // normal connection stuff * } finally { *   conn.close(); * } * </code></pre> * * <h4>Configuration</h4> * * <pre><code> * &lt;database name='jdbc/test'> *   &lt;init> *     &lt;driver>postgresql.Driver&lt;/driver> *     &lt;url>jdbc:postgresql://localhost/test&lt;/url> *     &lt;user>ferg&lt;/user> *     &lt;password>foobar&lt;/password> *   &lt;/init> * &lt;/database> * </code></pre> * * <h4>Pool limits and timeouts</h4> * * The pool will only allow getMaxConnections() connections alive at a time. * If <code>getMaxConnection</code> connections are already active, * <code>getPooledConnection</code> will block waiting for an available * connection.  The wait is timed.  If connection-wait-time passes * and there is still no connection, <code>getPooledConnection</code> * create a new connection anyway. * * <p>Connections will only stay in the pool for about 5 seconds.  After * that they will be removed and closed.  This reduces the load on the DB * and also protects against the database dropping old connections. */public class DBPool  implements DataSource, java.io.Serializable, HandleAware{  protected static final Logger log    = Logger.getLogger(DBPool.class.getName());  private static final L10N L = new L10N(DBPool.class);  private static int _g_id;    private EnvironmentLocal<DBPoolImpl> _localPoolImpl;  private EnvironmentLocal<DataSource> _localDataSourceImpl;  private String _var;  private String _name;  private String _jndiName;  private String _tmpName;  private ResourceManagerImpl _resourceManager;  private ConnectionPool _connectionPool;  private DBPoolImpl _poolImpl;  private DataSource _dataSource;  private DataSourceImpl _resinDataSource;  // serialization handle  private Object _serializationHandle;  /**   * Null constructor for the Driver interface; called by the JNDI   * configuration.  Applications should not call this directly.   */  public DBPool()  {    _poolImpl = new DBPoolImpl();    _resourceManager = ResourceManagerImpl.createLocalManager();    _connectionPool = _resourceManager.createConnectionPool();    DatabaseConfig.configDefault(this);  }  /**   * Returns the Pool's name   */  public String getJndiName()  {    return _jndiName;  }  /**   * Sets the Pool's JNDI name.  Also puts the pool in the classloader's   * list of pools.   */  public void setJndiName(String name)  {    _jndiName = name;    if (_var == null && _name == null)      getPool().setName(name);  }  /**   * Sets the Pool's WebBeans name.  Also puts the pool in the classloader's   * list of pools.   */  public void setName(String name)  {    _name = name;    getPool().setName(name);  }  /**   * Sets the Pool's var.   */  public void setVar(String var)  {    _var = var;    if (_name == null)      getPool().setName(var);  }  /**   * Returns the Pool's name   */  public String getName()  {    return getPool().getName();  }  /**   * Sets a custom driver (or data source)   */  public DriverConfig createDriver()    throws ConfigException  {    return getPool().createDriver();  }  /**   * Sets a custom driver (or data source)   */  public DriverConfig createBackupDriver()    throws ConfigException  {    return getPool().createBackupDriver();  }  /**   * Sets a driver parameter (compat).   */  public void setInitParam(InitParam init)  {    getPool().setInitParam(init);  }  /**   * Sets the jdbc-driver config.   */  public void setJDBCDriver(Driver jdbcDriver)    throws SQLException  {    getPool().setJDBCDriver(jdbcDriver);  }  /**   * Configure the initial connection   */  public ConnectionConfig createConnection()    throws ConfigException  {    return getPool().createConnection();  }  /**   * Sets the jdbc-driver config.   */  public void setPoolDataSource(ConnectionPoolDataSource poolDataSource)    throws SQLException  {    getPool().setPoolDataSource(poolDataSource);  }  /**   * Sets the jdbc-driver config.   */  public void setXADataSource(XADataSource xaDataSource)    throws SQLException  {    getPool().setXADataSource(xaDataSource);  }  /**   * Sets the jdbc-driver URL   */  public void setURL(String url)    throws ConfigException  {    getPool().setURL(url);  }  /**   * Return the url   */  public String getURL()  {    return getPool().getURL();  }  /**   * Returns the connection's user (compat).   */  public String getUser()  {    return getPool().getUser();  }  /**   * Sets the connection's user.   */  public void setUser(String user)  {    getPool().setUser(user);  }  /**   * Returns the connection's password   */  public String getPassword()  {    return getPool().getPassword();  }  /**   * Sets the connection's password   */  public void setPassword(String password)  {    getPool().setPassword(password);  }  /**   * Get the maximum number of pooled connections.   */  public int getMaxConnections()  {    return _connectionPool.getMaxConnections();  }  /**   * Sets the maximum number of pooled connections.   */  public void setMaxConnections(int maxConnections)    throws ConfigException  {    _connectionPool.setMaxConnections(maxConnections);  }  /**   * Get the total number of connections   */  public int getTotalConnections()  {    return _connectionPool.getConnectionCount();  }  /**   * Sets the time to wait for a connection when all are used.   */  public void setConnectionWaitTime(Period waitTime)  {    _connectionPool.setConnectionWaitTime(waitTime);  }  /**   * Gets the time to wait for a connection when all are used.   */  public long getConnectionWaitTime()  {    return _connectionPool.getConnectionWaitTime();  }  /**   * The number of connections to overflow if the connection pool fills   * and there's a timeout.   */  public void setMaxOverflowConnections(int maxOverflowConnections)  {    _connectionPool.setMaxOverflowConnections(maxOverflowConnections);  }  /**   * The number of connections to create at any one time.   */  public void setMaxCreateConnections(int maxCreateConnections)    throws ConfigException  {    _connectionPool.setMaxCreateConnections(maxCreateConnections);  }  /**   * Set true if the stack trace should be saved on allocation.   */  public void setSaveAllocationStackTrace(boolean save)  {    _connectionPool.setSaveAllocationStackTrace(save);  }  /**   * Set true if dangling connections should be closed.   */  public void setCloseDanglingConnections(boolean isClose)  {    _connectionPool.setCloseDanglingConnections(isClose);  }  /**   * The number of connections to overflow if the connection pool fills   * and there's a timeout.   */  public int getMaxOverflowConnections()  {    return _connectionPool.getMaxOverflowConnections();  }  /**   * Get the total number of connections in use by the program.   */  public int getActiveConnections()  {    return _connectionPool.getConnectionActiveCount();  }  /**   * Get the maximum number of connections in the idle pool.   */  public int getMaxIdleCount()  {    return _connectionPool.getMaxIdleCount();  }  /**   * Set the maximum number of connections in the idle pool.   * being closed.   */  public void setMaxIdleCount(int count)  {    _connectionPool.setMaxIdleCount(count);  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?