Skip to content

NH-3564 - Fix possible second level cache key calculation issue. #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3564/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.NHSpecificTest.NH3564
{
public class MyDummyCache : ICache
{
private IDictionary hashtable = new Hashtable();
private readonly string regionName;

public MyDummyCache(string regionName)
{
this.regionName = regionName;
}

public object Get(object key)
{
return hashtable[KeyAsString(key)];
}

public void Put(object key, object value)
{
hashtable[KeyAsString(key)] = value;
}

public void Remove(object key)
{
hashtable.Remove(KeyAsString(key));
}

public void Clear()
{
hashtable.Clear();
}

public void Destroy()
{
}

public void Lock(object key)
{
// local cache, so we use synchronization
}

public void Unlock(object key)
{
// local cache, so we use synchronization
}

public long NextTimestamp()
{
return Timestamper.Next();
}

public int Timeout
{
get { return Timestamper.OneMs*60000; }
}

public string RegionName
{
get { return regionName; }
}

private string KeyAsString(object key)
{
//This is how MemCached provider uses key.
return string.Format("{0}@{1}", RegionName, (key == null ? string.Empty : key.ToString()));
}
}

public class MyDummyCacheProvider : ICacheProvider
{
public ICache BuildCache(string regionName, IDictionary<string, string> properties)
{
return new MyDummyCache(regionName);
}

public long NextTimestamp()
{
return Timestamper.Next();
}

public void Start(IDictionary<string, string> properties)
{
}

public void Stop()
{
}
}

class Person
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual DateTime DateOfBirth { get; set; }
}

public class FixtureByCode : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Person>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name);
rc.Property(x => x.DateOfBirth, pm =>
{
pm.Type(NHibernateUtil.Timestamp);
});
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.CacheProvider, typeof (MyDummyCacheProvider).AssemblyQualifiedName);
configuration.SetProperty(Environment.UseQueryCache, "true");
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Save(new Person {Name = "Bob", DateOfBirth = new DateTime(2015, 4, 22)});
session.Save(new Person {Name = "Sally", DateOfBirth = new DateTime(2014, 4, 22)});

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}

[Test]
public void ShouldUseDifferentCache()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var bob = session.Query<Person>().Cacheable().Where(e => e.DateOfBirth == new DateTime(2015, 4, 22)).ToList();
var sally = session.Query<Person>().Cacheable().Where(e => e.DateOfBirth == new DateTime(2014, 4, 22)).ToList();

Assert.That(bob, Has.Count.EqualTo(1));
Assert.That(bob[0].Name, Is.EqualTo("Bob"));

Assert.That(sally, Has.Count.EqualTo(1));
Assert.That(sally[0].Name, Is.EqualTo("Sally"));
}
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
<Compile Include="Linq\ByMethod\DistinctTests.cs" />
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
<Compile Include="NHSpecificTest\NH3564\FixtureByCode.cs" />
<Compile Include="NHSpecificTest\NH3583\Entity.cs" />
<Compile Include="NHSpecificTest\NH3583\AutoFlushFixture.cs" />
<Compile Include="NHSpecificTest\NH3372\Entity.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Type/TimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override int GetHashCode(object x, EntityMode entityMode)

public override string ToString(object val)
{
return ((DateTime) val).ToShortTimeString();
return ((DateTime) val).ToString("T");
}

public object StringToObject(string xml)
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Type/TimestampType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override string Name

public override string ToString(object val)
{
return ((DateTime) val).ToShortTimeString();
return ((DateTime) val).ToString("O");
}

public override object FromStringValue(string xml)
Expand Down