不久之前,我提出了一个关于TransactionScope升级到MSDTC的问题,而我并没有期望它会这样做。 (以前的问题)
问题的实质是,在SQL2005中,为了使用TransactionScope,您只能在TransactionScope的生命周期内创建和打开单个SqlConnection。在SQL2008中,您可以创建多个SqlConnection,但在任何给定时间只能打开一个。 SQL2000将始终升级到DTC……我们的应用程序不支持SQL2000,这是一个WinForms应用程序,BTW。
我们解决单连接问题的方法是创建一个TransactionScope帮助程序类,称为LocalTransactionScope (aka 'LTS')。它包装一个TransactionScope,并且最重要的是为我们的应用程序创建和维护一个单独的SqlConnection实例。好消息是,它有效 - 我们可以在不同的代码片段中使用LTS并且它们都加入了环境事务。非常好。问题是,每个根 LTS实例都将从连接池创建并且有效地杀死一个连接。所谓的“有效杀死”是指它将实例化一个SqlConnetion,该连接将打开一个新的连接(出于某种原因,它从未重用来自池的连接),并且当该根LTS被处理时,它将关闭和处理SqlConnection,这应该释放连接以便可以重新使用,但是显然从未被重用。池膨胀直到达到最大值,然后当建立一个max-pool-size+1连接时,应用程序失败。
下面我附上了LTS代码的简化版本和一个演示连接池耗尽的样例控制台应用程序类。为了观察连接池膨胀,请使用SQL Server Managment Studio的“Activity Monitor”或此查询:
SELECT DB_NAME(dbid) as 'DB Name',
COUNT(dbid) as 'Connections'
FROM sys.sysprocesses WITH (nolock)
WHERE dbid > 0
GROUP BY dbid
我在这里附上LTS和一个示例控制台应用程序,您可以使用它来证明它会从池中消耗连接,而不会重复使用或释放它们。您需要添加对System.Transactions.dll的引用以使LTS编译。
需要注意的是:根级别的LTS打开和关闭SqlConnection,总是在池中打开新连接。嵌套的LTS实例没有任何区别,因为只有根LTS实例建立SqlConnection。如您所见,连接字符串始终相同,因此它应该重新使用连接。
是否存在我们未满足的神秘条件导致连接无法被重复使用?除了完全关闭池外,是否有其他解决方案?
public sealed class LocalTransactionScope : IDisposable
{
private static SqlConnection _Connection;
private TransactionScope _TransactionScope;
private bool _IsNested;
public LocalTransactionScope(string connectionString)
{
// stripped out a few cases that need to throw an exception
_TransactionScope = new TransactionScope();
// we'll use this later in Dispose(...) to determine whether this LTS instance should close the connection.
_IsNested = (_Connection != null);
if (_Connection == null)
{
_Connection = new SqlConnection(connectionString);
// This Has Code-Stink. You want to open your connections as late as possible and hold them open for as little
// time as possible. However, in order to use TransactionScope with SQL2005 you can only have a single
// connection, and it can only be opened once within the scope of the entire TransactionScope. If you have
// more than one SqlConnection, or you open a SqlConnection, close it, and re-open it, it more than once,
// the TransactionScope will escalate to the MSDTC. SQL2008 allows you to have multiple connections within a
// single TransactionScope, however you can only have a single one open at any given time.
// Lastly, let's not forget about SQL2000. Using TransactionScope with SQL2000 will immediately and always escalate to DTC.
// We've dropped support of SQL2000, so that's not a concern we have.
_Connection.Open();
}
}
/// <summary>'Completes' the <see cref="TransactionScope"/> this <see cref="LocalTransactionScope"/> encapsulates.</summary>
public void Complete() { _TransactionScope.Complete(); }
/// <summary>Creates a new <see cref="SqlCommand"/> from the current <see cref="SqlConnection"/> this <see cref="LocalTransactionScope"/> is managing.</summary>
public SqlCommand CreateCommand() { return _Connection.CreateCommand(); }
void IDisposable.Dispose() { this.Dispose(); }
public void Dispose()
{
Dispose(true); GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
_TransactionScope.Dispose();
_TransactionScope = null;
if (!_IsNested)
{
// last one out closes the door, this would be the root LTS, the first one to be instanced.
LocalTransactionScope._Connection.Close();
LocalTransactionScope._Connection.Dispose();
LocalTransactionScope._Connection = null;
}
}
}
}
这是一个展示连接池耗尽的 Program.cs 文件:
class Program
{
static void Main(string[] args)
{
// fill in your connection string, but don't monkey with any pooling settings, like
// "Pooling=false;" or the "Max Pool Size" stuff. Doesn't matter if you use
// Doesn't matter if you use Windows or SQL auth, just make sure you set a Data Soure and an Initial Catalog
string connectionString = "your connection string here";
List<string> randomTables = new List<string>();
using (var nonLTSConnection = new SqlConnection(connectionString))
using (var command = nonLTSConnection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = @"SELECT [TABLE_NAME], NEWID() AS [ID]
FROM [INFORMATION_SCHEMA].TABLES]
WHERE [TABLE_SCHEMA] = 'dbo' and [TABLE_TYPE] = 'BASE TABLE'
ORDER BY [ID]";
nonLTSConnection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
string table = (string)reader["TABLE_NAME"];
randomTables.Add(table);
if (randomTables.Count > 200) { break; } // got more than enough to test.
}
}
nonLTSConnection.Close();
}
// we're going to assume your database had some tables.
for (int j = 0; j < 200; j++)
{
// At j = 100 you'll see it pause, and you'll shortly get an InvalidOperationException with the text of:
// "Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.
// This may have occurred because all pooled connections were in use and max pool size was reached."
string tableName = randomTables[j % randomTables.Count];
Console.Write("Creating root-level LTS " + j.ToString() + " selecting from " + tableName);
using (var scope = new LocalTransactionScope(connectionString))
using (var command = scope.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = "SELECT TOP 20 * FROM [" + tableName + "]";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.Write(".");
}
Console.Write(Environment.NewLine);
}
}
Thread.Sleep(50);
scope.Complete();
}
Console.ReadKey();
}
}