1#if !NETSTANDARD2_0 && !NET5_0
12using System.Data.Common;
31 public class ConnectionManager : IDisposable
33 private static object _lock =
new object();
34 private IDbConnection _connection;
35 private string _connectionString;
36 private string _label;
79 return GetManager(database, isDatabaseName,
"default");
101 var connection = ConfigurationManager.ConnectionStrings[database];
102 if (connection ==
null)
105 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
106 if (
string.IsNullOrEmpty(conn))
112 var ctxName = GetContextName(database, label);
115 var cached = ApplicationContext.LocalContext.GetValueOrNull(ctxName);
128 ApplicationContext.LocalContext[ctxName] = mgr;
138 _connectionString = connectionString;
140#if NETSTANDARD2_0 || NET5_0
141 _connection =
new System.Data.SqlClient.SqlConnection(connectionString);
144 string provider = ConfigurationManager.AppSettings[
"CslaDbProvider"];
145 if (
string.IsNullOrEmpty(provider))
146 provider =
"System.Data.SqlClient";
148 DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
151 _connection = factory.CreateConnection();
152 _connection.ConnectionString = connectionString;
157 private static string GetContextName(
string connectionString,
string label)
159 return "__db:" + label +
"-" + connectionString;
175 private int _refCount;
183 get {
return _refCount; }
186 private void AddRef()
197 _connection.Dispose();
199 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
Exception thrown due to configuration errors.
Provides an automated way to reuse open database connections within the context of a single data port...
void Dispose()
Dispose object, dereferencing or disposing the connection it is managing.
static ConnectionManager GetManager(string database)
Gets the ConnectionManager object for the specified database.
static ConnectionManager GetManager(string database, bool isDatabaseName)
Gets the ConnectionManager object for the specified database.
static ConnectionManager GetManager(string database, bool isDatabaseName, string label)
Gets the ConnectionManager object for the specified database.
int RefCount
Gets the current reference count for this object.
static ConnectionManager GetManager(string database, string label)
Gets the ConnectionManager object for the specified database.
IDbConnection Connection
Dispose object, dereferencing or disposing the connection it is managing.
A strongly-typed resource class, for looking up localized strings, etc.
static string DatabaseNameNotFound
Looks up a localized string similar to Database name not found in config file ({0}).