forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionManager.cs
353 lines (312 loc) · 7.91 KB
/
ConnectionManager.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Data;
using System.Runtime.Serialization;
using System.Security.Permissions;
using log4net;
using NHibernate.Engine;
namespace NHibernate.AdoNet
{
/// <summary>
/// Manages the database connection and transaction for an <see cref="ISession" />.
/// </summary>
/// <remarks>
/// This class corresponds to ConnectionManager and JDBCContext in Hibernate,
/// combined.
/// </remarks>
[Serializable]
public class ConnectionManager : ISerializable
{
private static readonly ILog log = LogManager.GetLogger(typeof(ConnectionManager));
[NonSerialized]
private IDbConnection connection;
// Whether we own the connection, i.e. connect and disconnect automatically.
private bool ownConnection;
[NonSerialized]
private ITransaction transaction;
private readonly ISessionImplementor session;
private readonly ConnectionReleaseMode connectionReleaseMode;
[NonSerialized]
private bool isFlushing;
public ConnectionManager(
ISessionImplementor session,
IDbConnection suppliedConnection,
ConnectionReleaseMode connectionReleaseMode)
{
this.session = session;
this.connection = suppliedConnection;
this.connectionReleaseMode = connectionReleaseMode;
this.ownConnection = suppliedConnection == null;
}
public bool IsInActiveTransaction
{
get { return transaction != null && transaction.IsActive; }
}
public bool IsConnected
{
get { return connection != null || ownConnection; }
}
public void Reconnect()
{
if (IsConnected)
{
throw new HibernateException("session already connected");
}
ownConnection = true;
}
public void Reconnect(IDbConnection suppliedConnection)
{
if (IsConnected)
{
throw new HibernateException("session already connected");
}
log.Debug("reconnecting session");
connection = suppliedConnection;
ownConnection = false;
}
public IDbConnection Close()
{
if (session.Batcher != null)
{
session.Batcher.Dispose();
}
if (transaction != null)
{
transaction.Dispose();
}
// When the connection is null nothing needs to be done - if there
// is a value for connection then Disconnect() was not called - so we
// need to ensure it gets called.
if (connection == null)
{
ownConnection = false;
return null;
}
else
{
return Disconnect();
}
}
private IDbConnection DisconnectSuppliedConnection()
{
if (connection == null)
{
throw new HibernateException("Session already disconnected");
}
IDbConnection c = connection;
connection = null;
return c;
}
private void DisconnectOwnConnection()
{
if (connection == null)
{
// No active connection
return;
}
if (session.Batcher != null)
{
session.Batcher.CloseCommands();
}
CloseConnection();
}
public IDbConnection Disconnect()
{
try
{
if (!ownConnection)
{
return DisconnectSuppliedConnection();
}
else
{
DisconnectOwnConnection();
ownConnection = false;
return null;
}
}
finally
{
// Ensure that AfterTransactionCompletion gets called since
// it takes care of the locks and cache.
if (!IsInActiveTransaction)
{
// We don't know the state of the transaction
session.AfterTransactionCompletion(false, null);
}
}
}
private void CloseConnection()
{
session.Factory.CloseConnection(connection);
connection = null;
}
public IDbConnection GetConnection()
{
if (connection == null)
{
if (ownConnection)
{
connection = session.Factory.OpenConnection();
if (session.Factory.Statistics.IsStatisticsEnabled)
{
session.Factory.StatisticsImplementor.Connect();
}
}
else if (session.IsOpen)
{
throw new HibernateException("Session is currently disconnected");
}
else
{
throw new HibernateException("Session is closed");
}
}
return connection;
}
public void AfterTransaction()
{
if (IsAfterTransactionRelease)
{
AggressiveRelease();
}
else if (IsAggressiveRelease && session.Batcher.HasOpenResources)
{
log.Info("forcing batcher resource cleanup on transaction completion; forgot to close ScrollableResults/Enumerable?");
session.Batcher.CloseCommands();
AggressiveRelease();
}
else if (IsOnCloseRelease)
{
// log a message about potential connection leaks
log.Debug(
"transaction completed on session with on_close connection release mode; be sure to close the session to release ADO.Net resources!");
}
transaction = null;
}
public void AfterStatement()
{
if (IsAggressiveRelease)
{
if (isFlushing)
{
log.Debug("skipping aggressive-release due to flush cycle");
}
else if (session.Batcher.HasOpenResources)
{
log.Debug("skipping aggressive-release due to open resources on batcher");
}
// TODO H3:
//else if (borrowedConnection != null)
//{
// log.Debug("skipping aggressive-release due to borrowed connection");
//}
else
{
AggressiveRelease();
}
}
}
private void AggressiveRelease()
{
if (ownConnection)
{
log.Debug("aggressively releasing database connection");
if (connection != null)
{
CloseConnection();
}
}
}
public void FlushBeginning()
{
log.Debug("registering flush begin");
isFlushing = true;
}
public void FlushEnding()
{
log.Debug("registering flush end");
isFlushing = false;
AfterStatement();
}
#region Serialization
private ConnectionManager(SerializationInfo info, StreamingContext context)
{
this.ownConnection = info.GetBoolean("ownConnection");
this.session = (ISessionImplementor) info.GetValue("session", typeof(ISessionImplementor));
this.connectionReleaseMode =
(ConnectionReleaseMode) info.GetValue("connectionReleaseMode", typeof(ConnectionReleaseMode));
}
[SecurityPermission(SecurityAction.LinkDemand,
Flags = SecurityPermissionFlag.SerializationFormatter)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("ownConnection", ownConnection);
info.AddValue("session", session, typeof(ISessionImplementor));
info.AddValue("connectionReleaseMode", connectionReleaseMode, typeof(ConnectionReleaseMode));
}
#endregion
public ITransaction BeginTransaction(IsolationLevel isolationLevel)
{
Transaction.Begin(isolationLevel);
return transaction;
}
public ITransaction BeginTransaction()
{
Transaction.Begin();
return transaction;
}
public ITransaction Transaction
{
get
{
if (transaction == null)
{
transaction = session.Factory.TransactionFactory.CreateTransaction(session);
}
return transaction;
}
}
public void AfterNonTransactionalQuery(bool success)
{
log.Debug("after autocommit");
session.AfterTransactionCompletion(success, null);
}
private bool IsAfterTransactionRelease
{
get { return connectionReleaseMode == ConnectionReleaseMode.AfterTransaction; }
}
private bool IsOnCloseRelease
{
get { return connectionReleaseMode == ConnectionReleaseMode.OnClose; }
}
private bool IsAggressiveRelease
{
get
{
if (connectionReleaseMode == ConnectionReleaseMode.AfterTransaction)
{
return !IsInActiveTransaction;
}
return false;
}
}
public ISessionFactoryImplementor Factory
{
get { return session.Factory; }
}
public bool IsReadyForSerialization
{
get
{
if (ownConnection)
{
return connection == null && !session.Batcher.HasOpenResources;
}
else
{
return connection == null;
}
}
}
}
}