CSLA.NET 5.4.2
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 public class TransactionManager<C, T> : IDisposable
38 where C : IDbConnection, new()
39 where T : IDbTransaction
40 {
41 private static object _lock = new object();
42 private C _connection;
43 private T _transaction;
44 private string _connectionString;
45 private string _label;
46
54 public static TransactionManager<C, T> GetManager(string database)
55 {
56 return GetManager(database, true);
57 }
58
67 public static TransactionManager<C, T> GetManager(string database, string label)
68 {
69 return GetManager(database, true, label);
70 }
71
86 public static TransactionManager<C, T> GetManager(string database, bool isDatabaseName)
87 {
88 return GetManager(database, isDatabaseName, "default");
89 }
90
106 public static TransactionManager<C, T> GetManager(string database, bool isDatabaseName, string label)
107 {
108 if (isDatabaseName)
109 {
110#if NETSTANDARD2_0 || NET5_0
111 throw new NotSupportedException("isDatabaseName==true");
112#else
113 var connection = ConfigurationManager.ConnectionStrings[database];
114 if (connection == null)
115 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
116
117 var conn = ConfigurationManager.ConnectionStrings[database].ConnectionString;
118 if (string.IsNullOrEmpty(conn))
119 throw new System.Configuration.ConfigurationErrorsException(String.Format(Resources.DatabaseNameNotFound, database));
120 database = conn;
121#endif
122 }
123
124 lock (_lock)
125 {
126 var contextLabel = GetContextName(database, label);
127 TransactionManager<C, T> mgr = null;
128 if (ApplicationContext.LocalContext.Contains(contextLabel))
129 {
130 mgr = (TransactionManager<C, T>)(ApplicationContext.LocalContext[contextLabel]);
131
132 }
133 else
134 {
135 mgr = new TransactionManager<C, T>(database, label);
136 ApplicationContext.LocalContext[contextLabel] = mgr;
137 }
138 mgr.AddRef();
139 return mgr;
140 }
141 }
142
143 private TransactionManager(string connectionString, string label)
144 {
145 _label = label;
146 _connectionString = connectionString;
147
148 // create and open connection
149 _connection = new C();
150 _connection.ConnectionString = connectionString;
151 _connection.Open();
152 //start transaction
153 _transaction = (T)_connection.BeginTransaction();
154
155 }
156
157 private static string GetContextName(string connectionString, string label)
158 {
159 return "__transaction:" + label + "-" + connectionString;
160 }
161
166 public T Transaction
167 {
168 get
169 {
170 return _transaction;
171 }
172 }
173
179 public C Connection
180 {
181 get
182 {
183 return _connection;
184 }
185 }
186
187 private bool _commit = false;
188
196 public void Commit()
197 {
198 if (RefCount == 1)
199 _commit = true;
200 }
201
202#region Reference counting
203
204 private int _refCount;
205
210 public int RefCount
211 {
212 get { return _refCount; }
213 }
214
215 private void AddRef()
216 {
217 _refCount += 1;
218 }
219
220 private void DeRef()
221 {
222 lock (_lock)
223 {
224 _refCount -= 1;
225 if (_refCount == 0)
226 {
227 try
228 {
229 if (_commit)
230 _transaction.Commit();
231 else
232 _transaction.Rollback();
233 }
234 finally
235 {
236 _transaction.Dispose();
237 _connection.Dispose();
238 ApplicationContext.LocalContext.Remove(GetContextName(_connectionString, _label));
239 }
240 }
241 }
242
243 }
244
245#endregion
246
247#region IDisposable
248
254 public void Dispose()
255 {
256 DeRef();
257 }
258
259#endregion
260 }
261}
262#endif
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.
static TransactionManager< C, T > GetManager(string database)
Gets the TransactionManager object for the specified database.
T Transaction
Gets a reference to the current ADO.NET transaction object.
static TransactionManager< C, T > GetManager(string database, string label)
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.
static TransactionManager< C, T > GetManager(string database, bool isDatabaseName, string label)
Gets the TransactionManager object for the specified database.
static TransactionManager< C, T > GetManager(string database, bool isDatabaseName)
Gets the TransactionManager object for the specified database.
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}).