Cachekey key new cachekeyfactory statement sql new

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: are not equal. CacheKey.Equals(object) override can return true when the parameters have the same HashCode but are not equal. This can cause false cache hits. Only an issue for complex parameters. Failing NUnit test: ---- UNIT TEST BEGIN ---- using IBatisNet.DataMapper; using IBatisNet.DataMapper.TypeHandlers; using NUnit.Framework; namespace IBatisNet.DataAccess { [TestFixture] public class CacheKeyTests { private const long A_LONG = 1L; private const long ANOTHER_LONG_WITH_SAME_HASHCODE = -9223372034707292159; public CacheKeyTests() { } [Test] public void ShouldNotBeConsideredEqualWhenParametersHaveTheSameHashCodeButAreNotEqual() { TypeHandlerFactory factory = new TypeHandlerFactory(); // Two cache keys are equal except for the parameter. CacheKey key = new CacheKey(factory, "STATEMENT", "SQL", new TestClass(A_LONG), new string {"AProperty"}, 0, 0, CacheKeyType.Object); CacheKey aDifferentKey = new CacheKey(factory, "STATEMENT", "SQL", new TestClass(ANOTHER_LONG_WITH_SAME_HASHCODE), new string {"AProperty"}, 0, 0, CacheKeyType.Object); Assert.IsFalse(aDifferentKey.Equals(key)); // should not be equal. } } public class TestClass { private long aProperty; public TestClass(long aProperty) { this.aProperty = aProperty; } public long AProperty { get { return aProperty; } set { aProperty = value; } } } } ---- UNIT TEST END ---- The test passes if you replace this line in CacheKey.Equals(): if (_hashCode != cacheKey._hashCode) return false; with something like this: if (this._parameter == null && cacheKey._parameter != null) return false; if (this._parameter != null && cacheKey._parameter == null) return false; if (!this._parameter.Equals(cacheKey._parameter)) return false; CacheKey.Equals(object)/HashCode issue still a problem (was IBATISNET-118) The fix for IBATISNET-118 does not address the issue (just hides it in one particular instance). The issue is that you can not use HashCodes to determine...
View Full Document

This document was uploaded on 10/12/2012.

Ask a homework question - tutors are online