forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Clear pool for all Oracle drivers. #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
using System; | ||
using System.Data.Common; | ||
using System.Reflection; | ||
using NHibernate.Util; | ||
|
||
namespace NHibernate.Driver | ||
|
@@ -70,5 +71,58 @@ public override DbCommand CreateCommand() | |
{ | ||
return connectionCommandProvider.CreateCommand(); | ||
} | ||
|
||
/// <summary> | ||
/// Helper for clearing connection pools used by a reflection driver. Assumes the connection has a parameter-less | ||
/// <c>ClearAllPools</c> method and a <c>ClearPool</c> method taking as argument a connection. | ||
/// </summary> | ||
/// <typeparam name="T">The driver type for which the pool has to be cleared. This driver type must | ||
/// always use the same connection type.</typeparam> | ||
/// <remarks>Having <c>ClearAllPools</c> and <c>ClearPool</c> is a common pattern. <c>SqlConnection</c>, | ||
/// <c>OracleConnection</c> (managed, un-managed and from <c>System.Data</c>), <c>FirebirdConnection</c> | ||
/// <c>NpgsqlConnection</c>, <c>MySqlConnection</c> and <c>SQLiteConnection</c> have them. | ||
/// (<c>SqlCeConnection</c>, <c>OdbcConnection</c> and <c>OleDbConnection</c> lack them.)</remarks> | ||
protected static class PoolHelper<T> where T : IDriver | ||
{ | ||
// Static field in generic class => one field per concrete type used. This is exactly what | ||
// we need here, do not move the generic argument to the method. Otherwise it will cache the | ||
// method info of the first driver type used, and reuse it for other driver types, which | ||
// would fail. | ||
private static volatile MethodInfo _clearPool; | ||
private static volatile MethodInfo _clearAllPools; | ||
|
||
/// <summary> | ||
/// Clears the connection pool. | ||
/// </summary> | ||
/// <param name="driver">The driver for which the connection pool has to be cleared.</param> | ||
/// <param name="connectionString">The connection string of connections for which to clear the pool. | ||
/// <c>null</c> for clearing them all.</param> | ||
internal static void ClearPool(T driver, string connectionString) | ||
{ | ||
// In case of concurrent threads, may initialize many times. We do not care. | ||
// Members are volatile for avoiding they get used while their constructor is not yet ended. | ||
if (_clearPool == null || _clearAllPools == null) | ||
{ | ||
using (var clearConnection = driver.CreateConnection()) | ||
{ | ||
var connectionType = clearConnection.GetType(); | ||
_clearPool = connectionType.GetMethod("ClearPool") ?? throw new InvalidOperationException("Unable to resolve ClearPool method."); | ||
_clearAllPools = connectionType.GetMethod("ClearAllPools") ?? throw new InvalidOperationException("Unable to resolve ClearAllPools method."); | ||
} | ||
} | ||
|
||
if (connectionString != null) | ||
{ | ||
using (var clearConnection = driver.CreateConnection()) | ||
{ | ||
clearConnection.ConnectionString = connectionString; | ||
_clearPool.Invoke(null, new object[] {clearConnection}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we build a delegate instead of calling bare MethodInfo? |
||
} | ||
return; | ||
} | ||
|
||
_clearAllPools.Invoke(null, Array.Empty<object>()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be public, otherwise is not callable from out-of-core drivers