forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdoNetTransactionFactory.cs
120 lines (110 loc) · 3.38 KB
/
AdoNetTransactionFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using log4net;
using NHibernate.Dialect;
using NHibernate.Engine;
using NHibernate.Engine.Transaction;
using NHibernate.Exceptions;
using NHibernate.Impl;
namespace NHibernate.Transaction
{
public class AdoNetTransactionFactory : ITransactionFactory
{
private readonly ILog isolaterLog = LogManager.GetLogger(typeof(Isolater));
public ITransaction CreateTransaction(ISessionImplementor session)
{
return new AdoTransaction(session);
}
public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
{
// nothing need to do here, we only support local transactions with this factory
}
public bool IsInDistributedActiveTransaction(ISessionImplementor session)
{
return false;
}
public void ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork work, bool transacted)
{
IDbConnection connection = null;
IDbTransaction trans = null;
// bool wasAutoCommit = false;
try
{
// We make an exception for SQLite and use the session's connection,
// since SQLite only allows one connection to the database.
if (session.Factory.Dialect is SQLiteDialect)
connection = session.Connection;
else
connection = session.Factory.ConnectionProvider.GetConnection();
if (transacted)
{
trans = connection.BeginTransaction();
// TODO NH: a way to read the autocommit state is needed
//if (TransactionManager.GetAutoCommit(connection))
//{
// wasAutoCommit = true;
// TransactionManager.SetAutoCommit(connection, false);
//}
}
work.DoWork(connection, trans);
if (transacted)
{
trans.Commit();
//TransactionManager.Commit(connection);
}
}
catch (Exception t)
{
using (new SessionIdLoggingContext(session.SessionId))
{
try
{
if (trans != null && connection.State != ConnectionState.Closed)
{
trans.Rollback();
}
}
catch (Exception ignore)
{
isolaterLog.Debug("unable to release connection on exception [" + ignore + "]");
}
if (t is HibernateException)
{
throw;
}
else if (t is DbException)
{
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, t,
"error performing isolated work");
}
else
{
throw new HibernateException("error performing isolated work", t);
}
}
}
finally
{
//if (transacted && wasAutoCommit)
//{
// try
// {
// // TODO NH: reset autocommit
// // TransactionManager.SetAutoCommit(connection, true);
// }
// catch (Exception)
// {
// log.Debug("was unable to reset connection back to auto-commit");
// }
//}
if (session.Factory.Dialect is SQLiteDialect == false)
session.Factory.ConnectionProvider.CloseConnection(connection);
}
}
public void Configure(IDictionary props)
{
}
}
}