Skip to content

Commit 325c9cb

Browse files
committed
Disable caching for DML queries
1 parent 3441fb3 commit 325c9cb

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

src/NHibernate/Engine/Query/QueryPlanCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public IQueryExpressionPlan GetHQLQueryPlan(IQueryExpression queryExpression, bo
6262
}
6363
plan = new QueryExpressionPlan(queryExpression, shallow, enabledFilters, factory);
6464
// 6.0 TODO: add "CanCachePlan { get; }" to IQueryExpression interface
65-
if (!(queryExpression is NhLinqExpression linqExpression) || linqExpression.CanCachePlan)
65+
if (!(queryExpression is ICacheableQueryExpression linqExpression) || linqExpression.CanCachePlan)
6666
planCache.Put(key, plan);
6767
else
6868
log.Debug("Query plan not cacheable");
@@ -117,7 +117,7 @@ public IQueryExpressionPlan GetFilterQueryPlan(IQueryExpression queryExpression,
117117
log.Debug("unable to locate collection-filter query plan in cache; generating ({0} : {1})", collectionRole, queryExpression.Key);
118118
plan = new FilterQueryPlan(queryExpression, collectionRole, shallow, enabledFilters, factory);
119119
// 6.0 TODO: add "CanCachePlan { get; }" to IQueryExpression interface
120-
if (!(queryExpression is NhLinqExpression linqExpression) || linqExpression.CanCachePlan)
120+
if (!(queryExpression is ICacheableQueryExpression linqExpression) || linqExpression.CanCachePlan)
121121
planCache.Put(key, plan);
122122
else
123123
log.Debug("Query plan not cacheable");

src/NHibernate/IQueryExpression.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55

66
namespace NHibernate
77
{
8+
//TODO 6.0: Merge into IQueryExpression
9+
internal interface ICacheableQueryExpression
10+
{
11+
bool CanCachePlan { get; }
12+
}
13+
814
public interface IQueryExpression
915
{
1016
IASTNode Translate(ISessionFactoryImplementor sessionFactory, bool filter);
1117
string Key { get; }
1218
System.Type Type { get; }
1319
IList<NamedParameterDescriptor> ParameterDescriptors { get; }
1420
}
15-
}
21+
}

src/NHibernate/Impl/ExpressionQueryImpl.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public override object[] ValueArray()
150150
}
151151
}
152152

153-
internal class ExpandedQueryExpression : IQueryExpression
153+
internal class ExpandedQueryExpression : IQueryExpression, ICacheableQueryExpression
154154
{
155155
private readonly IASTNode _tree;
156156

@@ -160,6 +160,7 @@ public ExpandedQueryExpression(IQueryExpression queryExpression, IASTNode tree,
160160
Key = key;
161161
Type = queryExpression.Type;
162162
ParameterDescriptors = queryExpression.ParameterDescriptors;
163+
CanCachePlan = (queryExpression as ICacheableQueryExpression)?.CanCachePlan ?? true;
163164
}
164165

165166
#region IQueryExpression Members
@@ -176,6 +177,8 @@ public IASTNode Translate(ISessionFactoryImplementor sessionFactory, bool filter
176177
public IList<NamedParameterDescriptor> ParameterDescriptors { get; private set; }
177178

178179
#endregion
180+
181+
public bool CanCachePlan { get; }
179182
}
180183

181184
internal class ParameterExpander

src/NHibernate/Linq/NhLinqDmlExpression.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public NhLinqDmlExpression(QueryMode queryMode, Expression expression, ISessionF
1717
{
1818
Key = $"{queryMode.ToString().ToUpperInvariant()} {Key}";
1919
QueryMode = queryMode;
20+
21+
//For DML queries parameters check doesn't really work (queries are either not cached or cached incorrectly - see GH-2222 and GH-2298)
22+
//So for now disable caching for DML queries
23+
CanCachePlan = false;
2024
}
2125
}
2226
}

src/NHibernate/Linq/NhLinqExpression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
namespace NHibernate.Linq
1313
{
14-
public class NhLinqExpression : IQueryExpression
14+
public class NhLinqExpression : IQueryExpression, ICacheableQueryExpression
1515
{
1616
public string Key { get; protected set; }
1717

18-
public bool CanCachePlan { get; private set; } = true;
18+
public bool CanCachePlan { get; protected set; } = true;
1919

2020
public System.Type Type { get; private set; }
2121

0 commit comments

Comments
 (0)