源代码:
public List<string> GetData (List<long> Id)
{
List<string> data;
string sql = "select * from tblSample with(nolock) where SampleId in @sampleId";
Dapper.DynamicParameters param = new Dapper.DynamicParameters();
param.Add("@sampleId", Id);
try
{
data = this.queryrunner.QueryList(sql, param);
}
catch (Exception ex)
{
logger.Error(ex.Message, ex);
}
return data;
}
QueryRunner类的方法
private void OpenConnection()
{
if (this.conn.State != ConnectionState.Open)
{
this.conn.Open();
}
}
public List<t> QueryList(String sql, DynamicParameters param)
{
List<t> t;
try
{
OpenConnection();
t = this.conn.Query<t>(sql, param).ToList();
}
catch (System.Data.SqlClient.SqlException ex)
{
logger.Debug(ex.Message, ex);
LogSqlErrorException(this.conn.Database, sql, param);
throw;
}
finally
{
CloseConnection();
}
return t;
}
以下是部分错误日志:
该错误日志显示当前的 TransactionScope 已经完成,导致连接池无法获取连接。可能的原因是在事务范围内使用了多个数据库连接,或者没有正确地释放连接。请检查代码并确保在使用完连接后正确地关闭和释放它们。System.InvalidOperationException: 当前的 TransactionScope 已经完成。
at System.Transactions.Transaction.get_Current()
at System.Data.ProviderBase.DbConnectionPool.GetFromTransactedPool(Transaction& transaction)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)
1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
at System.Data.SqlClient.SqlConnection.Open()
this.queryrunner.QueryList(sql, param);
>OpenConnection();
>this.conn.Open();
时抛出异常。 - JackyLoo