-
Notifications
You must be signed in to change notification settings - Fork 934
Fix some NRE in work isolation and connection handling #2337
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
Fix some NRE in work isolation and connection handling #2337
Conversation
@@ -24,6 +24,9 @@ public abstract partial class ConnectionProvider : IConnectionProvider | |||
/// <param name="conn">The <see cref="DbConnection"/> to clean up.</param> | |||
public virtual void CloseConnection(DbConnection conn) | |||
{ | |||
if (conn == null) | |||
throw new ArgumentNullException(nameof(conn)); |
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.
We can choose to just exit instead of throwing. But since the case was clearly not supported, causing a NRE in the try then a second NRE in the catch, I consider the current code base is never meant to supply null
to this method, so it looks better to throw.
@@ -136,7 +136,7 @@ public virtual void ExecuteWorkInIsolation(ISessionImplementor session, IIsolate | |||
isolaterLog.Warn(ignore, "Unable to dispose transaction"); | |||
} | |||
|
|||
if (session.Factory.Dialect is SQLiteDialect == false) | |||
if (connection != null && session.Factory.Dialect is SQLiteDialect == false) |
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.
That's some messy method here.... Additional null check doesn't make it better. May be instead move connection retrieving logic outside of try
block?
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.
Well maybe not.. That would change exception..
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.
At least this will no more swallow a connection opening failure. But yes, it is indeed better to put the connection opening out of the try. It has no sense being into it.
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.
Well... Changing from NRE unrelated to the trouble to the bare exception is still better.
But yes, it loses the HibernateException
encapsulation.
Switching back.
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.
I've just realized that this exception never was wrapped (my main concern was that we changed existing behavior - but we didn't) and I really don't see any benefits in such wrappings (seems like some legacy practice). So maybe better again switch back? )
DbTransaction trans = null; | ||
// bool wasAutoCommit = false; |
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.
I have added a small cleanup commit.
Afaik, the auto-commit feature and the transaction manager are not ported into NHibernate, so those decade old comments and todo-s are unlikely to be done any time soon. They are just polluting the code in my view.
// We make an exception for SQLite and use the session's connection, | ||
// since SQLite only allows one connection to the database. | ||
var connection = session.Factory.Dialect is SQLiteDialect | ||
? session.Connection |
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.
@maca88, small indentation glitch here with async generator, that you may be interested to look into. Nothing important, it is just for polishing it if you wish.
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.
Fixed in #2363.
throw new HibernateException("error performing isolated work", t); | ||
} | ||
switch (t) | ||
{ |
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.
Another indentation glitch.
d88025f
to
8f45c08
Compare
Back to initial change, forced push to remove fixup noise. |
8f45c08
to
f82f953
Compare
f82f953
to
1962361
Compare
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.
I would prefer to have connection opening be outside of try block but feel free to keep it as is
@@ -16,7 +15,7 @@ namespace NHibernate.Transaction | |||
/// </summary> | |||
public partial class AdoNetTransactionFactory : ITransactionFactory | |||
{ | |||
private readonly INHibernateLogger isolaterLog = NHibernateLogger.For(typeof(ITransactionFactory)); | |||
private readonly INHibernateLogger _isolatorLog = NHibernateLogger.For(typeof(ITransactionFactory)); |
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.
Shouldn't it be static?
@@ -136,7 +136,7 @@ public virtual void ExecuteWorkInIsolation(ISessionImplementor session, IIsolate | |||
isolaterLog.Warn(ignore, "Unable to dispose transaction"); | |||
} | |||
|
|||
if (session.Factory.Dialect is SQLiteDialect == false) | |||
if (connection != null && session.Factory.Dialect is SQLiteDialect == false) |
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.
I've just realized that this exception never was wrapped (my main concern was that we changed existing behavior - but we didn't) and I really don't see any benefits in such wrappings (seems like some legacy practice). So maybe better again switch back? )
Yes it could not be wrapped since the finally was failing with NRE. |
1962361
to
8d58808
Compare
I'm very suspicious that |
I have amended the cleanup commit for switching the logger to static. |
|
Ok.. Bad example. But I meant I'm pretty sure that many session methods wouldn't wrap such issue in HibernateException. |
Fixes #2336