Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -382,42 +382,6 @@ internal WaitHandle[] GetHandles(bool withCreate)
return withCreate ? _handlesWithCreate : _handlesWithoutCreate;
}
}

/// <summary>
/// Helper class to obtain and release a semaphore.
/// </summary>
internal class SemaphoreHolder : IDisposable
{
private readonly Semaphore _semaphore;

/// <summary>
/// Whether the semaphore was successfully obtained within the timeout.
/// </summary>
internal bool Obtained { get; private set; }

/// <summary>
/// Obtains the semaphore, waiting up to the specified timeout.
/// </summary>
/// <param name="semaphore"></param>
/// <param name="timeout"></param>
internal SemaphoreHolder(Semaphore semaphore, int timeout)
{
_semaphore = semaphore;
Obtained = _semaphore.WaitOne(timeout);
}

/// <summary>
/// Releases the semaphore if it was successfully obtained.
/// </summary>
public void Dispose()
{
if (Obtained)
{
_semaphore.Release(1);
}
}
}

private const int MAX_Q_SIZE = 0x00100000;

// The order of these is important; we want the WaitAny call to be signaled
Expand Down Expand Up @@ -1356,26 +1320,36 @@ private bool TryGetConnection(DbConnection owningObject, uint waitForMultipleObj

if (onlyOneCheckConnection)
{
using SemaphoreHolder semaphoreHolder = new(_waitHandles.CreationSemaphore, unchecked((int)waitForMultipleObjectsTimeout));
if (semaphoreHolder.Obtained)
{
bool obtained = false;
#if NETFRAMEWORK
RuntimeHelpers.PrepareConstrainedRegions();
RuntimeHelpers.PrepareConstrainedRegions();
#endif
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.GetConnection|RES|CPOOL> {0}, Creating new connection.", Id);
obj = UserCreateRequest(owningObject, userOptions);
try
{
obtained = _waitHandles.CreationSemaphore.WaitOne(unchecked((int)waitForMultipleObjectsTimeout));
if (obtained)
{
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.GetConnection|RES|CPOOL> {0}, Creating new connection.", Id);
obj = UserCreateRequest(owningObject, userOptions);
}
else
{
// Timeout waiting for creation semaphore - return null
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.GetConnection|RES|CPOOL> {0}, Wait timed out.", Id);
connection = null;
return false;
}
}
else
finally
{
// Timeout waiting for creation semaphore - return null
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.GetConnection|RES|CPOOL> {0}, Wait timed out.", Id);
connection = null;
return false;
if (obtained)
{
_waitHandles.CreationSemaphore.Release(1);
}
}
}
}
break;

case WAIT_ABANDONED + SEMAPHORE_HANDLE:
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.GetConnection|RES|CPOOL> {0}, Semaphore handle abandonded.", Id);
Interlocked.Decrement(ref _waitCount);
Expand Down Expand Up @@ -1582,20 +1556,17 @@ private void PoolCreateRequest(object state)
{
return;
}

#if NETFRAMEWORK
RuntimeHelpers.PrepareConstrainedRegions();
#endif
bool obtained = false;
try
{
// Obtain creation mutex so we're the only one creating objects
using SemaphoreHolder semaphoreHolder = new(_waitHandles.CreationSemaphore, CreationTimeout);
obtained = _waitHandles.CreationSemaphore.WaitOne(CreationTimeout);

if (semaphoreHolder.Obtained)
if (obtained)
{
#if NETFRAMEWORK
RuntimeHelpers.PrepareConstrainedRegions();
#endif
DbConnectionInternal newObj;

// Check ErrorOccurred again after obtaining mutex
Expand Down Expand Up @@ -1648,6 +1619,13 @@ private void PoolCreateRequest(object state)
// thrown to the user the next time they request a connection.
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionPool.PoolCreateRequest|RES|CPOOL> {0}, PoolCreateRequest called CreateConnection which threw an exception: {1}", Id, e);
}
finally
{
if (obtained)
{
_waitHandles.CreationSemaphore.Release(1);
}
}
}
}
}
Expand Down
Loading