Datei: NDODLL/NDODistributedTransactionScope.cs
Last Commit (4a50246)
| 1 | using NDO.Mapping; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Data; |
| 5 | using System.Linq; |
| 6 | using ST = System.Transactions; |
| 7 | |
| 8 | namespace NDO |
| 9 | { |
| 10 | ····/// <summary> |
| 11 | ····/// TransactionScope class which uses System.Transactions.TransactionScope to manage distributed transactions. |
| 12 | ····/// </summary> |
| 13 | ····/// <remarks> |
| 14 | ····/// Register this type for the interface INDOTransactionScope, if you know about the consequences. |
| 15 | ····/// See: https://docs.microsoft.com/en-us/dotnet/api/system.transactions.transactionscope |
| 16 | ····/// </remarks> |
| 17 | ····public class NDODistributedTransactionScope : INDOTransactionScope |
| 18 | ····{ |
| 19 | ········ST.TransactionScope innerScope; |
| 20 | ········private PersistenceManager pm; |
| 21 | |
| 22 | ········private Dictionary<string, IDbConnection> usedConnections = new Dictionary<string, IDbConnection>(); |
| 23 | |
| 24 | ········///<inheritdoc/> |
| 25 | ········public IsolationLevel IsolationLevel { get; set; } |
| 26 | ········///<inheritdoc/> |
| 27 | ········public TransactionMode TransactionMode { get; set; } |
| 28 | |
| 29 | ········/// <summary> |
| 30 | ········/// Constructs an NDOTransactionScope object. |
| 31 | ········/// </summary> |
| 32 | ········/// <param name="pm"></param> |
| 33 | ········public NDODistributedTransactionScope() |
| 34 | ········{ |
| 35 | ············IsolationLevel = IsolationLevel.ReadCommitted; |
| 36 | ············TransactionMode = TransactionMode.Optimistic; |
| 37 | ········} |
| 38 | |
| 39 | ········///<inheritdoc/> |
| 40 | ········public void CheckTransaction() |
| 41 | ········{ |
| 42 | ············if (TransactionMode == TransactionMode.None) |
| 43 | ············{ |
| 44 | ················innerScope = null; |
| 45 | ················return; |
| 46 | ············} |
| 47 | |
| 48 | ············if (innerScope == null) |
| 49 | ············{ |
| 50 | ················innerScope = new ST.TransactionScope( ST.TransactionScopeOption.Required, new ST.TransactionOptions() { IsolationLevel = (ST.IsolationLevel) Enum.Parse( typeof( ST.IsolationLevel ), this.IsolationLevel.ToString() ) } ); |
| 51 | ················this.pm.LogIfVerbose( "Creating a new TransactionScope" ); |
| 52 | ············} |
| 53 | ········} |
| 54 | |
| 55 | ········///<inheritdoc/> |
| 56 | ········public void Complete() |
| 57 | ········{ |
| 58 | ············if (innerScope != null) |
| 59 | ············{ |
| 60 | ················this.pm.LogIfVerbose( "Completing the TransactionScope" ); |
| 61 | ················innerScope.Complete(); |
| 62 | ················innerScope.Dispose(); |
| 63 | ············} |
| 64 | |
| 65 | ············CloseConnections(); |
| 66 | |
| 67 | ············innerScope = null; |
| 68 | ········} |
| 69 | |
| 70 | ········///<inheritdoc/> |
| 71 | ········public IDbConnection GetConnection( Connection ndoConnection, Func<IDbConnection> factory ) |
| 72 | ········{ |
| 73 | ············var id = ndoConnection.ID; |
| 74 | ············if (this.usedConnections.ContainsKey( id )) |
| 75 | ············{ |
| 76 | ················return this.usedConnections[id]; |
| 77 | ············} |
| 78 | ············else |
| 79 | ············{ |
| 80 | ················var conn = factory(); |
| 81 | ················this.usedConnections.Add( id, conn ); |
| 82 | ················return conn; |
| 83 | ············} |
| 84 | ········} |
| 85 | |
| 86 | ········///<inheritdoc/> |
| 87 | ········public void Dispose() |
| 88 | ········{ |
| 89 | ············if (innerScope != null) |
| 90 | ············{ |
| 91 | ················this.pm.LogIfVerbose( "Disposing the TransactionScope" ); |
| 92 | ················innerScope.Dispose(); |
| 93 | ················innerScope = null; |
| 94 | ············} |
| 95 | |
| 96 | ············CloseConnections(); |
| 97 | ········} |
| 98 | |
| 99 | ········private void CloseConnections() |
| 100 | ········{ |
| 101 | ············foreach (var conn in this.usedConnections.Values.Where( c => c.State == ConnectionState.Open )) |
| 102 | ············{ |
| 103 | ················conn.Close(); |
| 104 | ················pm.LogIfVerbose( $"Closed connection {new Connection( null ) { Name = conn.ConnectionString }.DisplayName }" ); |
| 105 | ············} |
| 106 | |
| 107 | ············this.usedConnections.Clear(); |
| 108 | ········} |
| 109 | |
| 110 | ········///<inheritdoc/> |
| 111 | ········public IDbTransaction GetTransaction( string id ) |
| 112 | ········{ |
| 113 | ············return null; |
| 114 | ········} |
| 115 | |
| 116 | ········/// <inheritdoc/> |
| 117 | ········public INDOTransactionScope Initialize( PersistenceManager pm ) |
| 118 | ········{ |
| 119 | ············this.pm = pm; |
| 120 | ············return this; |
| 121 | ········} |
| 122 | ····} |
| 123 | } |
| 124 |
New Commit (a4bc8a0)
| 1 | using NDO.Mapping; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Data; |
| 5 | using System.Linq; |
| 6 | using ST = System.Transactions; |
| 7 | |
| 8 | namespace NDO |
| 9 | { |
| 10 | ····/// <summary> |
| 11 | ····/// TransactionScope class which uses System.Transactions.TransactionScope to manage distributed transactions. |
| 12 | ····/// </summary> |
| 13 | ····/// <remarks> |
| 14 | ····/// Register this type for the interface INDOTransactionScope, if you know about the consequences. |
| 15 | ····/// See: https://docs.microsoft.com/en-us/dotnet/api/system.transactions.transactionscope |
| 16 | ····/// </remarks> |
| 17 | ····public class NDODistributedTransactionScope : INDOTransactionScope |
| 18 | ····{ |
| 19 | ········ST.TransactionScope innerScope; |
| 20 | ········private PersistenceManager pm; |
| 21 | |
| 22 | ········private Dictionary<string, IDbConnection> usedConnections = new Dictionary<string, IDbConnection>(); |
| 23 | |
| 24 | ········///<inheritdoc/> |
| 25 | ········public IsolationLevel IsolationLevel { get; set; } |
| 26 | ········///<inheritdoc/> |
| 27 | ········public TransactionMode TransactionMode { get; set; } |
| 28 | |
| 29 | ········/// <summary> |
| 30 | ········/// Constructs an NDOTransactionScope object. |
| 31 | ········/// </summary> |
| 32 | ········public NDODistributedTransactionScope() |
| 33 | ········{ |
| 34 | ············IsolationLevel = IsolationLevel.ReadCommitted; |
| 35 | ············TransactionMode = TransactionMode.Optimistic; |
| 36 | ········} |
| 37 | |
| 38 | ········///<inheritdoc/> |
| 39 | ········public void CheckTransaction() |
| 40 | ········{ |
| 41 | ············if (TransactionMode == TransactionMode.None) |
| 42 | ············{ |
| 43 | ················innerScope = null; |
| 44 | ················return; |
| 45 | ············} |
| 46 | |
| 47 | ············if (innerScope == null) |
| 48 | ············{ |
| 49 | ················innerScope = new ST.TransactionScope( ST.TransactionScopeOption.Required, new ST.TransactionOptions() { IsolationLevel = (ST.IsolationLevel) Enum.Parse( typeof( ST.IsolationLevel ), this.IsolationLevel.ToString() ) } ); |
| 50 | ················this.pm.LogIfVerbose( "Creating a new TransactionScope" ); |
| 51 | ············} |
| 52 | ········} |
| 53 | |
| 54 | ········///<inheritdoc/> |
| 55 | ········public void Complete() |
| 56 | ········{ |
| 57 | ············if (innerScope != null) |
| 58 | ············{ |
| 59 | ················this.pm.LogIfVerbose( "Completing the TransactionScope" ); |
| 60 | ················innerScope.Complete(); |
| 61 | ················innerScope.Dispose(); |
| 62 | ············} |
| 63 | |
| 64 | ············CloseConnections(); |
| 65 | |
| 66 | ············innerScope = null; |
| 67 | ········} |
| 68 | |
| 69 | ········///<inheritdoc/> |
| 70 | ········public IDbConnection GetConnection( Connection ndoConnection, Func<IDbConnection> factory ) |
| 71 | ········{ |
| 72 | ············var id = ndoConnection.ID; |
| 73 | ············if (this.usedConnections.ContainsKey( id )) |
| 74 | ············{ |
| 75 | ················return this.usedConnections[id]; |
| 76 | ············} |
| 77 | ············else |
| 78 | ············{ |
| 79 | ················var conn = factory(); |
| 80 | ················this.usedConnections.Add( id, conn ); |
| 81 | ················return conn; |
| 82 | ············} |
| 83 | ········} |
| 84 | |
| 85 | ········///<inheritdoc/> |
| 86 | ········public void Dispose() |
| 87 | ········{ |
| 88 | ············if (innerScope != null) |
| 89 | ············{ |
| 90 | ················this.pm.LogIfVerbose( "Disposing the TransactionScope" ); |
| 91 | ················innerScope.Dispose(); |
| 92 | ················innerScope = null; |
| 93 | ············} |
| 94 | |
| 95 | ············CloseConnections(); |
| 96 | ········} |
| 97 | |
| 98 | ········private void CloseConnections() |
| 99 | ········{ |
| 100 | ············foreach (var conn in this.usedConnections.Values.Where( c => c.State == ConnectionState.Open )) |
| 101 | ············{ |
| 102 | ················conn.Close(); |
| 103 | ················pm.LogIfVerbose( $"Closed connection {new Connection( null ) { Name = conn.ConnectionString }.DisplayName }" ); |
| 104 | ············} |
| 105 | |
| 106 | ············this.usedConnections.Clear(); |
| 107 | ········} |
| 108 | |
| 109 | ········///<inheritdoc/> |
| 110 | ········public IDbTransaction GetTransaction( string id ) |
| 111 | ········{ |
| 112 | ············return null; |
| 113 | ········} |
| 114 | |
| 115 | ········/// <inheritdoc/> |
| 116 | ········public INDOTransactionScope Initialize( PersistenceManager pm ) |
| 117 | ········{ |
| 118 | ············this.pm = pm; |
| 119 | ············return this; |
| 120 | ········} |
| 121 | ····} |
| 122 | } |
| 123 |