Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
DataMapper 1.2.1
-
None
Description
When caching is enabled and the sqlMap.QueryForObject method returns a null value, that null value is not cached. Subsequent calls to sqlMap.QueryForObject always miss the cache and query the database. For more information see the Java ticket of the same issue:
http://issues.apache.org/jira/browse/IBATIS-174
One solution is to define a special marker object in CacheModel.cs:
/// <summary>
/// Marked object to allow null objects to be cached
/// </summary>
[NonSerialized]
public readonly static object NULL_OBJECT = new Object();
and insert that value into the cache when a null value is returned from the initial query.
The code to retrieve an item from the cache in MappedStatement.cs:
if (obj == null)
{ obj = RunQueryForObject(request, session, parameterObject, resultObject); _statement.CacheModel[key] = obj; }could be changed to this:
// check if this query has alreay been run
if (obj == CacheModel.NULL_OBJECT)
else if (obj == null)
{
obj = RunQueryForObject(request, session, parameterObject, resultObject);
if (obj == null)
else
{ // query returned a normal, non-null value _statement.CacheModel[key] = obj; }}