Skip to content

Commit f82f953

Browse files
Clean up a bit isolation work code
1 parent 0a85990 commit f82f953

File tree

2 files changed

+29
-81
lines changed

2 files changed

+29
-81
lines changed

src/NHibernate/Async/Transaction/AdoNetTransactionFactory.cs

Lines changed: 15 additions & 41 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,33 +41,24 @@ 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,
4947
// since SQLite only allows one connection to the database.
50-
if (session.Factory.Dialect is SQLiteDialect)
51-
connection = session.Connection;
52-
else
53-
connection = await (session.Factory.ConnectionProvider.GetConnectionAsync(cancellationToken)).ConfigureAwait(false);
48+
var connection = session.Factory.Dialect is SQLiteDialect
49+
? session.Connection
50+
: await (session.Factory.ConnectionProvider.GetConnectionAsync(cancellationToken)).ConfigureAwait(false);
5451

5552
if (transacted)
5653
{
5754
trans = connection.BeginTransaction();
58-
// TODO NH: a way to read the autocommit state is needed
59-
//if (TransactionManager.GetAutoCommit(connection))
60-
//{
61-
// wasAutoCommit = true;
62-
// TransactionManager.SetAutoCommit(connection, false);
63-
//}
6455
}
6556

6657
await (work.DoWorkAsync(connection, trans, cancellationToken)).ConfigureAwait(false);
6758

6859
if (transacted)
6960
{
7061
trans.Commit();
71-
//TransactionManager.Commit(connection);
7262
}
7363
}
7464
catch (Exception t)
@@ -84,46 +74,30 @@ async Task InternalExecuteWorkInIsolationAsync()
8474
}
8575
catch (Exception ignore)
8676
{
87-
isolaterLog.Debug(ignore, "Unable to rollback transaction");
77+
_isolatorLog.Debug(ignore, "Unable to rollback transaction");
8878
}
8979

90-
if (t is HibernateException)
91-
{
92-
throw;
93-
}
94-
else if (t is DbException)
95-
{
96-
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, t,
97-
"error performing isolated work");
98-
}
99-
else
100-
{
101-
throw new HibernateException("error performing isolated work", t);
102-
}
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+
}
10390
}
10491
}
10592
finally
10693
{
107-
//if (transacted && wasAutoCommit)
108-
//{
109-
// try
110-
// {
111-
// // TODO NH: reset autocommit
112-
// // TransactionManager.SetAutoCommit(connection, true);
113-
// }
114-
// catch (Exception)
115-
// {
116-
// log.Debug("was unable to reset connection back to auto-commit");
117-
// }
118-
//}
119-
12094
try
12195
{
12296
trans?.Dispose();
12397
}
12498
catch (Exception ignore)
12599
{
126-
isolaterLog.Warn(ignore, "Unable to dispose transaction");
100+
_isolatorLog.Warn(ignore, "Unable to dispose transaction");
127101
}
128102

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

src/NHibernate/Transaction/AdoNetTransactionFactory.cs

Lines changed: 14 additions & 40 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,33 +51,24 @@ 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,
5957
// since SQLite only allows one connection to the database.
60-
if (session.Factory.Dialect is SQLiteDialect)
61-
connection = session.Connection;
62-
else
63-
connection = session.Factory.ConnectionProvider.GetConnection();
58+
var connection = session.Factory.Dialect is SQLiteDialect
59+
? session.Connection
60+
: session.Factory.ConnectionProvider.GetConnection();
6461

6562
if (transacted)
6663
{
6764
trans = connection.BeginTransaction();
68-
// TODO NH: a way to read the autocommit state is needed
69-
//if (TransactionManager.GetAutoCommit(connection))
70-
//{
71-
// wasAutoCommit = true;
72-
// TransactionManager.SetAutoCommit(connection, false);
73-
//}
7465
}
7566

7667
work.DoWork(connection, trans);
7768

7869
if (transacted)
7970
{
8071
trans.Commit();
81-
//TransactionManager.Commit(connection);
8272
}
8373
}
8474
catch (Exception t)
@@ -94,46 +84,30 @@ public virtual void ExecuteWorkInIsolation(ISessionImplementor session, IIsolate
9484
}
9585
catch (Exception ignore)
9686
{
97-
isolaterLog.Debug(ignore, "Unable to rollback transaction");
87+
_isolatorLog.Debug(ignore, "Unable to rollback transaction");
9888
}
9989

100-
if (t is HibernateException)
90+
switch (t)
10191
{
102-
throw;
103-
}
104-
else if (t is DbException)
105-
{
106-
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, t,
107-
"error performing isolated work");
108-
}
109-
else
110-
{
111-
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);
11299
}
113100
}
114101
}
115102
finally
116103
{
117-
//if (transacted && wasAutoCommit)
118-
//{
119-
// try
120-
// {
121-
// // TODO NH: reset autocommit
122-
// // TransactionManager.SetAutoCommit(connection, true);
123-
// }
124-
// catch (Exception)
125-
// {
126-
// log.Debug("was unable to reset connection back to auto-commit");
127-
// }
128-
//}
129-
130104
try
131105
{
132106
trans?.Dispose();
133107
}
134108
catch (Exception ignore)
135109
{
136-
isolaterLog.Warn(ignore, "Unable to dispose transaction");
110+
_isolatorLog.Warn(ignore, "Unable to dispose transaction");
137111
}
138112

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

0 commit comments

Comments
 (0)