Skip to content

Commit 8f45c08

Browse files
Clean up a bit isolation work code
1 parent dbeda66 commit 8f45c08

File tree

2 files changed

+23
-73
lines changed

2 files changed

+23
-73
lines changed

src/NHibernate/Async/Transaction/AdoNetTransactionFactory.cs

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using NHibernate.Engine;
1717
using NHibernate.Engine.Transaction;
1818
using NHibernate.Exceptions;
19-
using NHibernate.Impl;
2019

2120
namespace NHibernate.Transaction
2221
{
@@ -42,7 +41,6 @@ async Task InternalExecuteWorkInIsolationAsync()
4241

4342
DbConnection connection = null;
4443
DbTransaction trans = null;
45-
// bool wasAutoCommit = false;
4644
try
4745
{
4846
// We make an exception for SQLite and use the session's connection,
@@ -54,20 +52,13 @@ async Task InternalExecuteWorkInIsolationAsync()
5452
if (transacted)
5553
{
5654
trans = connection.BeginTransaction();
57-
// TODO NH: a way to read the autocommit state is needed
58-
//if (TransactionManager.GetAutoCommit(connection))
59-
//{
60-
// wasAutoCommit = true;
61-
// TransactionManager.SetAutoCommit(connection, false);
62-
//}
6355
}
6456

6557
await (work.DoWorkAsync(connection, trans, cancellationToken)).ConfigureAwait(false);
6658

6759
if (transacted)
6860
{
6961
trans.Commit();
70-
//TransactionManager.Commit(connection);
7162
}
7263
}
7364
catch (Exception t)
@@ -83,46 +74,30 @@ async Task InternalExecuteWorkInIsolationAsync()
8374
}
8475
catch (Exception ignore)
8576
{
86-
isolaterLog.Debug(ignore, "Unable to rollback transaction");
77+
_isolatorLog.Debug(ignore, "Unable to rollback transaction");
8778
}
8879

89-
if (t is HibernateException)
90-
{
91-
throw;
92-
}
93-
else if (t is DbException)
94-
{
95-
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, t,
96-
"error performing isolated work");
97-
}
98-
else
99-
{
100-
throw new HibernateException("error performing isolated work", t);
101-
}
80+
switch (t)
81+
{
82+
case HibernateException _:
83+
throw;
84+
case DbException _:
85+
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, t,
86+
"error performing isolated work");
87+
default:
88+
throw new HibernateException("error performing isolated work", t);
89+
}
10290
}
10391
}
10492
finally
10593
{
106-
//if (transacted && wasAutoCommit)
107-
//{
108-
// try
109-
// {
110-
// // TODO NH: reset autocommit
111-
// // TransactionManager.SetAutoCommit(connection, true);
112-
// }
113-
// catch (Exception)
114-
// {
115-
// log.Debug("was unable to reset connection back to auto-commit");
116-
// }
117-
//}
118-
11994
try
12095
{
12196
trans?.Dispose();
12297
}
12398
catch (Exception ignore)
12499
{
125-
isolaterLog.Warn(ignore, "Unable to dispose transaction");
100+
_isolatorLog.Warn(ignore, "Unable to dispose transaction");
126101
}
127102

128103
if (connection != null && session.Factory.Dialect is SQLiteDialect == false)

src/NHibernate/Transaction/AdoNetTransactionFactory.cs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using NHibernate.Engine;
77
using NHibernate.Engine.Transaction;
88
using NHibernate.Exceptions;
9-
using NHibernate.Impl;
109

1110
namespace NHibernate.Transaction
1211
{
@@ -16,7 +15,7 @@ namespace NHibernate.Transaction
1615
/// </summary>
1716
public partial class AdoNetTransactionFactory : ITransactionFactory
1817
{
19-
private readonly INHibernateLogger isolaterLog = NHibernateLogger.For(typeof(ITransactionFactory));
18+
private readonly INHibernateLogger _isolatorLog = NHibernateLogger.For(typeof(ITransactionFactory));
2019

2120
/// <inheritdoc />
2221
public virtual ITransaction CreateTransaction(ISessionImplementor session)
@@ -52,7 +51,6 @@ public virtual void ExecuteWorkInIsolation(ISessionImplementor session, IIsolate
5251

5352
DbConnection connection = null;
5453
DbTransaction trans = null;
55-
// bool wasAutoCommit = false;
5654
try
5755
{
5856
// We make an exception for SQLite and use the session's connection,
@@ -64,20 +62,13 @@ public virtual void ExecuteWorkInIsolation(ISessionImplementor session, IIsolate
6462
if (transacted)
6563
{
6664
trans = connection.BeginTransaction();
67-
// TODO NH: a way to read the autocommit state is needed
68-
//if (TransactionManager.GetAutoCommit(connection))
69-
//{
70-
// wasAutoCommit = true;
71-
// TransactionManager.SetAutoCommit(connection, false);
72-
//}
7365
}
7466

7567
work.DoWork(connection, trans);
7668

7769
if (transacted)
7870
{
7971
trans.Commit();
80-
//TransactionManager.Commit(connection);
8172
}
8273
}
8374
catch (Exception t)
@@ -93,46 +84,30 @@ public virtual void ExecuteWorkInIsolation(ISessionImplementor session, IIsolate
9384
}
9485
catch (Exception ignore)
9586
{
96-
isolaterLog.Debug(ignore, "Unable to rollback transaction");
87+
_isolatorLog.Debug(ignore, "Unable to rollback transaction");
9788
}
9889

99-
if (t is HibernateException)
90+
switch (t)
10091
{
101-
throw;
102-
}
103-
else if (t is DbException)
104-
{
105-
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, t,
106-
"error performing isolated work");
107-
}
108-
else
109-
{
110-
throw new HibernateException("error performing isolated work", t);
92+
case HibernateException _:
93+
throw;
94+
case DbException _:
95+
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, t,
96+
"error performing isolated work");
97+
default:
98+
throw new HibernateException("error performing isolated work", t);
11199
}
112100
}
113101
}
114102
finally
115103
{
116-
//if (transacted && wasAutoCommit)
117-
//{
118-
// try
119-
// {
120-
// // TODO NH: reset autocommit
121-
// // TransactionManager.SetAutoCommit(connection, true);
122-
// }
123-
// catch (Exception)
124-
// {
125-
// log.Debug("was unable to reset connection back to auto-commit");
126-
// }
127-
//}
128-
129104
try
130105
{
131106
trans?.Dispose();
132107
}
133108
catch (Exception ignore)
134109
{
135-
isolaterLog.Warn(ignore, "Unable to dispose transaction");
110+
_isolatorLog.Warn(ignore, "Unable to dispose transaction");
136111
}
137112

138113
if (connection != null && session.Factory.Dialect is SQLiteDialect == false)

0 commit comments

Comments
 (0)