Datei: NDODLL/NDOTransactionScope.cs
Last Commit (e34b4f0)
| 1 | using NDO.Mapping; |
| 2 | using NDOInterfaces; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Data; |
| 6 | using System.Linq; |
| 7 | using System.Text.RegularExpressions; |
| 8 | |
| 9 | namespace NDO |
| 10 | { |
| 11 | ····/// <summary> |
| 12 | ····/// class NDOTransactionScope |
| 13 | ····/// </summary> |
| 14 | ····public class NDOTransactionScope : INDOTransactionScope |
| 15 | ····{ |
| 16 | ········private PersistenceManager pm; |
| 17 | |
| 18 | private Dictionary<string, IDbConnection> usedConnections = new Dictionary<string, IDbConnection>( ) ; |
| 19 | ········private Dictionary<string, IDbTransaction> usedTransactions = new Dictionary<string, IDbTransaction>(); |
| 20 | |
| 21 | ········///<inheritdoc/> |
| 22 | ········public IsolationLevel IsolationLevel { get; set; } |
| 23 | ········///<inheritdoc/> |
| 24 | ········public TransactionMode TransactionMode { get; set; } |
| 25 | |
| 26 | ········bool isInTransaction = false; |
| 27 | |
| 28 | ········/// <summary> |
| 29 | ········/// Constructs an NDOTransactionScope object. |
| 30 | ········/// </summary> |
| 31 | ········public NDOTransactionScope() |
| 32 | ········{ |
| 33 | ············IsolationLevel = IsolationLevel.ReadCommitted; |
| 34 | ············TransactionMode = TransactionMode.Optimistic; |
| 35 | ········} |
| 36 | |
| 37 | ········///<inheritdoc/> |
| 38 | ········public void CheckTransaction() |
| 39 | ········{ |
| 40 | ············if (TransactionMode == TransactionMode.None) |
| 41 | ············{ |
| 42 | ················return; |
| 43 | ············} |
| 44 | |
| 45 | ············if (!this.isInTransaction) |
| 46 | ············{ |
| 47 | ················foreach (var connId in this.usedConnections.Keys) |
| 48 | ················{ |
| 49 | OpenConnAndStartTransaction( connId, this. usedConnections[connId] ) ; |
| 50 | ················} |
| 51 | ············} |
| 52 | |
| 53 | ············this.isInTransaction = true; |
| 54 | ········} |
| 55 | |
| 56 | private void OpenConnAndStartTransaction( string connId, IDbConnection conn ) |
| 57 | ········{ |
| 58 | ············conn.Open(); |
| 59 | var serverId = ( ( INdoDbConnection) conn) . ConnectionId; |
| 60 | ············pm.LogIfVerbose( $"Opening connection {serverId} = '{conn.DisplayName()}'" ); |
| 61 | ············var tx = conn.BeginTransaction(IsolationLevel); |
| 62 | usedTransactions. Add( connId, tx ) ; |
| 63 | ············this.pm.LogIfVerbose( $"Starting transaction {tx.GetHashCode():X} at connection {serverId} = '{conn.DisplayName()}'" ); |
| 64 | ········} |
| 65 | |
| 66 | ········///<inheritdoc/> |
| 67 | ········public void Complete() |
| 68 | ········{ |
| 69 | ············CommitTransactions(); |
| 70 | ············CloseConnections(); |
| 71 | ············isInTransaction = false; |
| 72 | ········} |
| 73 | |
| 74 | ········private void CommitTransactions() |
| 75 | ········{ |
| 76 | ············foreach (var id in usedTransactions.Keys) |
| 77 | ············{ |
| 78 | ················var tx = usedTransactions[id]; |
| 79 | ················tx.Commit(); |
| 80 | |
| 81 | IDbConnection conn = null; |
| 82 | usedConnections. TryGetValue( id, out conn ) ; |
| 83 | ················if (conn == null) |
| 84 | ····················throw new NDOException( 121, $"Can't commit. No open connection found for NDO Connection {id} ({conn.DisplayName()})" ); |
| 85 | var serverId = ( ( INdoDbConnection) conn) . ConnectionId; |
| 86 | ················this.pm.LogIfVerbose( $"Committing transaction {tx.GetHashCode():X} at connection {serverId} = '{conn.DisplayName()}'" ); |
| 87 | ············} |
| 88 | |
| 89 | ············usedTransactions.Clear(); |
| 90 | ········} |
| 91 | |
| 92 | ········///<inheritdoc/> |
| 93 | public IDbConnection GetConnection( string id, Func<IDbConnection> factory ) |
| 94 | ········{ |
| 95 | ············if (this.usedConnections.ContainsKey( id )) |
| 96 | ············{ |
| 97 | return this. usedConnections[id]; |
| 98 | ············} |
| 99 | ············else |
| 100 | ············{ |
| 101 | ················var conn = factory(); |
| 102 | this. usedConnections. Add( id, conn ) ; |
| 103 | ················if (this.isInTransaction) |
| 104 | OpenConnAndStartTransaction( id, conn ) ; |
| 105 | ················return conn; |
| 106 | ············} |
| 107 | ········} |
| 108 | |
| 109 | |
| 110 | ········///<inheritdoc/> |
| 111 | ········public IDbTransaction GetTransaction( string id ) |
| 112 | ········{ |
| 113 | ············if (isInTransaction) |
| 114 | ············{ |
| 115 | ················IDbTransaction tx = null; |
| 116 | ················this.usedTransactions.TryGetValue( id, out tx ); |
| 117 | ················return tx; |
| 118 | ············} |
| 119 | |
| 120 | ············return null; |
| 121 | ········} |
| 122 | |
| 123 | ········///<inheritdoc/> |
| 124 | ········public void Dispose() |
| 125 | ········{ |
| 126 | ············RollbackTransactions(); |
| 127 | ············CloseConnections(); |
| 128 | ············this.isInTransaction = false; |
| 129 | ········} |
| 130 | |
| 131 | ········private void RollbackTransactions() |
| 132 | ········{ |
| 133 | ············foreach (var key in usedTransactions.Keys) |
| 134 | ············{ |
| 135 | ················var tx = this.usedTransactions[key]; |
| 136 | ················var id = tx.GetHashCode(); |
| 137 | ················try |
| 138 | ················{ |
| 139 | ····················// See https://github.com/dotnet/runtime/issues/95399 |
| 140 | ····················// We don't have any information about the state of the transaction. |
| 141 | ····················// If it is completed, we will get an exception here. |
| 142 | ····················// Given that in most cases it's possible to track the tx state outside of NDO, |
| 143 | ····················// we are safe here in the most cases. |
| 144 | ····················tx.Rollback(); |
| 145 | ················} |
| 146 | ················catch |
| 147 | ················{ |
| 148 | ················} |
| 149 | IDbConnection conn = null; |
| 150 | this. usedConnections. TryGetValue( key, out conn ) ; |
| 151 | ················{ |
| 152 | var serverId = ( ( INdoDbConnection) conn) . ConnectionId; |
| 153 | ····················this.pm.LogIfVerbose( $"Rollback transaction {id.ToString( "X" )} at connection {serverId} = '{conn.DisplayName()}'" ); |
| 154 | ················} |
| 155 | ············} |
| 156 | |
| 157 | ············usedTransactions.Clear(); |
| 158 | ········} |
| 159 | |
| 160 | ········private void CloseConnections() |
| 161 | ········{ |
| 162 | foreach ( var conn in this. usedConnections. Values. Where( c => c. State == ConnectionState. Open ) ) |
| 163 | ············{ |
| 164 | var serverId = ( ( INdoDbConnection) conn) . ConnectionId; |
| 165 | ················pm.LogIfVerbose( $"Closed connection {serverId} = '{conn.DisplayName()}'" ); |
| 166 | conn. Close( ) ; |
| 167 | ············} |
| 168 | |
| 169 | ············this.usedConnections.Clear(); |
| 170 | ········} |
| 171 | |
| 172 | ········/// <inheritdoc/> |
| 173 | ········public INDOTransactionScope Initialize( PersistenceManager pm ) |
| 174 | ········{ |
| 175 | ············this.pm = pm; |
| 176 | ············return this; |
| 177 | ········} |
| 178 | ····} |
| 179 | |
| 180 | ····static class ConnectionExtension |
| 181 | ····{ |
| 182 | ········public static string DisplayName(this IDbConnection conn) |
| 183 | ········{ |
| 184 | ············if (conn == null) |
| 185 | ················return "??"; |
| 186 | |
| 187 | ············var str = conn.ConnectionString; |
| 188 | ············if (String.IsNullOrEmpty( str )) |
| 189 | ················return str; |
| 190 | ············Regex regex = new Regex( "password=[^;]*" ); |
| 191 | ············return regex.Replace( str, "password=***" ); |
| 192 | ········} |
| 193 | ····} |
| 194 | } |
| 195 |
New Commit (14cacf2)
| 1 | using NDO.Mapping; |
| 2 | using NDOInterfaces; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Data; |
| 6 | using System.Linq; |
| 7 | using System.Text.RegularExpressions; |
| 8 | |
| 9 | namespace NDO |
| 10 | { |
| 11 | ····class UsedConnectionsInfo |
| 12 | ····{ |
| 13 | ········public IDbConnection Connection; |
| 14 | ········public IProvider Provider; |
| 15 | ····} |
| 16 | |
| 17 | ····/// <summary> |
| 18 | ····/// class NDOTransactionScope |
| 19 | ····/// </summary> |
| 20 | ····public class NDOTransactionScope : INDOTransactionScope |
| 21 | ····{ |
| 22 | ········private PersistenceManager pm; |
| 23 | |
| 24 | private Dictionary<string, UsedConnectionsInfo> usedConnections = new Dictionary<string, UsedConnectionsInfo>( ) ; |
| 25 | ········private Dictionary<string, IDbTransaction> usedTransactions = new Dictionary<string, IDbTransaction>(); |
| 26 | |
| 27 | ········///<inheritdoc/> |
| 28 | ········public IsolationLevel IsolationLevel { get; set; } |
| 29 | ········///<inheritdoc/> |
| 30 | ········public TransactionMode TransactionMode { get; set; } |
| 31 | |
| 32 | ········bool isInTransaction = false; |
| 33 | |
| 34 | ········/// <summary> |
| 35 | ········/// Constructs an NDOTransactionScope object. |
| 36 | ········/// </summary> |
| 37 | ········public NDOTransactionScope() |
| 38 | ········{ |
| 39 | ············IsolationLevel = IsolationLevel.ReadCommitted; |
| 40 | ············TransactionMode = TransactionMode.Optimistic; |
| 41 | ········} |
| 42 | |
| 43 | ········///<inheritdoc/> |
| 44 | ········public void CheckTransaction() |
| 45 | ········{ |
| 46 | ············if (TransactionMode == TransactionMode.None) |
| 47 | ············{ |
| 48 | ················return; |
| 49 | ············} |
| 50 | |
| 51 | ············if (!this.isInTransaction) |
| 52 | ············{ |
| 53 | ················foreach (var connId in this.usedConnections.Keys) |
| 54 | ················{ |
| 55 | OpenConnAndStartTransaction( connId ) ; |
| 56 | ················} |
| 57 | ············} |
| 58 | |
| 59 | ············this.isInTransaction = true; |
| 60 | ········} |
| 61 | |
| 62 | private void OpenConnAndStartTransaction( string id ) |
| 63 | ········{ |
| 64 | ············var cinfo = this.usedConnections[id]; |
| 65 | ············var provider = cinfo.Provider; |
| 66 | ············var conn = cinfo.Connection; |
| 67 | ············conn.Open(); |
| 68 | var serverId = provider. GetConnectionId( conn) ; |
| 69 | ············pm.LogIfVerbose( $"Opening connection {serverId} = '{conn.DisplayName()}'" ); |
| 70 | ············var tx = conn.BeginTransaction(IsolationLevel); |
| 71 | usedTransactions. Add( id, tx ) ; |
| 72 | ············this.pm.LogIfVerbose( $"Starting transaction {tx.GetHashCode():X} at connection {serverId} = '{conn.DisplayName()}'" ); |
| 73 | ········} |
| 74 | |
| 75 | ········///<inheritdoc/> |
| 76 | ········public void Complete() |
| 77 | ········{ |
| 78 | ············CommitTransactions(); |
| 79 | ············CloseConnections(); |
| 80 | ············isInTransaction = false; |
| 81 | ········} |
| 82 | |
| 83 | ········private void CommitTransactions() |
| 84 | ········{ |
| 85 | ············foreach (var id in usedTransactions.Keys) |
| 86 | ············{ |
| 87 | ················var tx = usedTransactions[id]; |
| 88 | ················tx.Commit(); |
| 89 | |
| 90 | usedConnections. TryGetValue( id, out var cinfo ) ; |
| 91 | var conn = cinfo?. Connection; |
| 92 | ················if (conn == null) |
| 93 | ····················throw new NDOException( 121, $"Can't commit. No open connection found for NDO Connection {id} ({conn.DisplayName()})" ); |
| 94 | var serverId = cinfo. Provider. GetConnectionId( conn) ; |
| 95 | ················this.pm.LogIfVerbose( $"Committing transaction {tx.GetHashCode():X} at connection {serverId} = '{conn.DisplayName()}'" ); |
| 96 | ············} |
| 97 | |
| 98 | ············usedTransactions.Clear(); |
| 99 | ········} |
| 100 | |
| 101 | ········///<inheritdoc/> |
| 102 | public IDbConnection GetConnection( Connection ndoConnection, Func<IDbConnection> factory ) |
| 103 | ········{ |
| 104 | ············var id = ndoConnection.ID; |
| 105 | ············if (this.usedConnections.ContainsKey( id )) |
| 106 | ············{ |
| 107 | return this. usedConnections[id]. Connection; |
| 108 | ············} |
| 109 | ············else |
| 110 | ············{ |
| 111 | ················var conn = factory(); |
| 112 | this. usedConnections. Add( id, new UsedConnectionsInfo { Connection = conn, Provider = ndoConnection. Provider } ) ; |
| 113 | ················if (this.isInTransaction) |
| 114 | OpenConnAndStartTransaction( id ) ; |
| 115 | ················return conn; |
| 116 | ············} |
| 117 | ········} |
| 118 | |
| 119 | |
| 120 | ········///<inheritdoc/> |
| 121 | ········public IDbTransaction GetTransaction( string id ) |
| 122 | ········{ |
| 123 | ············if (isInTransaction) |
| 124 | ············{ |
| 125 | ················IDbTransaction tx = null; |
| 126 | ················this.usedTransactions.TryGetValue( id, out tx ); |
| 127 | ················return tx; |
| 128 | ············} |
| 129 | |
| 130 | ············return null; |
| 131 | ········} |
| 132 | |
| 133 | ········///<inheritdoc/> |
| 134 | ········public void Dispose() |
| 135 | ········{ |
| 136 | ············RollbackTransactions(); |
| 137 | ············CloseConnections(); |
| 138 | ············this.isInTransaction = false; |
| 139 | ········} |
| 140 | |
| 141 | ········private void RollbackTransactions() |
| 142 | ········{ |
| 143 | ············foreach (var key in usedTransactions.Keys) |
| 144 | ············{ |
| 145 | ················var tx = this.usedTransactions[key]; |
| 146 | ················var id = tx.GetHashCode(); |
| 147 | ················try |
| 148 | ················{ |
| 149 | ····················// See https://github.com/dotnet/runtime/issues/95399 |
| 150 | ····················// We don't have any information about the state of the transaction. |
| 151 | ····················// If it is completed, we will get an exception here. |
| 152 | ····················// Given that in most cases it's possible to track the tx state outside of NDO, |
| 153 | ····················// we are safe here in the most cases. |
| 154 | ····················tx.Rollback(); |
| 155 | ················} |
| 156 | ················catch |
| 157 | ················{ |
| 158 | ················} |
| 159 | |
| 160 | if ( this. usedConnections. TryGetValue( key, out var cinfo ) ) |
| 161 | ················{ |
| 162 | var conn = cinfo. Connection; |
| 163 | ····················var provider = cinfo.Provider; |
| 164 | ····················var serverId = provider.GetConnectionId(conn); |
| 165 | ····················this.pm.LogIfVerbose( $"Rollback transaction {id.ToString( "X" )} at connection {serverId} = '{conn.DisplayName()}'" ); |
| 166 | ················} |
| 167 | ············} |
| 168 | |
| 169 | ············usedTransactions.Clear(); |
| 170 | ········} |
| 171 | |
| 172 | ········private void CloseConnections() |
| 173 | ········{ |
| 174 | foreach ( var cinfo in this. usedConnections. Values) |
| 175 | ············{ |
| 176 | var conn = cinfo. Connection; |
| 177 | ················if (conn.State != ConnectionState.Open) |
| 178 | ····················continue; |
| 179 | ················var serverId = cinfo.Provider.GetConnectionId(conn); |
| 180 | ················pm.LogIfVerbose( $"Closed connection {serverId} = '{conn.DisplayName()}'" ); |
| 181 | conn. Dispose( ) ; |
| 182 | ············} |
| 183 | |
| 184 | ············this.usedConnections.Clear(); |
| 185 | ········} |
| 186 | |
| 187 | ········/// <inheritdoc/> |
| 188 | ········public INDOTransactionScope Initialize( PersistenceManager pm ) |
| 189 | ········{ |
| 190 | ············this.pm = pm; |
| 191 | ············return this; |
| 192 | ········} |
| 193 | ····} |
| 194 | |
| 195 | ····static class ConnectionExtension |
| 196 | ····{ |
| 197 | ········public static string DisplayName(this IDbConnection conn) |
| 198 | ········{ |
| 199 | ············if (conn == null) |
| 200 | ················return "??"; |
| 201 | |
| 202 | ············var str = conn.ConnectionString; |
| 203 | ············if (String.IsNullOrEmpty( str )) |
| 204 | ················return str; |
| 205 | ············Regex regex = new Regex( "password=[^;]*" ); |
| 206 | ············return regex.Replace( str, "password=***" ); |
| 207 | ········} |
| 208 | ····} |
| 209 | } |
| 210 |