예제 #1
0
        /// <summary>
        /// Gets the ConnectionManager object for the
        /// specified database.
        /// </summary>
        /// <param name="database">
        /// The database name or connection string.
        /// </param>
        /// <param name="isDatabaseName">
        /// True to indicate that the connection string
        /// should be retrieved from the config file. If
        /// False, the database parameter is directly
        /// used as a connection string.
        /// </param>
        /// <param name="label">Label for this connection.</param>
        /// <returns>ConnectionManager object for the name.</returns>
        public static ConnectionManager <C> GetManager(string database, bool isDatabaseName, string label)
        {
            if (isDatabaseName)
            {
#if NETSTANDARD2_0 || NET5_0
                throw new NotSupportedException("isDatabaseName==true");
#else
                var connection = ConfigurationManager.ConnectionStrings[database];
                if (connection == null)
                {
                    throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
                }

                var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
                if (string.IsNullOrEmpty(conn))
                {
                    throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
                }
                database = conn;
#endif
            }

            ConnectionManager <C> mgr = null;
            var ctxName = GetContextName(database, label);
            lock (_lock)
            {
                var cached = ApplicationContext.LocalContext.GetValueOrNull(ctxName);
                if (cached != null)
                {
                    mgr = (ConnectionManager <C>)cached;
                    mgr.AddRef();
                }
            }

            if (mgr == null)
            {
                mgr = new ConnectionManager <C>(database, label);
                lock (_lock)
                {
                    ApplicationContext.LocalContext[ctxName] = mgr;
                    mgr.AddRef();
                }
            }
            return(mgr);
        }
예제 #2
0
        /// <summary>
        /// Gets the ConnectionManager object for the
        /// specified database.
        /// </summary>
        /// <param name="database">
        /// The database name or connection string.
        /// </param>
        /// <param name="isDatabaseName">
        /// True to indicate that the connection string
        /// should be retrieved from the config file. If
        /// False, the database parameter is directly
        /// used as a connection string.
        /// </param>
        /// <param name="label">Label for this connection.</param>
        /// <returns>ConnectionManager object for the name.</returns>
        public static ConnectionManager GetManager(string database, bool isDatabaseName, string label)
        {
            if (isDatabaseName)
            {
                var connection = ConfigurationManager.ConnectionStrings[database];
                if (connection == null)
                {
                    throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
                }

                var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
                if (string.IsNullOrEmpty(conn))
                {
                    throw new ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
                }
                database = conn;
            }

            ConnectionManager mgr = null;
            var ctxName           = GetContextName(database, label);

            lock (_lock)
            {
                var cached = ApplicationContext.LocalContext.GetValueOrNull(ctxName);
                if (cached != null)
                {
                    mgr = (ConnectionManager)cached;
                    mgr.AddRef();
                }
            }

            if (mgr == null)
            {
                mgr = new ConnectionManager(database, label);
                lock (_lock)
                {
                    ApplicationContext.LocalContext[ctxName] = mgr;
                    mgr.AddRef();
                }
            }
            return(mgr);
        }