Skip to content

Commit ef7e7e1

Browse files
author
Sergey Koshcheyev
committed
ConnectionReleaseMode stubbed.
Minor change to IBatcher to remove CloseQueryCommand which is equivalent to CloseCommand. SVN: trunk@2530
1 parent 0203812 commit ef7e7e1

11 files changed

+88
-76
lines changed

Diff for: src/NHibernate/ConnectionReleaseMode.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace NHibernate
4+
{
5+
public enum ConnectionReleaseMode
6+
{
7+
AfterStatement,
8+
AfterTransaction,
9+
OnClose
10+
}
11+
}

Diff for: src/NHibernate/Engine/IBatcher.cs

+1-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface IBatcher : IDisposable
3636
/// </returns>
3737
/// <remarks>
3838
/// <para>
39-
/// If not explicitly released by <c>CloseQueryStatement()</c>, it will be
39+
/// If not explicitly released by <see cref="CloseCommand" />, it will be
4040
/// released when the session is closed or disconnected.
4141
/// </para>
4242
/// <para>
@@ -46,14 +46,6 @@ public interface IBatcher : IDisposable
4646
/// </remarks>
4747
IDbCommand PrepareQueryCommand( CommandType commandType, SqlString sql, SqlType[] parameterTypes );
4848

49-
/// <summary>
50-
/// Closes the <see cref="IDbCommand"/> &amp; the <see cref="IDataReader"/> that was
51-
/// opened with the method <c>PrepareQueryCommand</c>.
52-
/// </summary>
53-
/// <param name="cmd">The <see cref="IDbCommand"/> to close.</param>
54-
/// <param name="reader">The <see cref="IDataReader"/> to close.</param>
55-
void CloseQueryCommand( IDbCommand cmd, IDataReader reader );
56-
5749
/// <summary>
5850
/// Get a non-batchable an <see cref="IDbCommand"/> to use for inserting / deleting / updating.
5951
/// Must be explicitly released by <c>CloseCommand()</c>

Diff for: src/NHibernate/Impl/BatcherImpl.cs

+14-21
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,6 @@ protected void CheckReaders()
213213
}
214214
}
215215

216-
public void CloseCommand(IDbCommand cmd, IDataReader reader)
217-
{
218-
CloseQueryCommand(cmd, reader);
219-
}
220-
221216
public void CloseCommands()
222217
{
223218
foreach (IDataReader reader in readersToClose)
@@ -238,7 +233,7 @@ public void CloseCommands()
238233
{
239234
try
240235
{
241-
CloseQueryCommand(cmd);
236+
CloseCommand(cmd);
242237
}
243238
catch (Exception e)
244239
{
@@ -249,16 +244,13 @@ public void CloseCommands()
249244
commandsToClose.Clear();
250245
}
251246

252-
private void CloseQueryCommand(IDbCommand cmd)
247+
private void CloseCommand(IDbCommand cmd)
253248
{
254249
try
255250
{
256251
// no equiv to the java code in here
257-
if (cmd != null)
258-
{
259-
cmd.Dispose();
260-
LogClosePreparedCommand();
261-
}
252+
cmd.Dispose();
253+
LogClosePreparedCommand();
262254
}
263255
catch (Exception e)
264256
{
@@ -273,28 +265,29 @@ private void CloseQueryCommand(IDbCommand cmd)
273265
}
274266
}
275267

276-
public void CloseQueryCommand(IDbCommand st, IDataReader reader)
268+
public void CloseCommand(IDbCommand st, IDataReader reader)
277269
{
278270
commandsToClose.Remove(st);
279-
if (reader != null)
280-
{
281-
readersToClose.Remove(reader);
282-
}
283271
try
284272
{
285273
if (reader != null)
286274
{
287-
reader.Close();
288-
reader.Dispose();
289-
LogCloseReader();
275+
readersToClose.Remove(reader);
276+
CloseReader(reader);
290277
}
291278
}
292279
finally
293280
{
294-
CloseQueryCommand(st);
281+
CloseCommand(st);
295282
}
296283
}
297284

285+
private void CloseReader(IDataReader reader)
286+
{
287+
reader.Dispose();
288+
LogCloseReader();
289+
}
290+
298291
/// <summary></summary>
299292
public void ExecuteBatch()
300293
{

Diff for: src/NHibernate/Impl/ConnectionManager.cs

+41-1
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ public class ConnectionManager : ISerializable
3030

3131
private readonly ISessionImplementor session;
3232
private bool autoClose;
33+
private readonly ConnectionReleaseMode connectionReleaseMode;
3334

34-
public ConnectionManager(ISessionImplementor session, IDbConnection connection, bool autoClose)
35+
public ConnectionManager(
36+
ISessionImplementor session,
37+
IDbConnection connection,
38+
ConnectionReleaseMode connectionReleaseMode,
39+
bool autoClose)
3540
{
3641
this.session = session;
3742
this.connection = connection;
3843
this.connectForNextOperation = connection == null;
44+
this.connectionReleaseMode = connectionReleaseMode;
3945
this.autoClose = autoClose;
4046
}
4147

@@ -202,13 +208,20 @@ public void AfterTransaction()
202208
transaction = null;
203209
}
204210

211+
public void AfterStatement()
212+
{
213+
// TODO
214+
}
215+
205216
#region Serialization
206217

207218
private ConnectionManager(SerializationInfo info, StreamingContext context)
208219
{
209220
this.connectForNextOperation = false;
210221
this.autoClose = info.GetBoolean("autoClose");
211222
this.session = (ISessionImplementor) info.GetValue("session", typeof (ISessionImplementor));
223+
this.connectionReleaseMode =
224+
(ConnectionReleaseMode) info.GetValue("connectionReleaseMode", typeof (ConnectionReleaseMode));
212225
}
213226

214227
[SecurityPermission(SecurityAction.LinkDemand,
@@ -217,6 +230,7 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
217230
{
218231
info.AddValue("autoClose", autoClose);
219232
info.AddValue("session", session, typeof(ISessionImplementor));
233+
info.AddValue("connectionReleaseMode", connectionReleaseMode, typeof(ConnectionReleaseMode));
220234
}
221235

222236
#endregion
@@ -250,5 +264,31 @@ public void AfterNonTransactionalQuery(bool success)
250264
log.Debug("after autocommit");
251265
session.AfterTransactionCompletion(success);
252266
}
267+
268+
private bool IsAfterTransactionRelease
269+
{
270+
get { return connectionReleaseMode == ConnectionReleaseMode.AfterTransaction; }
271+
}
272+
273+
private bool IsOnCloseRelease
274+
{
275+
get { return connectionReleaseMode == ConnectionReleaseMode.OnClose; }
276+
}
277+
278+
private bool IsAggressiveRelease
279+
{
280+
get
281+
{
282+
if (connectionReleaseMode == ConnectionReleaseMode.AfterStatement)
283+
{
284+
return true;
285+
}
286+
else if (connectionReleaseMode == ConnectionReleaseMode.AfterTransaction)
287+
{
288+
return !IsInActiveTransaction;
289+
}
290+
return false;
291+
}
292+
}
253293
}
254294
}

Diff for: src/NHibernate/Impl/EnumerableImpl.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void PostMoveNext( bool hasNext )
7373
{
7474
log.Debug( "exhausted results" );
7575
_currentResult = null;
76-
_sess.Batcher.CloseQueryCommand( _cmd, _reader );
76+
_sess.Batcher.CloseCommand( _cmd, _reader );
7777
}
7878
else
7979
{
@@ -216,7 +216,7 @@ protected virtual void Dispose(bool isDisposing)
216216
if( _hasNext )
217217
{
218218
_currentResult = null;
219-
_sess.Batcher.CloseQueryCommand( _cmd, _reader );
219+
_sess.Batcher.CloseCommand( _cmd, _reader );
220220
}
221221
}
222222

Diff for: src/NHibernate/Impl/MultiQueryImpl.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ protected ArrayList DoList()
468468
}
469469
finally
470470
{
471-
session.Batcher.CloseQueryCommand(command, reader);
471+
session.Batcher.CloseCommand(command, reader);
472472
}
473473
for (int i = 0; i < translators.Count; i++)
474474
{

Diff for: src/NHibernate/Impl/SessionFactoryImpl.cs

+4-15
Original file line numberDiff line numberDiff line change
@@ -530,35 +530,24 @@ public IFilterTranslator GetFilter(string filterString, string collectionRole, b
530530
return filter;
531531
}
532532

533-
private ISession OpenSession(IDbConnection connection, bool autoClose, long timestamp, IInterceptor interceptor)
533+
private ISession OpenSession(IDbConnection connection, bool autoClose, long timestamp, IInterceptor interceptor, ConnectionReleaseMode connectionReleaseMode)
534534
{
535-
return new SessionImpl(connection, this, autoClose, timestamp, interceptor);
535+
return new SessionImpl(connection, this, autoClose, timestamp, interceptor, connectionReleaseMode);
536536
}
537537

538-
/// <summary>
539-
///
540-
/// </summary>
541-
/// <param name="connection"></param>
542-
/// <param name="interceptor"></param>
543-
/// <returns></returns>
544538
public ISession OpenSession(IDbConnection connection, IInterceptor interceptor)
545539
{
546540
// specify false for autoClose because the user has passed in an IDbConnection
547541
// and they assume responsibility for it.
548-
return OpenSession(connection, false, long.MinValue, interceptor);
542+
return OpenSession(connection, false, long.MinValue, interceptor, ConnectionReleaseMode.OnClose);
549543
}
550544

551-
/// <summary>
552-
///
553-
/// </summary>
554-
/// <param name="interceptor"></param>
555-
/// <returns></returns>
556545
public ISession OpenSession(IInterceptor interceptor)
557546
{
558547
long timestamp = Timestamper.Next();
559548
// specify true for autoClose because NHibernate has responsibility for
560549
// the IDbConnection.
561-
return OpenSession(null, true, timestamp, interceptor);
550+
return OpenSession(null, true, timestamp, interceptor, ConnectionReleaseMode.OnClose);
562551
}
563552

564553
/// <summary>

Diff for: src/NHibernate/Impl/SessionImpl.cs

+8-22
Original file line numberDiff line numberDiff line change
@@ -397,24 +397,20 @@ void IDeserializationCallback.OnDeserialization(object sender)
397397

398398
#endregion
399399

400-
/// <summary>
401-
///
402-
/// </summary>
403-
/// <param name="connection"></param>
404-
/// <param name="factory"></param>
405-
/// <param name="autoClose"></param>
406-
/// <param name="timestamp"></param>
407-
/// <param name="interceptor"></param>
408-
internal SessionImpl(IDbConnection connection, SessionFactoryImpl factory, bool autoClose, long timestamp, IInterceptor interceptor)
400+
internal SessionImpl(
401+
IDbConnection connection,
402+
SessionFactoryImpl factory,
403+
bool autoClose,
404+
long timestamp,
405+
IInterceptor interceptor,
406+
ConnectionReleaseMode connectionReleaseMode)
409407
{
410408
if (interceptor == null)
411409
throw new ArgumentNullException("interceptor", "The interceptor can not be null");
412410

413-
this.connectionManager = new ConnectionManager(this, connection, autoClose);
411+
this.connectionManager = new ConnectionManager(this, connection, connectionReleaseMode, autoClose);
414412
this.interceptor = interceptor;
415-
416413
this.timestamp = timestamp;
417-
418414
this.factory = factory;
419415

420416
entitiesByKey = new Hashtable(50);
@@ -554,11 +550,6 @@ private void Cleanup()
554550
}
555551
}
556552

557-
/// <summary>
558-
///
559-
/// </summary>
560-
/// <param name="obj"></param>
561-
/// <returns></returns>
562553
public LockMode GetCurrentLockMode(object obj)
563554
{
564555
CheckIsOpen();
@@ -1568,11 +1559,6 @@ public void SaveOrUpdate(object obj)
15681559
}
15691560
}
15701561

1571-
/// <summary>
1572-
///
1573-
/// </summary>
1574-
/// <param name="obj"></param>
1575-
/// <param name="id"></param>
15761562
public void Update(object obj, object id)
15771563
{
15781564
CheckIsOpen();

Diff for: src/NHibernate/Loader/Loader.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ private IList DoQuery(
471471
}
472472
finally
473473
{
474-
session.Batcher.CloseQueryCommand( st, rs );
474+
session.Batcher.CloseCommand( st, rs );
475475
}
476476

477477
InitializeEntitiesAndCollections( hydratedObjects, rs, session );
@@ -1282,13 +1282,13 @@ protected virtual IDbCommand PrepareQueryCommand(
12821282
}
12831283
catch( HibernateException )
12841284
{
1285-
session.Batcher.CloseQueryCommand( command, null );
1285+
session.Batcher.CloseCommand( command, null );
12861286
throw;
12871287
}
12881288
catch( Exception sqle )
12891289
{
12901290
ADOExceptionReporter.LogExceptions( sqle );
1291-
session.Batcher.CloseQueryCommand( command, null );
1291+
session.Batcher.CloseCommand( command, null );
12921292
throw;
12931293
}
12941294

@@ -1388,7 +1388,7 @@ protected IDataReader GetResultSet( IDbCommand st, RowSelection selection, ISess
13881388
catch( Exception sqle )
13891389
{
13901390
ADOExceptionReporter.LogExceptions( sqle );
1391-
session.Batcher.CloseQueryCommand( st, rs );
1391+
session.Batcher.CloseCommand( st, rs );
13921392
throw;
13931393
}
13941394
}

Diff for: src/NHibernate/NHibernate-2.0.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@
504504
<Compile Include="Collection\PersistentList.cs" />
505505
<Compile Include="Collection\PersistentMap.cs" />
506506
<Compile Include="Collection\PersistentSet.cs" />
507+
<Compile Include="ConnectionReleaseMode.cs" />
507508
<Compile Include="Context\ICurrentSessionContext.cs" />
508509
<Compile Include="Context\ManagedWebSessionContext.cs" />
509510
<Compile Include="DebugHelpers\DictionaryProxy.cs" />

Diff for: src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ public object GetCurrentVersion(object id, ISessionImplementor session)
14131413
}
14141414
finally
14151415
{
1416-
session.Batcher.CloseQueryCommand(st, rs);
1416+
session.Batcher.CloseCommand(st, rs);
14171417
}
14181418
}
14191419
catch (HibernateException)

0 commit comments

Comments
 (0)