Skip to content

Commit 7504b1f

Browse files
Never cached entities/collections cannot be used in a cacheable query default true it could be disabled via query.throw_never_cached option
1 parent f2a213e commit 7504b1f

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

src/NHibernate/Cfg/Environment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ public static string Version
175175
/// <summary> Should named queries be checked during startup (the default is enabled). </summary>
176176
/// <remarks>Mainly intended for test environments.</remarks>
177177
public const string QueryStartupChecking = "query.startup_check";
178+
/// <summary> Should never cached entity/collecion query cache throws exception(the default is enabled). /// </summary>
179+
public const string QueryThrowNeverCached = "query.throw_never_cached";
178180

179181
/// <summary> Enable statistics collection</summary>
180182
public const string GenerateStatistics = "generate_statistics";

src/NHibernate/Cfg/Settings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public Settings()
139139
/// </summary>
140140
public bool ThrowOnSchemaUpdate { get; internal set; }
141141

142+
public bool QueryThrowNeverCached { get; internal set; }
143+
142144
#region NH specific
143145

144146
public IsolationLevel IsolationLevel { get; internal set; }

src/NHibernate/Cfg/SettingsFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ public Settings BuildSettings(IDictionary<string, string> properties)
292292
bool namedQueryChecking = PropertiesHelper.GetBoolean(Environment.QueryStartupChecking, properties, true);
293293
log.Info("Named query checking : {0}", EnabledDisabled(namedQueryChecking));
294294
settings.IsNamedQueryStartupCheckingEnabled = namedQueryChecking;
295-
295+
296+
bool queryThrowNeverCached = PropertiesHelper.GetBoolean(Environment.QueryThrowNeverCached, properties, true);
297+
log.Info("Never cached entities/collections query cache throws exception : {0}", EnabledDisabled(queryThrowNeverCached));
298+
settings.QueryThrowNeverCached = queryThrowNeverCached;
299+
296300
// Not ported - settings.StatementFetchSize = statementFetchSize;
297301
// Not ported - ScrollableResultSetsEnabled
298302
// Not ported - GetGeneratedKeysEnabled

src/NHibernate/Loader/Loader.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,12 +1838,19 @@ internal bool IsCacheable(QueryParameters queryParameters, bool supportsQueryCac
18381838
bool isCacheable = Factory.Settings.IsQueryCacheEnabled && queryParameters.Cacheable;
18391839
if (isCacheable && !supportsQueryCache)
18401840
{
1841-
throw new QueryException(
1842-
$"Never cached entities/collections: {string.Join(", ", persisters.Where(o => !o.SupportsQueryCache).Select(o => o.Name))} " +
1843-
$"cannot be used in a cacheable query.");
1841+
if (Factory.Settings.QueryThrowNeverCached)
1842+
{
1843+
throw new QueryException(
1844+
$"Never cached entities/collections: {string.Join(", ", persisters.Where(o => !o.SupportsQueryCache).Select(o => o.Name))} " +
1845+
$"cannot be used in a cacheable query.");
1846+
}
1847+
else if (Log.IsWarnEnabled())
1848+
{
1849+
Log.Warn("Never cached entities/collections: {0} cannot be used in a cacheable query.", string.Join(", ", persisters.Where(o => !o.SupportsQueryCache).Select(o => o.Name)));
1850+
}
18441851
}
18451852

1846-
return isCacheable;
1853+
return isCacheable && supportsQueryCache;
18471854
}
18481855

18491856
private IList ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)

0 commit comments

Comments
 (0)