Ejemplo n.º 1
0
        public IDbConnection OpenConnection()
        {
            CheckDispose();
            IDbConnection connection = null;
            string        key        = null;

            if (shared && HttpContext.Current != null)
            {
                key        = string.Format("Connection {0}|{1}", GetDialect(), DbRegistry.GetConnectionString(DatabaseId));
                connection = DisposableHttpContext.Current[key] as IDbConnection;
                if (connection != null)
                {
                    return(connection);
                }
            }
            connection = DbRegistry.CreateDbConnection(DatabaseId);
            if (proxyContext != null)
            {
                connection = new DbConnectionProxy(connection, proxyContext);
            }
            connection.Open();
            if (shared && HttpContext.Current != null)
            {
                DisposableHttpContext.Current[key] = connection;
            }
            return(connection);
        }
Ejemplo n.º 2
0
        private IDbConnection OpenConnection()
        {
            CheckDispose();
            IDbConnection connection = null;
            string        key        = null;

            if (shared && HttpContext.Current != null)
            {
                key        = string.Format("Connection {0}|{1}", GetDialect(), DbRegistry.GetConnectionString(DatabaseId));
                connection = DisposableHttpContext.Current[key] as IDbConnection;
                if (connection != null)
                {
                    var state    = ConnectionState.Closed;
                    var disposed = false;
                    try
                    {
                        state = connection.State;
                    }
                    catch (ObjectDisposedException)
                    {
                        disposed = true;
                    }
                    if (!disposed && (state == ConnectionState.Closed || state == ConnectionState.Broken))
                    {
                        if (string.IsNullOrEmpty(connection.ConnectionString))
                        {
                            connection.ConnectionString = DbRegistry.GetConnectionString(DatabaseId).ConnectionString;
                        }
                        connection.Open();
                        return(connection);
                    }
                }
            }
            connection = DbRegistry.CreateDbConnection(DatabaseId);
            if (proxyContext != null)
            {
                connection = new DbConnectionProxy(connection, proxyContext);
            }
            connection.Open();
            if (shared && HttpContext.Current != null)
            {
                DisposableHttpContext.Current[key] = connection;
            }
            return(connection);
        }