/// <summary> /// 初始化数据库连接 /// </summary> /// <param name="options"></param> public static void InitConnection(StagingOptions options) { CheckNotNull.NotNull(options, nameof(options)); CheckNotNull.NotEmpty(options.ConnectionMaster, nameof(options.ConnectionMaster)); Options = options; // 初始化主库连接实例 ConnectionStringConfiguration conn = new ConnectionStringConfiguration() { ConnectionString = options.ConnectionMaster, DbConnection = new Npgsql.NpgsqlConnection(options.ConnectionMaster) }; InstanceMaster = new MasterExecute(options.Logger, conn); // 初始化从库连接实例 List <ConnectionStringConfiguration> connList = GetSlaves(options.ConnectionSlaves); if (connList != null) { InstanceSlave = new SlaveExecute(options.Logger, connList); } if (options.CacheOptions != null && options.CacheOptions.Cache != null) { CacheManager = new CacheManager(options.CacheOptions); } }
/// <summary> /// 初始化数据库连接 /// </summary> /// <param name="logger">日志组件</param> /// <param name="connectionMaster">可读写数据库连接</param> /// <param name="connectionStringSlave">从库数据库连接</param> /// <param name="connectionSlaves">从库连接池总大小,如果不指定(默认 -1),如果没有设定 maximum pool size 的值,则从库中读取 maximum pool size 设定的值进行累计</param> public static void InitConnection(StagingOptions options) { if (options == null) { throw new ArgumentNullException(nameof(StagingOptions)); } if (string.IsNullOrEmpty(options.ConnectionMaster)) { throw new ArgumentNullException("connectionString not null"); } Options = options; // 初始化主库连接实例 int poolsizeMaster = GetPollSize(options.ConnectionMaster); ConnectionStringConfiguration connS = new ConnectionStringConfiguration() { ConnectionString = options.ConnectionMaster, MaxConnection = poolsizeMaster, DbConnection = new Npgsql.NpgsqlConnection(options.ConnectionMaster) }; InstanceMaster = new MasterExecute(options.Logger, connS); // 初始化从库连接实例 List <ConnectionStringConfiguration> connList = GetSlaves(options.ConnectionSlaves, out int poolsizeSlave); if (options.SlavesMaxPool != -1) { poolsizeSlave = options.SlavesMaxPool; } if (connList != null) { InstanceSlave = new SlaveExecute(options.Logger, connList, poolsizeSlave); } if (options.CacheOptions != null && options.CacheOptions.Cache != null) { Console.WriteLine("options.CacheOptions.Cache:{0}", options.CacheOptions.Cache == null); CacheManager = new CacheManager(options.CacheOptions); } }
/// <summary> /// 初始化数据库连接 /// </summary> /// <param name="logger">日志组件</param> /// <param name="connectionMaster">可读写数据库连接</param> /// <param name="connectionStringSlave">从库数据库连接</param> /// <param name="connectionSlaves">从库连接池总大小,如果不指定(默认 -1),如果没有设定 maximum pool size 的值,则从库中读取 maximum pool size 设定的值进行累计</param> public static void InitConnection(ILogger logger, string connectionMaster, string[] connectionSlaves = null, int slavesMaxPool = -1) { if (string.IsNullOrEmpty(connectionMaster)) { throw new ArgumentNullException("connectionString not null"); } // 初始化主库连接实例 int poolsizeMaster = GetPollSize(connectionMaster); ConnectionStringConfiguration connS = new ConnectionStringConfiguration() { ConnectionString = connectionMaster, MaxConnection = poolsizeMaster, DbConnection = new Npgsql.NpgsqlConnection(connectionMaster) }; instanceMaster = new MasterExecute(logger, connS); // 初始化从库连接实例 if (connectionSlaves != null && connectionSlaves.Length > 0) { int pollsizeSlave = 0; List <ConnectionStringConfiguration> connList = new List <ConnectionStringConfiguration>(); for (int i = 0; i < connectionSlaves.Length; i++) { var item = connectionSlaves[i]; connList.Add(new ConnectionStringConfiguration() { ConnectionString = item, Id = i, MaxConnection = GetPollSize(item), DbConnection = new Npgsql.NpgsqlConnection(connectionMaster) }); pollsizeSlave += connList[i].MaxConnection; } if (slavesMaxPool != -1) { pollsizeSlave = slavesMaxPool; } instanceSlave = new SlaveExecute(logger, connList, pollsizeSlave); } }