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