Skip to content

Commit ca80fba

Browse files
committed
- 优化 Ado读写分离存储过程默认使用从库;#1833
1 parent c9a311d commit ca80fba

File tree

5 files changed

+306
-5
lines changed

5 files changed

+306
-5
lines changed

FreeSql/Extensions/FreeSqlGlobalExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ WHERE @cte_ids IS NOT NULL
833833
#endif
834834

835835
var nsselsb = new StringBuilder();
836-
if (AdoProvider.IsFromSlave(select._select) == false) nsselsb.Append(' '); //读写分离规则,如果强制读主库,则在前面加个空格
836+
if (AdoProvider.IsFromSlave(select._select, CommandType.Text) == false) nsselsb.Append(' '); //读写分离规则,如果强制读主库,则在前面加个空格
837837
nsselsb.Append("WITH ");
838838
switch (select._orm.Ado.DataType)
839839
{

FreeSql/FreeSql.xml

Lines changed: 299 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Internal/CommonProvider/AdoProvider/AdoProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ public void ExecuteReaderMultiple(int multipleResult, DbConnection connection, D
585585
if (transaction == null && connection == null)
586586
{
587587
//读写分离规则
588-
if (this.SlavePools.Any() && IsFromSlave(cmdText))
588+
if (this.SlavePools.Any() && IsFromSlave(cmdText, cmdType))
589589
{
590590
var availables = slaveUnavailables == 0 ?
591591
//查从库

FreeSql/Internal/CommonProvider/AdoProvider/AdoProviderAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ async public Task ExecuteReaderMultipleAsync(int multipleResult, DbConnection co
505505
if (transaction == null && connection == null)
506506
{
507507
//读写分离规则
508-
if (this.SlavePools.Any() && IsFromSlave(cmdText))
508+
if (this.SlavePools.Any() && IsFromSlave(cmdText, cmdType))
509509
{
510510
var availables = slaveUnavailables == 0 ?
511511
//查从库

FreeSql/Internal/CommonProvider/AdoProvider/AdoProviderUtils.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections;
44
using System.Collections.Concurrent;
5+
using System.Data;
56
using System.Globalization;
67
using System.Text;
78
using System.Text.RegularExpressions;
@@ -56,9 +57,10 @@ protected string AddslashesIEnumerable(object param, Type mapType, ColumnInfo ma
5657
return sb.Length == 0 ? "(NULL)" : sb.Remove(0, 1).Insert(0, "(").Append(")").ToString();
5758
}
5859

59-
public static bool IsFromSlave(string cmdText)
60+
public static bool IsFromSlave(string cmdText, CommandType cmdType)
6061
{
61-
return cmdText.StartsWith("SELECT ", StringComparison.CurrentCultureIgnoreCase) ||
62+
return cmdType == CommandType.StoredProcedure ||
63+
cmdText.StartsWith("SELECT ", StringComparison.CurrentCultureIgnoreCase) ||
6264
cmdText.StartsWith("WITH ", StringComparison.CurrentCultureIgnoreCase);
6365
}
6466
}

0 commit comments

Comments
 (0)