-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathConnectionWrapper.cs
35 lines (31 loc) · 955 Bytes
/
ConnectionWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using Microsoft.Data.SqlClient;
using System.Runtime.CompilerServices;
using System.Threading;
namespace SecurityDriven.TinyORM
{
internal sealed class ConnectionWrapper : IDisposable
{
public SqlConnection Connection;
int refCount = 1;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ConnectionWrapper(SqlConnection connection) { this.Connection = connection; }//ctor
#region IDisposable Members
/// <summary>Decrement the reference count and, if refcount is 0, close the underlying connection.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
int count = Interlocked.Add(ref refCount, -1);
if (count == 0)
{
this.Connection.Close();
}
}//Dispose()
#endregion
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void IncrementUseCount()
{
Interlocked.Add(ref refCount, +1);
}//IncrementUseCount()
}//class ConnectionWrapper
}//ns