Skip to content

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 1 commit into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
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
18 changes: 12 additions & 6 deletions src/NHibernate.Test/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System.Text;
using NHibernate.Dialect;
using NHibernate.Driver;
using Oracle.ManagedDataAccess.Client;

namespace NHibernate.Test
{
Expand Down Expand Up @@ -314,16 +312,24 @@ protected virtual DebugSessionFactory BuildSessionFactory()

private void Cleanup()
{
Sfi?.Close();

// Clear connection pool for Oracle to avoid problem that was manifested with https://github.com/nhibernate/nhibernate-core/pull/1517:
// As it seems Oracle can cache returned types for query for given connection.
// So exception can be thrown if two tests execute same query but with different types in result (like for Entity.Id int and Entity.Id Guid)
if (Dialect is Oracle8iDialect)
switch (Sfi?.ConnectionProvider.Driver)
{
OracleConnection.ClearAllPools();
case OracleClientDriver oraSysData:
oraSysData.ClearPool(null);
break;
case OracleDataClientDriver oraUnmanaged:
oraUnmanaged.ClearPool(null);
break;
case OracleManagedDataClientDriver oraManaged:
oraManaged.ClearPool(null);
break;
}

Sfi?.Close();

_sessionFactory = null;
cfg = null;
}
Expand Down
31 changes: 3 additions & 28 deletions src/NHibernate/Driver/FirebirdClientDriver.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using NHibernate.Dialect;
using NHibernate.SqlCommand;
Expand Down Expand Up @@ -128,39 +126,16 @@ private string GetFbTypeForParam(SqlType sqlType)
return _fbDialect.GetCastTypeName(sqlType);
}

private static volatile MethodInfo _clearPool;
private static volatile MethodInfo _clearAllPools;

/// <summary>
/// Clears the connection pool.
/// </summary>
/// <param name="connectionString">The connection string of connections for which to clear the pool.
/// <c>null</c> for clearing them all.</param>
public void ClearPool(string connectionString)
{
// In case of concurrent threads, may initialize many times. We do not care.
// Members are volatile for avoiding it gets used while its constructor is not yet ended.
if (_clearPool == null || _clearAllPools == null)
{
using (var clearConnection = 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 = CreateConnection())
{
clearConnection.ConnectionString = connectionString;
_clearPool.Invoke(null, new object[] {clearConnection});
}
return;
}

_clearAllPools.Invoke(null, Array.Empty<object>());
// Do not move in a base class common to different connection types, or it may not clear
// expected pool.
PoolHelper<FirebirdClientDriver>.ClearPool(this, connectionString);
}

/// <summary>
Expand Down
14 changes: 13 additions & 1 deletion src/NHibernate/Driver/OracleClientDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,17 @@ protected override void OnBeforePrepare(DbCommand command)
" does not support CallableStatement syntax (stored procedures)." +
" Consider using OracleDataClientDriver instead.");
}

/// <summary>
/// Clears the connection pool.
/// </summary>
/// <param name="connectionString">The connection string of connections for which to clear the pool.
/// <c>null</c> for clearing them all.</param>
public void ClearPool(string connectionString)
{
// Do not move in a base class common to different connection types, or it may not clear
// expected pool.
PoolHelper<OracleClientDriver>.ClearPool(this, connectionString);
}
}
}
}
14 changes: 13 additions & 1 deletion src/NHibernate/Driver/OracleDataClientDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@ public OracleDataClientDriver()
: base("Oracle.DataAccess")
{
}

/// <summary>
/// Clears the connection pool.
/// </summary>
/// <param name="connectionString">The connection string of connections for which to clear the pool.
/// <c>null</c> for clearing them all.</param>
public void ClearPool(string connectionString)
{
// Do not move in a base class common to different connection types, or it may not clear
// expected pool.
PoolHelper<OracleDataClientDriver>.ClearPool(this, connectionString);
}
}
}
}
1 change: 0 additions & 1 deletion src/NHibernate/Driver/OracleDataClientDriverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Reflection;
using NHibernate.AdoNet;
using NHibernate.Engine.Query;
using NHibernate.SqlTypes;
Expand Down
12 changes: 12 additions & 0 deletions src/NHibernate/Driver/OracleManagedDataClientDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@ public OracleManagedDataClientDriver()
}

public override bool HasDelayedDistributedTransactionCompletion => true;

/// <summary>
/// Clears the connection pool.
/// </summary>
/// <param name="connectionString">The connection string of connections for which to clear the pool.
/// <c>null</c> for clearing them all.</param>
public void ClearPool(string connectionString)
{
// Do not move in a base class common to different connection types, or it may not clear
// expected pool.
PoolHelper<OracleManagedDataClientDriver>.ClearPool(this, connectionString);
}
}
}
56 changes: 55 additions & 1 deletion src/NHibernate/Driver/ReflectionBasedDriver.cs
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
Expand Down Expand Up @@ -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)
Copy link

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

{
// 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});
Copy link

Choose a reason for hiding this comment

The 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>());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

}
}
}
}
}