@@ -469,7 +469,11 @@ private ICacheConcurrencyStrategy GetCacheConcurrencyStrategy(
469
469
470
470
public EventListeners EventListeners
471
471
{
472
- get { return eventListeners ; }
472
+ get
473
+ {
474
+ CheckNotClosed ( ) ;
475
+ return eventListeners ;
476
+ }
473
477
}
474
478
475
479
#region IObjectReference Members
@@ -513,6 +517,7 @@ public object GetRealObject(StreamingContext context)
513
517
514
518
public ISessionBuilder WithOptions ( )
515
519
{
520
+ CheckNotClosed ( ) ;
516
521
return new SessionBuilderImpl ( this ) ;
517
522
}
518
523
@@ -563,6 +568,7 @@ public ISession OpenSession(DbConnection connection, bool flushBeforeCompletionE
563
568
564
569
public IStatelessSessionBuilder WithStatelessOptions ( )
565
570
{
571
+ CheckNotClosed ( ) ;
566
572
return new StatelessSessionBuilderImpl ( this ) ;
567
573
}
568
574
@@ -580,6 +586,7 @@ public IStatelessSession OpenStatelessSession(DbConnection connection)
580
586
581
587
public IEntityPersister GetEntityPersister ( string entityName )
582
588
{
589
+ CheckNotClosed ( ) ;
583
590
IEntityPersister value ;
584
591
if ( entityPersisters . TryGetValue ( entityName , out value ) == false )
585
592
throw new MappingException ( "No persister for: " + entityName ) ;
@@ -588,13 +595,15 @@ public IEntityPersister GetEntityPersister(string entityName)
588
595
589
596
public IEntityPersister TryGetEntityPersister ( string entityName )
590
597
{
598
+ CheckNotClosed ( ) ;
591
599
IEntityPersister result ;
592
600
entityPersisters . TryGetValue ( entityName , out result ) ;
593
601
return result ;
594
602
}
595
603
596
604
public ICollectionPersister GetCollectionPersister ( string role )
597
605
{
606
+ CheckNotClosed ( ) ;
598
607
ICollectionPersister value ;
599
608
if ( collectionPersisters . TryGetValue ( role , out value ) == false )
600
609
throw new MappingException ( "Unknown collection role: " + role ) ;
@@ -863,6 +872,14 @@ public void Dispose()
863
872
Close ( ) ;
864
873
}
865
874
875
+ private void CheckNotClosed ( )
876
+ {
877
+ if ( isClosed )
878
+ {
879
+ throw new ObjectDisposedException ( $ "Session factory { Name } with id { Uuid } ") ;
880
+ }
881
+ }
882
+
866
883
/// <summary>
867
884
/// Closes the session factory, releasing all held resources.
868
885
/// <list>
@@ -1105,6 +1122,7 @@ public UpdateTimestampsCache UpdateTimestampsCache
1105
1122
public IDictionary < string , ICache > GetAllSecondLevelCacheRegions ( )
1106
1123
#pragma warning restore 618
1107
1124
{
1125
+ CheckNotClosed ( ) ;
1108
1126
return
1109
1127
_allCacheRegions
1110
1128
// ToArray creates a moment in time snapshot
@@ -1119,6 +1137,7 @@ public IDictionary<string, ICache> GetAllSecondLevelCacheRegions()
1119
1137
public ICache GetSecondLevelCacheRegion ( string regionName )
1120
1138
#pragma warning restore 618
1121
1139
{
1140
+ CheckNotClosed ( ) ;
1122
1141
_allCacheRegions . TryGetValue ( regionName , out var result ) ;
1123
1142
return result ;
1124
1143
}
@@ -1145,7 +1164,12 @@ public IStatisticsImplementor StatisticsImplementor
1145
1164
1146
1165
public IQueryCache QueryCache
1147
1166
{
1148
- get { return queryCache ; }
1167
+ get
1168
+ {
1169
+ if ( queryCache != null )
1170
+ CheckNotClosed ( ) ;
1171
+ return queryCache ;
1172
+ }
1149
1173
}
1150
1174
1151
1175
public IQueryCache GetQueryCache ( string cacheRegion )
@@ -1159,6 +1183,7 @@ public IQueryCache GetQueryCache(string cacheRegion)
1159
1183
return null ;
1160
1184
}
1161
1185
1186
+ CheckNotClosed ( ) ;
1162
1187
// The factory may be run concurrently by threads trying to get the same region.
1163
1188
// But the GetOrAdd will yield the same lazy for all threads, so only one will
1164
1189
// initialize. https://stackoverflow.com/a/31637510/1178314
@@ -1175,6 +1200,7 @@ public void EvictQueries()
1175
1200
// NH Different implementation
1176
1201
if ( queryCache != null )
1177
1202
{
1203
+ CheckNotClosed ( ) ;
1178
1204
queryCache . Clear ( ) ;
1179
1205
if ( queryCaches . Count == 0 )
1180
1206
{
@@ -1193,6 +1219,7 @@ public void EvictQueries(string cacheRegion)
1193
1219
{
1194
1220
if ( settings . IsQueryCacheEnabled )
1195
1221
{
1222
+ CheckNotClosed ( ) ;
1196
1223
if ( queryCaches . TryGetValue ( cacheRegion , out var currentQueryCache ) )
1197
1224
{
1198
1225
currentQueryCache . Value . Clear ( ) ;
0 commit comments