@@ -28,88 +28,111 @@ namespace NHibernate.Transaction
28
28
public partial class AdoNetTransactionFactory : ITransactionFactory
29
29
{
30
30
31
- public async Task ExecuteWorkInIsolationAsync ( ISessionImplementor session , IIsolatedWork work , bool transacted , CancellationToken cancellationToken )
31
+ /// <inheritdoc />
32
+ public virtual Task ExecuteWorkInIsolationAsync ( ISessionImplementor session , IIsolatedWork work , bool transacted , CancellationToken cancellationToken )
32
33
{
33
- cancellationToken . ThrowIfCancellationRequested ( ) ;
34
- DbConnection connection = null ;
35
- DbTransaction trans = null ;
36
- // bool wasAutoCommit = false;
37
- try
34
+ if ( session == null )
35
+ throw new ArgumentNullException ( nameof ( session ) ) ;
36
+ if ( work == null )
37
+ throw new ArgumentNullException ( nameof ( work ) ) ;
38
+ if ( cancellationToken . IsCancellationRequested )
39
+ {
40
+ return Task . FromCanceled < object > ( cancellationToken ) ;
41
+ }
42
+ return InternalExecuteWorkInIsolationAsync ( ) ;
43
+ async Task InternalExecuteWorkInIsolationAsync ( )
38
44
{
39
- // We make an exception for SQLite and use the session's connection,
40
- // since SQLite only allows one connection to the database.
41
- if ( session . Factory . Dialect is SQLiteDialect )
42
- connection = session . Connection ;
43
- else
44
- connection = await ( session . Factory . ConnectionProvider . GetConnectionAsync ( cancellationToken ) ) . ConfigureAwait ( false ) ;
45
45
46
- if ( transacted )
46
+ DbConnection connection = null ;
47
+ DbTransaction trans = null ;
48
+ // bool wasAutoCommit = false;
49
+ try
47
50
{
48
- trans = connection . BeginTransaction ( ) ;
49
- // TODO NH: a way to read the autocommit state is needed
50
- //if (TransactionManager.GetAutoCommit(connection))
51
- //{
52
- // wasAutoCommit = true;
53
- // TransactionManager.SetAutoCommit(connection, false);
54
- //}
55
- }
51
+ // We make an exception for SQLite and use the session's connection,
52
+ // since SQLite only allows one connection to the database.
53
+ if ( session . Factory . Dialect is SQLiteDialect )
54
+ connection = session . Connection ;
55
+ else
56
+ connection = await ( session . Factory . ConnectionProvider . GetConnectionAsync ( cancellationToken ) ) . ConfigureAwait ( false ) ;
56
57
57
- await ( work . DoWorkAsync ( connection , trans , cancellationToken ) ) . ConfigureAwait ( false ) ;
58
+ if ( transacted )
59
+ {
60
+ trans = connection . BeginTransaction ( ) ;
61
+ // TODO NH: a way to read the autocommit state is needed
62
+ //if (TransactionManager.GetAutoCommit(connection))
63
+ //{
64
+ // wasAutoCommit = true;
65
+ // TransactionManager.SetAutoCommit(connection, false);
66
+ //}
67
+ }
58
68
59
- if ( transacted )
60
- {
61
- trans . Commit ( ) ;
62
- //TransactionManager.Commit(connection);
69
+ await ( work . DoWorkAsync ( connection , trans , cancellationToken ) ) . ConfigureAwait ( false ) ;
70
+
71
+ if ( transacted )
72
+ {
73
+ trans . Commit ( ) ;
74
+ //TransactionManager.Commit(connection);
75
+ }
63
76
}
64
- }
65
- catch ( Exception t )
66
- {
67
- using ( new SessionIdLoggingContext ( session . SessionId ) )
77
+ catch ( Exception t )
68
78
{
69
- try
79
+ using ( new SessionIdLoggingContext ( session . SessionId ) )
70
80
{
71
- if ( trans != null && connection . State != ConnectionState . Closed )
81
+ try
72
82
{
73
- trans . Rollback ( ) ;
83
+ if ( trans != null && connection . State != ConnectionState . Closed )
84
+ {
85
+ trans . Rollback ( ) ;
86
+ }
87
+ }
88
+ catch ( Exception ignore )
89
+ {
90
+ isolaterLog . Debug ( "Unable to rollback transaction" , ignore ) ;
74
91
}
75
- }
76
- catch ( Exception ignore )
77
- {
78
- isolaterLog . Debug ( "unable to release connection on exception [" + ignore + "]" ) ;
79
- }
80
92
81
- if ( t is HibernateException )
82
- {
83
- throw ;
93
+ if ( t is HibernateException )
94
+ {
95
+ throw ;
96
+ }
97
+ else if ( t is DbException )
98
+ {
99
+ throw ADOExceptionHelper . Convert ( session . Factory . SQLExceptionConverter , t ,
100
+ "error performing isolated work" ) ;
101
+ }
102
+ else
103
+ {
104
+ throw new HibernateException ( "error performing isolated work" , t ) ;
105
+ }
84
106
}
85
- else if ( t is DbException )
107
+ }
108
+ finally
109
+ {
110
+ //if (transacted && wasAutoCommit)
111
+ //{
112
+ // try
113
+ // {
114
+ // // TODO NH: reset autocommit
115
+ // // TransactionManager.SetAutoCommit(connection, true);
116
+ // }
117
+ // catch (Exception)
118
+ // {
119
+ // log.Debug("was unable to reset connection back to auto-commit");
120
+ // }
121
+ //}
122
+
123
+ try
86
124
{
87
- throw ADOExceptionHelper . Convert ( session . Factory . SQLExceptionConverter , t ,
88
- "error performing isolated work" ) ;
125
+ trans ? . Dispose ( ) ;
89
126
}
90
- else
127
+ catch ( Exception ignore )
91
128
{
92
- throw new HibernateException ( "error performing isolated work ", t ) ;
129
+ isolaterLog . Warn ( "Unable to dispose transaction ", ignore ) ;
93
130
}
131
+
132
+ if ( session . Factory . Dialect is SQLiteDialect == false )
133
+ session . Factory . ConnectionProvider . CloseConnection ( connection ) ;
94
134
}
95
135
}
96
- finally
97
- {
98
- //if (transacted && wasAutoCommit)
99
- //{
100
- // try
101
- // {
102
- // // TODO NH: reset autocommit
103
- // // TransactionManager.SetAutoCommit(connection, true);
104
- // }
105
- // catch (Exception)
106
- // {
107
- // log.Debug("was unable to reset connection back to auto-commit");
108
- // }
109
- //}
110
- if ( session . Factory . Dialect is SQLiteDialect == false )
111
- session . Factory . ConnectionProvider . CloseConnection ( connection ) ;
112
- }
113
136
}
114
137
}
115
138
}
0 commit comments