Azavea.Open.DAO.SQL.DbConnectionCache.Return C# (CSharp) Method

Return() public method

Inserts the connection into the cache IF we don't have one already (we only cache one connection per unique descriptor). This method is thread safe. Due to MS Access not allowing more than one connection to have the DB locked for modifying at one time, any connections to Access DBs are closed and not cached.
public Return ( AbstractSqlConnectionDescriptor connDesc, IDbConnection conn ) : void
connDesc AbstractSqlConnectionDescriptor
conn IDbConnection
return void
        public void Return(AbstractSqlConnectionDescriptor connDesc, IDbConnection conn)
        {
            bool closeConn = false;
            if (!connDesc.UsePooling())
            {
                closeConn = true;
            }
            else
            {
                lock (_cache)
                {
                    if (_cache.ContainsKey(connDesc))
                    {
                        // already have a connection for this connstr.  We're tolerant of this, just
                        // go ahead and close the second connection.
                        closeConn = true;
                    }
                    else
                    {
                        _cache[connDesc] = new TimestampedData<IDbConnection>(conn);
                    }
                }
            }
            if (closeConn)
            {
                // Not caching it, just close it.
                try
                {
                    if (_log.IsDebugEnabled)
                    {
                        _log.Debug("Closing DB connection: " + conn.ConnectionString);
                    }
                    conn.Close();
                }
                catch (Exception e)
                {
                    // This exception is not rethrown because we don't want to mask any
                    // previous exception that might be being thrown out of "invokeMe".
                    _log.Warn("Caught an exception while trying to close a DB connection we just used.", e);
                }
            }
        }
DbConnectionCache