mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Update MasterSlave Demo
This commit is contained in:
parent
ddb3c3e17f
commit
02c0b845ef
@ -9,5 +9,7 @@ namespace OrmTest
|
||||
public class Config
|
||||
{
|
||||
public static string ConnectionString = "server=.;uid=sa;pwd=sasa;database=SqlSugar4XTest";
|
||||
public static string ConnectionString2 = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST";
|
||||
public static string ConnectionString3 = "server=.;uid=sa;pwd=sasa;database=sqlsugar4xtest";
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,43 @@
|
||||
using System;
|
||||
using OrmTest.Demo;
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest.Demos
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class MasterSlave
|
||||
public class MasterSlave : DemoBase
|
||||
{
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
var db = GetMasterSlaveInstance();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var list = db.Queryable<Student>().ToList(); // ConnectionString2 or ConnectionString3
|
||||
}
|
||||
db.Insertable(new Student() { Name = "masterTest" }).ExecuteCommand();// Config.ConnectionString
|
||||
}
|
||||
|
||||
public static SqlSugarClient GetMasterSlaveInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.SqlServer,
|
||||
IsAutoCloseConnection = true,
|
||||
SlaveConnectionStrings = new List<SlaveConnectionConfig>() {
|
||||
new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 },
|
||||
new SlaveConnectionConfig() { HitRate=30, ConnectionString=Config.ConnectionString3 }
|
||||
}
|
||||
});
|
||||
db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
Console.WriteLine(db.Ado.Connection.ConnectionString);
|
||||
};
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,6 +46,7 @@ namespace OrmTest
|
||||
OrmTest.Demo.ComplexModel.Init();
|
||||
OrmTest.Demo.CodeFirst.Init();
|
||||
OrmTest.Demo.Aop.Init();
|
||||
OrmTest.Demo.MasterSlave.Init();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -75,6 +75,16 @@ namespace SqlSugar
|
||||
{
|
||||
this.Connection.Close();
|
||||
}
|
||||
if (this.IsMasterSlaveSeparation)
|
||||
{
|
||||
foreach (var slaveConnection in this.SlaveConnections)
|
||||
{
|
||||
if (slaveConnection != null && slaveConnection.State == ConnectionState.Open)
|
||||
{
|
||||
slaveConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public virtual void Dispose()
|
||||
{
|
||||
@ -92,6 +102,17 @@ namespace SqlSugar
|
||||
this.Connection.Dispose();
|
||||
}
|
||||
this.Connection = null;
|
||||
|
||||
if (this.IsMasterSlaveSeparation)
|
||||
{
|
||||
foreach (var slaveConnection in this.SlaveConnections)
|
||||
{
|
||||
if (slaveConnection != null && slaveConnection.State == ConnectionState.Open)
|
||||
{
|
||||
slaveConnection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public virtual void CheckConnection()
|
||||
{
|
||||
@ -670,7 +691,7 @@ namespace SqlSugar
|
||||
}
|
||||
private void SetConnectionStart(string sql)
|
||||
{
|
||||
if (this.IsMasterSlaveSeparation&&IsRead(sql))
|
||||
if (this.IsMasterSlaveSeparation && IsRead(sql))
|
||||
{
|
||||
if (this.MasterConnection == null)
|
||||
{
|
||||
@ -681,19 +702,29 @@ namespace SqlSugar
|
||||
var currentSaveConnection = saves[currentIndex];
|
||||
this.Connection = null;
|
||||
this.Context.CurrentConnectionConfig.ConnectionString = currentSaveConnection.ConnectionString;
|
||||
var connection = this.SlaveConnections.FirstOrDefault(it => it.ToString() == currentSaveConnection.ConnectionString);
|
||||
if (connection == null)
|
||||
this.Connection = this.Connection;
|
||||
if (this.SlaveConnections.IsNullOrEmpty() || !this.SlaveConnections.Any(it => EqualsConnectionString(it.ConnectionString, this.Connection.ConnectionString)))
|
||||
{
|
||||
if (this.SlaveConnections == null) this.SlaveConnections = new List<IDbConnection>();
|
||||
this.SlaveConnections.Add(this.Connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool EqualsConnectionString(string connectionString1, string connectionString2)
|
||||
{
|
||||
var connectionString1Array = connectionString1.Split(';');
|
||||
var connectionString2Array = connectionString2.Split(';');
|
||||
var result = connectionString1Array.Except(connectionString2Array);
|
||||
return result.Count() == 0;
|
||||
}
|
||||
|
||||
private void SetConnectionEnd()
|
||||
{
|
||||
if (this.IsMasterSlaveSeparation)
|
||||
{
|
||||
this.Connection = this.MasterConnection;
|
||||
this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnection.ToString();
|
||||
this.Context.CurrentConnectionConfig.ConnectionString = this.MasterConnection.ConnectionString;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ namespace SqlSugar
|
||||
int maxValue = 0;
|
||||
foreach (var item in pars)
|
||||
{
|
||||
maxValue = +item.Value;
|
||||
maxValue += item.Value;
|
||||
}
|
||||
var num = Random.Next(1, maxValue);
|
||||
var result = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user