Skip to content

NH-3724 - Added support for notification handlers #2113

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions src/NHibernate.Test/CfgTest/Loquacious/ConfigurationFixture.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
Expand All @@ -11,12 +12,26 @@
using NHibernate.Hql.Ast.ANTLR;
using NHibernate.Type;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.CfgTest.Loquacious
{
[TestFixture]
public class ConfigurationFixture
{
[Test]
public void CanSetNotificationHandler()
{
//NH-3724
EventHandler<SqlInfoMessageEventArgs> handler = (s, e) => {};
var cfg = new Configuration().DataBaseIntegration(x =>
{
x.WithNotificationHandler(handler);
}).Configure();

Assert.IsNotNull(cfg.NotificationHandler);
}

[Test]
public void CompleteConfiguration()
{
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1547/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ void IDriver.Configure(IDictionary<string, string> settings)
_driverImplementation.Configure(settings);
}

public void AddNotificationHandler(DbConnection con, Delegate handler)
{
_driverImplementation.AddNotificationHandler(con, handler);
}

DbConnection IDriver.CreateConnection()
{
return _driverImplementation.CreateConnection();
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate.Test/TypesTest/AbstractDateTimeTypeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,11 @@ void IDriver.Configure(IDictionary<string, string> settings)
_driverImplementation.Configure(settings);
}

public void AddNotificationHandler(DbConnection con, Delegate handler)
{
_driverImplementation.AddNotificationHandler(con, handler);
}

DbConnection IDriver.CreateConnection()
{
return _driverImplementation.CreateConnection();
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/AdoNet/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public DbConnection GetConnection()
if (_ownConnection)
{
_connection = Factory.ConnectionProvider.GetConnection();
//NH-3724
if (Factory.Settings.NotificationHandler != null)
{
Factory.ConnectionProvider.Driver.AddNotificationHandler(_connection, Factory.Settings.NotificationHandler);
}
// Will fail if the connection is already enlisted in another transaction.
// Probable case: nested transaction scope with connection auto-enlistment enabled.
// That is an user error.
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/Async/AdoNet/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ async Task<DbConnection> InternalGetConnectionAsync()
if (_ownConnection)
{
_connection = await (Factory.ConnectionProvider.GetConnectionAsync(cancellationToken)).ConfigureAwait(false);
//NH-3724
if (Factory.Settings.NotificationHandler != null)
{
Factory.ConnectionProvider.Driver.AddNotificationHandler(_connection, Factory.Settings.NotificationHandler);
}
// Will fail if the connection is already enlisted in another transaction.
// Probable case: nested transaction scope with connection auto-enlistment enabled.
// That is an user error.
Expand Down
14 changes: 14 additions & 0 deletions src/NHibernate/Cfg/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ protected void Reset()
tableNameBinding = new Dictionary<string, Mappings.TableDescription>();
columnNameBindingPerTable = new Dictionary<Table, Mappings.ColumnNames>();
filtersSecondPasses = new Queue<FilterSecondPassArgs>();
NotificationHandler = null;
}

[Serializable]
Expand Down Expand Up @@ -1405,6 +1406,14 @@ public Configuration SetDefaultNamespace(string newDefaultNamespace)
return this;
}

//NH-3724

public Delegate NotificationHandler
{
get => _notificationHandler;
set => _notificationHandler = value;
}

/// <summary>
/// Sets the default interceptor for use by all sessions.
/// </summary>
Expand Down Expand Up @@ -1763,6 +1772,8 @@ private Settings BuildSettings()
// NH : Set configuration for IdGenerator SQL logging
PersistentIdGeneratorParmsNames.SqlStatementLogger.FormatSql = result.SqlStatementLogger.FormatSql;
PersistentIdGeneratorParmsNames.SqlStatementLogger.LogToStdout = result.SqlStatementLogger.LogToStdout;
//NH-3724
result.NotificationHandler = NotificationHandler;
return result;
}

Expand Down Expand Up @@ -1928,6 +1939,9 @@ protected virtual string GetDefaultConfigurationFilePath()
#endregion

private XmlSchemas schemas;

[NonSerialized]
private Delegate _notificationHandler;

private XmlSchemas Schemas
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using NHibernate.AdoNet;
using NHibernate.Connection;
Expand All @@ -19,6 +20,12 @@ public DbIntegrationConfigurationProperties(Configuration configuration)

#region Implementation of IDbIntegrationConfigurationProperties

public void WithNotificationHandler(Delegate handler)
{
//NH-3724
configuration.NotificationHandler = handler;
}

public void Dialect<TDialect>() where TDialect : Dialect.Dialect
{
configuration.SetProperty(Environment.Dialect, typeof(TDialect).AssemblyQualifiedName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using NHibernate.AdoNet;
using NHibernate.Connection;
Expand All @@ -15,6 +16,9 @@ public interface IDbIntegrationConfigurationProperties
bool LogSqlInConsole { set; }
bool LogFormattedSql { set; }

//NH-3724
void WithNotificationHandler(Delegate handler);

void ConnectionProvider<TProvider>() where TProvider : IConnectionProvider;
void Driver<TDriver>() where TDriver : IDriver;
IsolationLevel IsolationLevel { set; }
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Cfg/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public Settings()
//private int jdbcFetchSize;

#endregion

public Delegate NotificationHandler { get; internal set; }

public SqlStatementLogger SqlStatementLogger { get; internal set; }

public int MaximumFetchDepth { get; internal set; }
Expand Down
16 changes: 16 additions & 0 deletions src/NHibernate/Driver/DriverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ public abstract class DriverBase : IDriver, ISqlParameterFormatter
private int commandTimeout;
private bool prepareSql;

public virtual void AddNotificationHandler(DbConnection con, Delegate handler)
{
//NH-3724
if (handler != null)
{
var prop = con.GetType().GetEvent("InfoMessage");

if (prop == null)
{
throw new NotSupportedException("Current driver does not support notifications.");
}

prop.AddEventHandler(con, handler);
}
}

public virtual void Configure(IDictionary<string, string> settings)
{
// Command timeout
Expand Down
8 changes: 8 additions & 0 deletions src/NHibernate/Driver/IDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public interface IDriver
/// </summary>
void Configure(IDictionary<string, string> settings);

//NH-3724
/// <summary>
/// Attaches an event handler for the notification event (InfoMessage in most ADO.NET drivers).
/// </summary>
/// <param name="con"></param>
/// <param name="handler"></param>
void AddNotificationHandler(DbConnection con, Delegate handler);

/// <summary>
/// Creates an uninitialized DbConnection object for the specific Driver
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Driver/IfxDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Data;

namespace NHibernate.Driver
{
/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions src/NHibernate/Driver/NpgsqlDriver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using System.Data.Common;
using NHibernate.AdoNet;
Expand Down Expand Up @@ -41,6 +42,12 @@ public NpgsqlDriver() : base(
{
}

public override void AddNotificationHandler(DbConnection con, Delegate handler)
{
//NH-3724
con.GetType().GetEvent("Notification").AddEventHandler(con, handler);
}

public override bool UseNamedPrefixInSql => true;

public override bool UseNamedPrefixInParameter => true;
Expand Down
7 changes: 7 additions & 0 deletions src/NHibernate/Driver/OdbcDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public override void Configure(IDictionary<string, string> settings)
}
}


#if !NETFX
public OdbcDriver()
: base("System.Data.Odbc", "System.Data.Odbc.OdbcConnection", "System.Data.Odbc.OdbcCommand")
Expand All @@ -49,6 +50,12 @@ public override DbConnection CreateConnection()
return new System.Data.Odbc.OdbcConnection();
}

public override void AddNotificationHandler(DbConnection con, Delegate handler)
{
//NH-3724
(con as System.Data.Odbc.OdbcConnection).InfoMessage += (System.Data.Odbc.OdbcInfoMessageEventHandler) handler;
}

public override DbCommand CreateCommand()
{
return new System.Data.Odbc.OdbcCommand();
Expand Down
6 changes: 6 additions & 0 deletions src/NHibernate/Driver/OleDbDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public override DbConnection CreateConnection()
return new System.Data.OleDb.OleDbConnection();
}

public override void AddNotificationHandler(DbConnection con, Delegate handler)
{
//NH-3724
(con as System.Data.OleDb.OleDbConnection).InfoMessage += (System.Data.OleDb.OleDbInfoMessageEventHandler) handler;
}

public override DbCommand CreateCommand()
{
return new System.Data.OleDb.OleDbCommand();
Expand Down
7 changes: 6 additions & 1 deletion src/NHibernate/Driver/SqlClientDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using NHibernate.Dialect;
using NHibernate.Engine;
using NHibernate.SqlTypes;
using NHibernate.Type;

namespace NHibernate.Driver
{
Expand Down Expand Up @@ -88,6 +87,12 @@ public override DbConnection CreateConnection()
return new SqlConnection();
}

public override void AddNotificationHandler(DbConnection con, Delegate handler)
{
//NH-3724
(con as SqlConnection).InfoMessage += (SqlInfoMessageEventHandler) handler;
}

/// <summary>
/// Creates an uninitialized <see cref="DbCommand" /> object for
/// the SqlClientDriver.
Expand Down