Explorar o código

数据库连接后,马上进入Read Unommit模式

shuiping150 hai 2 días
pai
achega
a0b8e0ad33

+ 2 - 1
JLHHJSvr/Excutor/JLH_FetchPriceExcutor.cs

@@ -4,6 +4,7 @@ using System.Data.SqlClient;
 using JLHHJSvr.Com;
 using JLHHJSvr.Com.Model;
 using LJLib.Net.SPI.Server;
+using LJLib.SQLEX;
 
 namespace JLHHJSvr.Excutor
 {
@@ -17,7 +18,7 @@ namespace JLHHJSvr.Excutor
                 return;
             }
 
-            using (var con = new SqlConnection(GlobalVar.ConnectionString))
+            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
             using (var cmd = con.CreateCommand())
             {
                 con.Open();

+ 3 - 0
JLHHJSvr/JLHHJSvr.csproj

@@ -449,6 +449,9 @@
     <Compile Include="LJFrameWork\LJException\LJException.cs" />
     <Compile Include="LJFrameWork\ljlib.cache\LJCache.cs" />
     <Compile Include="LJFrameWork\LJLib.Method\SortedListEx.cs" />
+    <Compile Include="LJFrameWork\LJLib.SQLEX\LJTransactionScope.cs" />
+    <Compile Include="LJFrameWork\LJLib.SQLEX\SqlConnectionStringEx.cs" />
+    <Compile Include="LJFrameWork\LJLib.SQLEX\TransactionGroup.cs" />
     <Compile Include="LJFrameWork\Tools\LJExprParser.cs" />
     <Compile Include="LJFrameWork\Tools\LJHttpUtil.cs" />
     <Compile Include="LJFrameWork\Tools\menuRecursion.cs" />

+ 65 - 0
JLHHJSvr/LJFrameWork/LJLib.SQLEX/LJTransactionScope.cs

@@ -0,0 +1,65 @@
+using System;
+using System.Data;
+using System.Data.SqlClient;
+using System.Diagnostics;
+
+namespace LJLib.SQLEX
+{
+    public sealed class LJTransactionScope : IDisposable
+    {
+        private bool complete = false;
+        private bool ifCreateTran = false;
+        private SqlCommand _cmd;
+        private string pointName;
+
+
+        public LJTransactionScope(SqlCommand cmd)
+        {
+            Trace.Assert(cmd != null && cmd.Connection.State == ConnectionState.Open, "cmd != null && cmd.Connection.State == ConnectionState.Open");
+            pointName = Guid.NewGuid().ToString("N");
+            _cmd = cmd;
+            if (_cmd == null) return;
+            if (_cmd.Transaction == null)
+            {
+                _cmd.Transaction = _cmd.Connection.BeginTransaction(IsolationLevel.ReadUncommitted);
+                ifCreateTran = true;
+            }
+            _cmd.Transaction.Save(pointName);
+        }
+
+        public void Complete()
+        {
+            if (_cmd != null && ifCreateTran)
+            {
+                _cmd.Transaction.Commit();
+            }
+            complete = true;
+        }
+
+        public void Dispose()
+        {
+            if (_cmd == null) return;
+            if (!complete)
+            {
+                try
+                {
+                    _cmd.Transaction.Rollback(pointName);
+                    if (ifCreateTran)
+                    {
+                        _cmd.Transaction.Rollback();
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Trace.Write(ex);
+                }
+            }
+
+            if (ifCreateTran)
+            {
+                using (_cmd.Transaction) { }
+                _cmd.Transaction = null;
+            }
+        }
+    }
+}

+ 60 - 0
JLHHJSvr/LJFrameWork/LJLib.SQLEX/SqlConnectionStringEx.cs

@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.SqlClient;
+using System.Diagnostics;
+using System.Security.Cryptography;
+
+namespace LJLib.SQLEX
+{
+    internal static class SqlConnectionStringEx
+    {
+        public static SqlConnection NewSqlConnection(this string connectionString, bool pooling = false)
+        {
+            var builder = new SqlConnectionStringBuilder(connectionString);
+            builder.Pooling = pooling;
+            builder.PersistSecurityInfo = true;
+            if (pooling)
+            {
+                builder.MaxPoolSize = 200;
+                builder.MinPoolSize = 20;
+                if (builder.ConnectTimeout < 30)
+                {
+                    builder.ConnectTimeout = 30;
+                }
+            }
+            var rslt = new SqlConnection(builder.ConnectionString);
+            rslt.StateChange += (sender, args) =>
+            {
+                if (args.OriginalState == ConnectionState.Closed && args.CurrentState == ConnectionState.Open)
+                {
+                    var cmd = (sender as SqlConnection).CreateCommand();
+                    cmd.CommandText = "set transaction isolation level READ UNCOMMITTED";
+                    cmd.ExecuteNonQuery();
+                    cmd.CommandText = "SET DEADLOCK_PRIORITY low";
+                    cmd.ExecuteNonQuery();
+                }
+            };
+            return rslt;
+        }
+        public static SqlConnection NewSqlConnectionReadCommitted(this string connectionString, bool pooling = false)
+        {
+            var builder = new SqlConnectionStringBuilder(connectionString);
+            builder.Pooling = pooling;
+            builder.PersistSecurityInfo = true;
+            var rslt = new SqlConnection(builder.ConnectionString);
+            rslt.StateChange += (sender, args) =>
+            {
+                if (args.OriginalState == ConnectionState.Closed && args.CurrentState == ConnectionState.Open)
+                {
+                    var cmd = (sender as SqlConnection).CreateCommand();
+                    cmd.CommandText = "set transaction isolation level READ COMMITTED";
+                    cmd.ExecuteNonQuery();
+                    cmd.CommandText = "SET DEADLOCK_PRIORITY low";
+                    cmd.ExecuteNonQuery();
+                }
+            };
+            return rslt;
+        }
+    }
+}

+ 70 - 0
JLHHJSvr/LJFrameWork/LJLib.SQLEX/TransactionGroup.cs

@@ -0,0 +1,70 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.SqlClient;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace LJLib.SQLEX
+{
+    public class TransactionGroup : IDisposable
+    {
+        private List<LJTransactionScope> _transactions;
+
+        public TransactionGroup(IEnumerable<SqlCommand> cmds)
+        {
+            _transactions = new List<LJTransactionScope>();
+            foreach (var cmd in cmds)
+            {
+                if (cmd == null)
+                {
+                    continue;
+                }
+
+                if (cmd.Connection == null)
+                {
+                    continue;
+                }
+
+                if (cmd.Connection.State != ConnectionState.Open)
+                {
+                    continue;
+                }
+
+                _transactions.Add(new LJTransactionScope(cmd));
+            }
+        }
+        
+        public void Commit()
+        {
+            foreach (var ts in _transactions)
+            {
+                try
+                {
+                    ts.Complete();
+                }
+                catch (Exception e)
+                {
+                    Trace.Write(e);
+                }
+            }
+        }
+
+        public void Dispose()
+        {
+            foreach (var ts in _transactions)
+            {
+                try
+                {
+                    ts.Dispose();
+                }
+                catch (Exception e)
+                {
+                    Trace.Write(e);
+                }
+            }
+        }
+    }
+}