-
Notifications
You must be signed in to change notification settings - Fork 20
/
ConnectionCache.cs
116 lines (103 loc) · 4.26 KB
/
ConnectionCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Transactions;
namespace SecurityDriven.TinyORM
{
internal static class ConnectionCache
{
public static ConcurrentDictionary<Transaction, ConnectionWrapperContainer> transactionConnections = new ConcurrentDictionary<Transaction, ConnectionWrapperContainer>();
public static ConnectionWrapper GetTransactionLinkedConnection(DbContext db)
{
Transaction currentTransaction = Transaction.Current;
if (currentTransaction == null) return null;
if (currentTransaction.TransactionInformation.Status == TransactionStatus.Aborted)
{
Throw_TransactionAbortedException();
}
static void Throw_TransactionAbortedException() => throw new TransactionAbortedException(nameof(GetTransactionLinkedConnection) + "() called on an already-aborted transaction.");
ConnectionWrapper wrappedConnection = null;
ref var _connectionString = ref db.connectionString;
var connectionWrapperContainer = transactionConnections.GetOrAdd(key: currentTransaction, valueFactory: _getNewConnectionCache);
lock (connectionWrapperContainer)
{
ref var _containerConnectionString = ref connectionWrapperContainer.ConnectionString;
ref var _containerConnectionWrapper = ref connectionWrapperContainer.ConnectionWrapper;
do
{
if (_containerConnectionString == null)
{
_containerConnectionString = _connectionString;
wrappedConnection = db.GetNewWrappedConnection();
_containerConnectionWrapper = wrappedConnection;
currentTransaction.TransactionCompleted += OnTransactionCompletedAction;
break;
}
ref var _containerConnectionWrapperDictionary = ref connectionWrapperContainer.ConnectionWrapperDictionary;
if (_containerConnectionWrapperDictionary == null)
{
if (_containerConnectionString == _connectionString)
{
System.Diagnostics.Debug.Assert(_containerConnectionWrapper != null);
wrappedConnection = _containerConnectionWrapper;
}
else
{
// Add a 2nd ConnectionWrapper
wrappedConnection = db.GetNewWrappedConnection();
_containerConnectionWrapperDictionary = new Dictionary<string, ConnectionWrapper>(capacity: 2, comparer: StringComparer.Ordinal)
{
{ _containerConnectionString, _containerConnectionWrapper },
{ _connectionString, wrappedConnection }
};
_containerConnectionWrapper = null;
}
break;
}
if (!_containerConnectionWrapperDictionary.TryGetValue(_connectionString, out wrappedConnection))
{
// Add 3rd or more ConnectionWrapper
wrappedConnection = db.GetNewWrappedConnection();
_containerConnectionWrapperDictionary.Add(_connectionString, wrappedConnection);
}
break;
} while (false);
}// release lock
wrappedConnection.IncrementUseCount();
return wrappedConnection;
}// GetTransactionLinkedConnection()
static TransactionCompletedEventHandler OnTransactionCompletedAction = OnTransactionCompleted;
static void OnTransactionCompleted(object sender, TransactionEventArgs e)
{
if (transactionConnections.TryRemove(e.Transaction, out var connectionWrapperContainer))
{
ref var _containerConnectionWrapper = ref connectionWrapperContainer.ConnectionWrapper;
if (_containerConnectionWrapper != null)
{
_containerConnectionWrapper.Dispose();
return;
}
ref var _containerConnectionWrapperDictionary = ref connectionWrapperContainer.ConnectionWrapperDictionary;
if (_containerConnectionWrapperDictionary != null)
{
var enumerator = _containerConnectionWrapperDictionary.GetEnumerator();
while (enumerator.MoveNext())
{
try
{
enumerator.Current.Value.Dispose();
}
catch { }
}// while enumerator
}// if dictionary
}// if connectionWrapperContainer
}// OnTransactionCompleted()
static Func<Transaction, ConnectionWrapperContainer> _getNewConnectionCache = transaction => new ConnectionWrapperContainer();
internal sealed class ConnectionWrapperContainer
{
public string ConnectionString;
public ConnectionWrapper ConnectionWrapper;
public Dictionary<string, ConnectionWrapper> ConnectionWrapperDictionary;
}// class ConnectionWrapperContainer
}// class ConnectionCache
}//ns