?? c3p0pooledconnectionpoolmanager.java
字號:
/* * Distributed as part of c3p0 v.0.9.1-pre6 * * Copyright (C) 2005 Machinery For Change, Inc. * * Author: Steve Waldman <swaldman@mchange.com> * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 2.1, as * published by the Free Software Foundation. * * This software 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. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this software; see the file LICENSE. If not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */package com.mchange.v2.c3p0.impl;import java.beans.*;import java.util.*;import java.lang.reflect.*;import java.sql.*;import javax.sql.*;import com.mchange.v2.c3p0.*;import com.mchange.v2.c3p0.impl.*;import com.mchange.v2.c3p0.stmt.*;import com.mchange.v2.c3p0.cfg.*;import com.mchange.v2.async.*;import com.mchange.v2.coalesce.*;import com.mchange.v1.db.sql.*;import com.mchange.v2.log.*;import com.mchange.v1.lang.BooleanUtils;import com.mchange.v2.sql.SqlUtils;import com.mchange.v2.resourcepool.ResourcePoolFactory;import com.mchange.v2.resourcepool.BasicResourcePoolFactory;public final class C3P0PooledConnectionPoolManager{ private final static MLogger logger = MLog.getLogger( C3P0PooledConnectionPoolManager.class ); private final static boolean POOL_EVENT_SUPPORT = false; private final static CoalesceChecker COALESCE_CHECKER = IdentityTokenizedCoalesceChecker.INSTANCE; // unsync'ed coalescer -- we synchronize the static factory method that uses it final static Coalescer COALESCER = CoalescerFactory.createCoalescer( COALESCE_CHECKER, true, false ); final static int DFLT_NUM_TASK_THREADS_PER_DATA_SOURCE = 3; //MT: protected by this' lock ThreadPoolAsynchronousRunner taskRunner; Timer timer; ResourcePoolFactory rpfact; Map authsToPools; /* MT: independently thread-safe, never reassigned post-ctor or factory */ final ConnectionPoolDataSource cpds; final Map propNamesToReadMethods; final Map flatPropertyOverrides; final Map userOverrides; final DbAuth defaultAuth; /* MT: end independently thread-safe, never reassigned post-ctor or factory */ /* MT: unchanging after constructor completes */ int num_task_threads = DFLT_NUM_TASK_THREADS_PER_DATA_SOURCE; /* MT: end unchanging after constructor completes */ private synchronized void poolsInit() { this.timer = new Timer( true ); this.taskRunner = new ThreadPoolAsynchronousRunner( num_task_threads, true, timer ); //this.taskRunner = new RoundRobinAsynchronousRunner( num_task_threads, true ); //this.rpfact = ResourcePoolFactory.createInstance( taskRunner, timer ); if (POOL_EVENT_SUPPORT) this.rpfact = ResourcePoolFactory.createInstance( taskRunner, null, timer ); else this.rpfact = BasicResourcePoolFactory.createNoEventSupportInstance( taskRunner, timer ); this.authsToPools = new HashMap(); } private void poolsDestroy() { poolsDestroy( true ); } private synchronized void poolsDestroy( boolean close_outstanding_connections ) { //System.err.println("poolsDestroy() -- " + this); for (Iterator ii = authsToPools.values().iterator(); ii.hasNext(); ) { try { ((C3P0PooledConnectionPool) ii.next()).close( close_outstanding_connections ); } catch ( Exception e ) { //e.printStackTrace(); logger.log(MLevel.WARNING, "An Exception occurred while trying to clean up a pool!", e); } } this.taskRunner.close( true ); this.timer.cancel(); this.taskRunner = null; this.timer = null; this.rpfact = null; this.authsToPools = null; } public C3P0PooledConnectionPoolManager(ConnectionPoolDataSource cpds, Map flatPropertyOverrides, // Map of properties, usually null Map forceUserOverrides, // userNames to Map of properties, usually null int num_task_threads ) throws SQLException { try { this.cpds = cpds; this.flatPropertyOverrides = flatPropertyOverrides; this.num_task_threads = num_task_threads; DbAuth auth = null; if ( flatPropertyOverrides != null ) { String overrideUser = (String) flatPropertyOverrides.get("overrideDefaultUser"); String overridePassword = (String) flatPropertyOverrides.get("overrideDefaultPassword"); if (overrideUser == null) { overrideUser = (String) flatPropertyOverrides.get("user"); overridePassword = (String) flatPropertyOverrides.get("password"); } if (overrideUser != null) auth = new DbAuth( overrideUser, overridePassword ); } if (auth == null) auth = C3P0ImplUtils.findAuth( cpds ); this.defaultAuth = auth; Map tmp = new HashMap(); BeanInfo bi = Introspector.getBeanInfo( cpds.getClass() ); PropertyDescriptor[] pds = bi.getPropertyDescriptors(); PropertyDescriptor pd = null; for (int i = 0, len = pds.length; i < len; ++i) { pd = pds[i]; String name = pd.getName(); Method m = pd.getReadMethod(); if (m != null) tmp.put( name, m ); } this.propNamesToReadMethods = tmp; if (forceUserOverrides == null) { Method uom = (Method) propNamesToReadMethods.get( "userOverridesAsString" ); if (uom != null) { String uoas = (String) uom.invoke( cpds, null ); //System.err.println("uoas: " + uoas); Map uo = C3P0ImplUtils.parseUserOverridesAsString( uoas ); this.userOverrides = uo; } else this.userOverrides = Collections.EMPTY_MAP; } else this.userOverrides = forceUserOverrides; poolsInit(); } catch (Exception e) { if (Debug.DEBUG) logger.log(MLevel.FINE, null, e); //e.printStackTrace(); throw SqlUtils.toSQLException(e); } } public synchronized C3P0PooledConnectionPool getPool(String username, String password) throws SQLException { return getPool( new DbAuth( username, password ) ); } public synchronized C3P0PooledConnectionPool getPool(DbAuth auth) throws SQLException { C3P0PooledConnectionPool out = (C3P0PooledConnectionPool) authsToPools.get(auth); if (out == null) { out = createPooledConnectionPool(auth); authsToPools.put( auth, out ); } return out; } public synchronized Set getManagedAuths() { return Collections.unmodifiableSet( authsToPools.keySet() ); } public synchronized int getNumManagedAuths() { return authsToPools.size(); } public C3P0PooledConnectionPool getPool() throws SQLException { return getPool( defaultAuth ); } public synchronized int getNumIdleConnectionsAllAuths() throws SQLException { int out = 0; for (Iterator ii = authsToPools.values().iterator(); ii.hasNext(); ) out += ((C3P0PooledConnectionPool) ii.next()).getNumIdleConnections(); return out; } public synchronized int getNumBusyConnectionsAllAuths() throws SQLException { int out = 0; for (Iterator ii = authsToPools.values().iterator(); ii.hasNext(); ) out += ((C3P0PooledConnectionPool) ii.next()).getNumBusyConnections(); return out; } public synchronized int getNumConnectionsAllAuths() throws SQLException { int out = 0; for (Iterator ii = authsToPools.values().iterator(); ii.hasNext(); ) out += ((C3P0PooledConnectionPool) ii.next()).getNumConnections(); return out; } public synchronized int getNumUnclosedOrphanedConnectionsAllAuths() throws SQLException { int out = 0; for (Iterator ii = authsToPools.values().iterator(); ii.hasNext(); ) out += ((C3P0PooledConnectionPool) ii.next()).getNumUnclosedOrphanedConnections(); return out; } public synchronized void softResetAllAuths() throws SQLException { for (Iterator ii = authsToPools.values().iterator(); ii.hasNext(); ) ((C3P0PooledConnectionPool) ii.next()).reset(); } public void close() { this.close( true ); } public synchronized void close( boolean close_outstanding_connections ) { // System.err.println("close()ing " + this); if (authsToPools != null) poolsDestroy( close_outstanding_connections ); } protected synchronized void finalize() { // System.err.println("finalizing... " + this); this.close(); } private Object getObject(String propName, String userName) { Object out = null; if (userName != null) { //userOverrides are usually config file defined, unless rarely used forceUserOverrides is supplied! Map specificUserOverrides = (Map) userOverrides.get( userName ); if (specificUserOverrides != null) out = specificUserOverrides.get( propName ); } if (out == null && flatPropertyOverrides != null) //flatPropertyOverrides is a rarely used mechanism for forcing a config out = flatPropertyOverrides.get( propName ); //if the ConnectionPoolDataSource has config parameter defined as a property use it //(unless there was a user-specific or force override found above) if (out == null)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -