From: Michael D. <mik...@us...> - 2004-06-18 13:59:16
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Connection In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31538/NHibernate/Connection Modified Files: ConnectionProvider.cs DriverConnectionProvider.cs IConnectionProvider.cs UserSuppliedConnectionProvider.cs Log Message: Synch with H2.0.3 - main thing is the addition of a basic connection pool to DriverManager so you can get back the same IDbConnection (same by reference - not just an already established connection by the data providers built in connection pool). This defaults to not being on. Index: IConnectionProvider.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Connection/IConnectionProvider.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** IConnectionProvider.cs 10 Mar 2004 15:36:45 -0000 1.4 --- IConnectionProvider.cs 18 Jun 2004 13:59:05 -0000 1.5 *************** *** 51,54 **** --- 51,59 ---- bool IsStatementCache { get; } + /// <summary> + /// Release all resources held by this ConnectionProvider. + /// </summary> + void Close(); + } } Index: DriverConnectionProvider.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Connection/DriverConnectionProvider.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DriverConnectionProvider.cs 10 Mar 2004 15:36:45 -0000 1.1 --- DriverConnectionProvider.cs 18 Jun 2004 13:59:05 -0000 1.2 *************** *** 1,3 **** --- 1,4 ---- using System; + using System.Collections; using System.Data; *************** *** 7,15 **** { /// <summary> ! /// Summary description for DriverConnectionProvider. /// </summary> public class DriverConnectionProvider : ConnectionProvider { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(DriverConnectionProvider)); public DriverConnectionProvider() --- 8,23 ---- { /// <summary> ! /// A ConnectionProvider that uses an IDriver to create connections. /// </summary> + /// <remarks> + /// This IConnectionProvider implements a rudimentary connection pool. + /// </remarks> public class DriverConnectionProvider : ConnectionProvider { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(DriverConnectionProvider)); + private static object poolLock = new object(); + private readonly ArrayList pool = new ArrayList(); + + private int checkedOut = 0; public DriverConnectionProvider() *************** *** 20,23 **** --- 28,50 ---- public override IDbConnection GetConnection() { + if(log.IsDebugEnabled) log.Debug("total checked-out connections: " + checkedOut); + + lock(poolLock) + { + if( pool.Count > 0 ) + { + int last = pool.Count - 1; + if(log.IsDebugEnabled) + { + log.Debug("using pooled connection, pool size: " + last); + checkedOut++; + } + + IDbConnection conn = (IDbConnection)pool[last]; + pool.RemoveAt(last); + return conn; + } + } + log.Debug("Obtaining IDbConnection from Driver"); try *************** *** 39,42 **** --- 66,108 ---- } + public override void Close() + { + log.Info("cleaning up connection pool"); + + for(int i = 0; i < pool.Count; i++) + { + try + { + ((IDbConnection)pool[i]).Close(); + } + catch(Exception e) + { + log.Warn("problem closing pooled connection", e); + } + } + + pool.Clear(); + } + + public override void CloseConnection(IDbConnection conn) + { + if(log.IsDebugEnabled) checkedOut--; + + lock(poolLock) + { + int currentSize = pool.Count; + if( currentSize < PoolSize ) + { + if(log.IsDebugEnabled) log.Debug("returning connection to pool, pool size: " + (currentSize + 1) ); + pool.Add(conn); + return; + } + } + + base.CloseConnection(conn); + } + + + } } Index: ConnectionProvider.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Connection/ConnectionProvider.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ConnectionProvider.cs 10 Mar 2004 15:36:45 -0000 1.2 --- ConnectionProvider.cs 18 Jun 2004 13:59:05 -0000 1.3 *************** *** 11,18 **** /// The base class for the ConnectionProvider. /// </summary> ! public abstract class ConnectionProvider : IConnectionProvider { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ConnectionProvider)); private string connString = null; protected IDriver driver = null; --- 11,19 ---- /// The base class for the ConnectionProvider. /// </summary> ! public abstract class ConnectionProvider : IConnectionProvider, IDisposable { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ConnectionProvider)); private string connString = null; + private int poolSize; protected IDriver driver = null; *************** *** 38,41 **** --- 39,50 ---- { log.Info("Configuring ConnectionProvider"); + + // default the poolSize to 0 if no setting was made because most of the .net DataProvider + // do their own connection pooling. This would be useful to change to some higher number + // if the .net DataProvider did not provide their own connection pooling. I don't know of + // any instances of this yet. + poolSize = PropertiesHelper.GetInt(Cfg.Environment.PoolSize, Cfg.Environment.Properties, 0); + log.Info("NHibernate connection pool size: " + poolSize); + connString = Cfg.Environment.Properties[ Cfg.Environment.ConnectionString ] as string; if (connString==null) throw new HibernateException("Could not find connection string setting"); *************** *** 75,78 **** --- 84,92 ---- } + protected virtual int PoolSize + { + get { return poolSize; } + } + public IDriver Driver { *************** *** 88,91 **** --- 102,116 ---- public abstract bool IsStatementCache {get;} + public abstract void Close(); + + #region IDisposable Members + + // equiv to java's object.finalize() + public void Dispose() + { + Close(); + } + + #endregion } } Index: UserSuppliedConnectionProvider.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Connection/UserSuppliedConnectionProvider.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** UserSuppliedConnectionProvider.cs 10 Mar 2004 15:36:45 -0000 1.4 --- UserSuppliedConnectionProvider.cs 18 Jun 2004 13:59:05 -0000 1.5 *************** *** 48,51 **** --- 48,58 ---- } + public override void Close() + { + // do nothing - don't need to throw an error because this is something + // that NHibernate will call. + } + + } } |