CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
TransactionManager.cs
Go to the documentation of this file.
1#if !NETFX_CORE && !(ANDROID || IOS)
2//-----------------------------------------------------------------------
3// <copyright file="TransactionManager.cs" company="Marimer LLC">
4// Copyright (c) Marimer LLC. All rights reserved.
5// Website: https://cslanet.com
6// </copyright>
7// <summary>Provides an automated way to reuse open</summary>
8//-----------------------------------------------------------------------
9using System;
11using System.Data;
12using Csla.Properties;
13
14namespace Csla.Data
15{
37 [Obsolete("Use dependency injection", false)]
38 public class TransactionManager<C, T> : IDisposable, Core.IUseApplicationContext
39 where C : IDbConnection, new()
40 where T : IDbTransaction
41 {
42 private static object _lock = new object();
43 private C _connection;
44 private T _transaction;
45 private string _connectionString;
46 private string _label;
47
48 ApplicationContext Core.IUseApplicationContext.ApplicationContext { get => ApplicationContext; set => ApplicationContext = value; }
49 private ApplicationContext ApplicationContext { get; set; }
50
58 public TransactionManager<C, T> GetManager(string database)
59 {
60 return GetManager(database, true);
61 }
62
71 public TransactionManager<C, T> GetManager(string database, string label)
72 {
73 return GetManager(database, true, label);
74 }
75
90 public TransactionManager<C, T> GetManager(string database, bool isDatabaseName)
91 {
92 return GetManager(database, isDatabaseName, "default");
93 }
94
110 public TransactionManager<C, T> GetManager(string database, bool isDatabaseName, string label)
111 {
112 if (isDatabaseName)
113 {
114#if NETSTANDARD2_0 || NET5_0 || NET6_0
115 throw new NotSupportedException("isDatabaseName==true");
116#else
117 var connection = ConfigurationManager.ConnectionStrings[database];
118 if (connection == null)
119 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
120
121 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
122 if (string.IsNullOrEmpty(conn))
123 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
124 database = conn;
125#endif
126 }
127
128 lock (_lock)
129 {
130 var contextLabel = GetContextName(database, label);
131 TransactionManager<C, T> mgr = null;
132 if (ApplicationContext.LocalContext.Contains(contextLabel))
133 {
135
136 }
137 else
138 {
139 mgr = new TransactionManager<C, T>(database, label);
140 ApplicationContext.LocalContext[contextLabel] = mgr;
141 }
142 mgr.AddRef();
143 return mgr;
144 }
145 }
146
147 private TransactionManager(string connectionString, string label)
148 {
149 _label = label;
150 _connectionString = connectionString;
151
152 // create and open connection
153 _connection = new C();
154 _connection.ConnectionString = connectionString;
155 _connection.Open();
156 //start transaction
157 _transaction = (T)_connection.BeginTransaction();
158
159 }
160
161 private static string GetContextName(string connectionString, string label)
162 {
163 return "__transaction:" + label + "-" + connectionString;
164 }
165
170 public T Transaction
171 {
172 get
173 {
174 return _transaction;
175 }
176 }
177
183 public C Connection
184 {
185 get
186 {
187 return _connection;
188 }
189 }
190
191 private bool _commit = false;
192
200 public void Commit()
201 {
202 if (RefCount == 1)
203 _commit = true;
204 }
205
206#region Reference counting
207
208 private int _refCount;
209
214 public int RefCount
215 {
216 get { return _refCount; }
217 }
218
219 private void AddRef()
220 {
221 _refCount += 1;
222 }
223
224 private void DeRef()
225 {
226 lock (_lock)
227 {
228 _refCount -= 1;
229 if (_refCount == 0)
230 {
231 try
232 {
233 if (_commit)
234 _transaction.Commit();
235 else
236 _transaction.Rollback();
237 }
238 finally
239 {
240 _transaction.Dispose();
241 _connection.Dispose();
242 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
243 }
244 }
245 }
246
247 }
248
249#endregion
250
251#region IDisposable
252
258 public void Dispose()
259 {
260 DeRef();
261 }
262
263#endregion
264 }
265}
266#endif
Provides consistent context information between the client and server DataPortal objects.
ApplicationContext(ApplicationContextAccessor applicationContextAccessor)
Creates a new instance of the type
ContextDictionary LocalContext
Returns the application-specific context data that is local to the current AppDomain.
Provides an automated way to reuse open database connections and associated ADO.NET transactions with...
void Commit()
Indicates that the current transactional scope has completed successfully.
int RefCount
Gets the current reference count for this object.
TransactionManager< C, T > GetManager(string database, bool isDatabaseName)
Gets the TransactionManager object for the specified database.
TransactionManager< C, T > GetManager(string database, bool isDatabaseName, string label)
Gets the TransactionManager object for the specified database.
T Transaction
Gets a reference to the current ADO.NET transaction object.
TransactionManager< C, T > GetManager(string database, string label)
Gets the TransactionManager object for the specified database.
TransactionManager< C, T > GetManager(string database)
Gets the TransactionManager object for the specified database.
C Connection
Gets a reference to the current ADO.NET connection object that is associated with current trasnaction...
void Dispose()
Dispose object, dereferencing or disposing the connection it is managing.
A strongly-typed resource class, for looking up localized strings, etc.
static string DatabaseNameNotFound
Looks up a localized string similar to Database name not found in config file ({0}).