mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-12-24 01:25:48 +08:00
源代码改用vs默认格式化
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -6,126 +6,158 @@ using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace FreeSql.Internal.CommonProvider {
|
||||
partial class AdoProvider {
|
||||
namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
partial class AdoProvider
|
||||
{
|
||||
|
||||
class Transaction2 {
|
||||
internal Object<DbConnection> Conn;
|
||||
internal DbTransaction Transaction;
|
||||
internal DateTime RunTime;
|
||||
internal TimeSpan Timeout;
|
||||
class Transaction2
|
||||
{
|
||||
internal Object<DbConnection> Conn;
|
||||
internal DbTransaction Transaction;
|
||||
internal DateTime RunTime;
|
||||
internal TimeSpan Timeout;
|
||||
|
||||
public Transaction2(Object<DbConnection> conn, DbTransaction tran, TimeSpan timeout) {
|
||||
Conn = conn;
|
||||
Transaction = tran;
|
||||
RunTime = DateTime.Now;
|
||||
Timeout = timeout;
|
||||
}
|
||||
}
|
||||
public Transaction2(Object<DbConnection> conn, DbTransaction tran, TimeSpan timeout)
|
||||
{
|
||||
Conn = conn;
|
||||
Transaction = tran;
|
||||
RunTime = DateTime.Now;
|
||||
Timeout = timeout;
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<int, Transaction2> _trans = new Dictionary<int, Transaction2>();
|
||||
private object _trans_lock = new object();
|
||||
private Dictionary<int, Transaction2> _trans = new Dictionary<int, Transaction2>();
|
||||
private object _trans_lock = new object();
|
||||
|
||||
public DbTransaction TransactionCurrentThread => _trans.TryGetValue(Thread.CurrentThread.ManagedThreadId, out var conn) && conn.Transaction?.Connection != null ? conn.Transaction : null;
|
||||
public DbTransaction TransactionCurrentThread => _trans.TryGetValue(Thread.CurrentThread.ManagedThreadId, out var conn) && conn.Transaction?.Connection != null ? conn.Transaction : null;
|
||||
|
||||
public void BeginTransaction(TimeSpan timeout) {
|
||||
if (TransactionCurrentThread != null) return;
|
||||
public void BeginTransaction(TimeSpan timeout)
|
||||
{
|
||||
if (TransactionCurrentThread != null) return;
|
||||
|
||||
int tid = Thread.CurrentThread.ManagedThreadId;
|
||||
Transaction2 tran = null;
|
||||
Object<DbConnection> conn = null;
|
||||
int tid = Thread.CurrentThread.ManagedThreadId;
|
||||
Transaction2 tran = null;
|
||||
Object<DbConnection> conn = null;
|
||||
|
||||
try {
|
||||
conn = MasterPool.Get();
|
||||
tran = new Transaction2(conn, conn.Value.BeginTransaction(), timeout);
|
||||
} catch(Exception ex) {
|
||||
Trace.WriteLine($"数据库出错(开启事务){ex.Message} \r\n{ex.StackTrace}");
|
||||
MasterPool.Return(conn);
|
||||
throw ex;
|
||||
}
|
||||
if (_trans.ContainsKey(tid)) CommitTransaction();
|
||||
try
|
||||
{
|
||||
conn = MasterPool.Get();
|
||||
tran = new Transaction2(conn, conn.Value.BeginTransaction(), timeout);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Trace.WriteLine($"数据库出错(开启事务){ex.Message} \r\n{ex.StackTrace}");
|
||||
MasterPool.Return(conn);
|
||||
throw ex;
|
||||
}
|
||||
if (_trans.ContainsKey(tid)) CommitTransaction();
|
||||
|
||||
lock (_trans_lock)
|
||||
_trans.Add(tid, tran);
|
||||
}
|
||||
lock (_trans_lock)
|
||||
_trans.Add(tid, tran);
|
||||
}
|
||||
|
||||
private void AutoCommitTransaction() {
|
||||
if (_trans.Count > 0) {
|
||||
Transaction2[] trans = null;
|
||||
lock (_trans_lock)
|
||||
trans = _trans.Values.Where(st2 => DateTime.Now.Subtract(st2.RunTime) > st2.Timeout).ToArray();
|
||||
foreach (Transaction2 tran in trans) CommitTransaction(true, tran);
|
||||
}
|
||||
}
|
||||
private void CommitTransaction(bool isCommit, Transaction2 tran) {
|
||||
if (tran == null || tran.Transaction == null || tran.Transaction.Connection == null) return;
|
||||
private void AutoCommitTransaction()
|
||||
{
|
||||
if (_trans.Count > 0)
|
||||
{
|
||||
Transaction2[] trans = null;
|
||||
lock (_trans_lock)
|
||||
trans = _trans.Values.Where(st2 => DateTime.Now.Subtract(st2.RunTime) > st2.Timeout).ToArray();
|
||||
foreach (Transaction2 tran in trans) CommitTransaction(true, tran);
|
||||
}
|
||||
}
|
||||
private void CommitTransaction(bool isCommit, Transaction2 tran)
|
||||
{
|
||||
if (tran == null || tran.Transaction == null || tran.Transaction.Connection == null) return;
|
||||
|
||||
if (_trans.ContainsKey(tran.Conn.LastGetThreadId))
|
||||
lock (_trans_lock)
|
||||
if (_trans.ContainsKey(tran.Conn.LastGetThreadId))
|
||||
_trans.Remove(tran.Conn.LastGetThreadId);
|
||||
if (_trans.ContainsKey(tran.Conn.LastGetThreadId))
|
||||
lock (_trans_lock)
|
||||
if (_trans.ContainsKey(tran.Conn.LastGetThreadId))
|
||||
_trans.Remove(tran.Conn.LastGetThreadId);
|
||||
|
||||
Exception ex = null;
|
||||
var f001 = isCommit ? "提交" : "回滚";
|
||||
try {
|
||||
Trace.WriteLine($"线程{tran.Conn.LastGetThreadId}事务{f001}");
|
||||
if (isCommit) tran.Transaction.Commit();
|
||||
else tran.Transaction.Rollback();
|
||||
} catch (Exception ex2) {
|
||||
ex = ex2;
|
||||
Trace.WriteLine($"数据库出错({f001}事务):{ex.Message} {ex.StackTrace}");
|
||||
} finally {
|
||||
ReturnConnection(MasterPool, tran.Conn, ex); //MasterPool.Return(tran.Conn, ex);
|
||||
}
|
||||
}
|
||||
private void CommitTransaction(bool isCommit) {
|
||||
if (_trans.TryGetValue(Thread.CurrentThread.ManagedThreadId, out var tran)) CommitTransaction(isCommit, tran);
|
||||
}
|
||||
public void CommitTransaction() => CommitTransaction(true);
|
||||
public void RollbackTransaction() => CommitTransaction(false);
|
||||
Exception ex = null;
|
||||
var f001 = isCommit ? "提交" : "回滚";
|
||||
try
|
||||
{
|
||||
Trace.WriteLine($"线程{tran.Conn.LastGetThreadId}事务{f001}");
|
||||
if (isCommit) tran.Transaction.Commit();
|
||||
else tran.Transaction.Rollback();
|
||||
}
|
||||
catch (Exception ex2)
|
||||
{
|
||||
ex = ex2;
|
||||
Trace.WriteLine($"数据库出错({f001}事务):{ex.Message} {ex.StackTrace}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
ReturnConnection(MasterPool, tran.Conn, ex); //MasterPool.Return(tran.Conn, ex);
|
||||
}
|
||||
}
|
||||
private void CommitTransaction(bool isCommit)
|
||||
{
|
||||
if (_trans.TryGetValue(Thread.CurrentThread.ManagedThreadId, out var tran)) CommitTransaction(isCommit, tran);
|
||||
}
|
||||
public void CommitTransaction() => CommitTransaction(true);
|
||||
public void RollbackTransaction() => CommitTransaction(false);
|
||||
|
||||
public void Transaction(Action handler) {
|
||||
Transaction(handler, TimeSpan.FromSeconds(60));
|
||||
}
|
||||
public void Transaction(Action handler, TimeSpan timeout) {
|
||||
try {
|
||||
BeginTransaction(timeout);
|
||||
handler();
|
||||
CommitTransaction();
|
||||
} catch (Exception ex) {
|
||||
RollbackTransaction();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
public void Transaction(Action handler)
|
||||
{
|
||||
Transaction(handler, TimeSpan.FromSeconds(60));
|
||||
}
|
||||
public void Transaction(Action handler, TimeSpan timeout)
|
||||
{
|
||||
try
|
||||
{
|
||||
BeginTransaction(timeout);
|
||||
handler();
|
||||
CommitTransaction();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
RollbackTransaction();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
~AdoProvider() {
|
||||
this.Dispose();
|
||||
}
|
||||
bool _isdisposed = false;
|
||||
public void Dispose() {
|
||||
if (_isdisposed) return;
|
||||
try {
|
||||
Transaction2[] trans = null;
|
||||
lock (_trans_lock)
|
||||
trans = _trans.Values.ToArray();
|
||||
foreach (Transaction2 tran in trans) CommitTransaction(false, tran);
|
||||
} catch { }
|
||||
~AdoProvider()
|
||||
{
|
||||
this.Dispose();
|
||||
}
|
||||
bool _isdisposed = false;
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isdisposed) return;
|
||||
try
|
||||
{
|
||||
Transaction2[] trans = null;
|
||||
lock (_trans_lock)
|
||||
trans = _trans.Values.ToArray();
|
||||
foreach (Transaction2 tran in trans) CommitTransaction(false, tran);
|
||||
}
|
||||
catch { }
|
||||
|
||||
ObjectPool<DbConnection>[] pools = null;
|
||||
for (var a = 0; a < 10; a++) {
|
||||
try {
|
||||
pools = SlavePools.ToArray();
|
||||
SlavePools.Clear();
|
||||
break;
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
if (pools != null) {
|
||||
foreach (var pool in pools) {
|
||||
try { pool.Dispose(); } catch { }
|
||||
}
|
||||
}
|
||||
try { MasterPool.Dispose(); } catch { }
|
||||
}
|
||||
}
|
||||
ObjectPool<DbConnection>[] pools = null;
|
||||
for (var a = 0; a < 10; a++)
|
||||
{
|
||||
try
|
||||
{
|
||||
pools = SlavePools.ToArray();
|
||||
SlavePools.Clear();
|
||||
break;
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
if (pools != null)
|
||||
{
|
||||
foreach (var pool in pools)
|
||||
{
|
||||
try { pool.Dispose(); } catch { }
|
||||
}
|
||||
}
|
||||
try { MasterPool.Dispose(); } catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,21 +2,25 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace FreeSql.Internal.CommonProvider {
|
||||
partial class AdoProvider {
|
||||
public abstract object AddslashesProcessParam(object param, Type mapType);
|
||||
public string Addslashes(string filter, params object[] parms) {
|
||||
if (filter == null || parms == null) return string.Empty;
|
||||
if (parms.Length == 0) return filter;
|
||||
var nparms = new object[parms.Length];
|
||||
for (int a = 0; a < parms.Length; a++) {
|
||||
if (parms[a] == null)
|
||||
filter = _dicAddslashesReplaceIsNull.GetOrAdd(a, b => new Regex(@"\s*(=|IN)\s*\{" + b + @"\}", RegexOptions.IgnoreCase | RegexOptions.Compiled))
|
||||
.Replace(filter, $" IS {{{a}}}");
|
||||
nparms[a] = AddslashesProcessParam(parms[a], null);
|
||||
}
|
||||
try { string ret = string.Format(filter, nparms); return ret; } catch { return filter; }
|
||||
}
|
||||
static ConcurrentDictionary<int, Regex> _dicAddslashesReplaceIsNull = new ConcurrentDictionary<int, Regex>();
|
||||
}
|
||||
namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
partial class AdoProvider
|
||||
{
|
||||
public abstract object AddslashesProcessParam(object param, Type mapType);
|
||||
public string Addslashes(string filter, params object[] parms)
|
||||
{
|
||||
if (filter == null || parms == null) return string.Empty;
|
||||
if (parms.Length == 0) return filter;
|
||||
var nparms = new object[parms.Length];
|
||||
for (int a = 0; a < parms.Length; a++)
|
||||
{
|
||||
if (parms[a] == null)
|
||||
filter = _dicAddslashesReplaceIsNull.GetOrAdd(a, b => new Regex(@"\s*(=|IN)\s*\{" + b + @"\}", RegexOptions.IgnoreCase | RegexOptions.Compiled))
|
||||
.Replace(filter, $" IS {{{a}}}");
|
||||
nparms[a] = AddslashesProcessParam(parms[a], null);
|
||||
}
|
||||
try { string ret = string.Format(filter, nparms); return ret; } catch { return filter; }
|
||||
}
|
||||
static ConcurrentDictionary<int, Regex> _dicAddslashesReplaceIsNull = new ConcurrentDictionary<int, Regex>();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user