From eb59b8f26ee443cd072a525c8b306fde1b35dfd2 Mon Sep 17 00:00:00 2001 From: Steve Bohlen Date: Thu, 13 Sep 2012 12:39:54 -0400 Subject: [PATCH] SPRNET-1522 added cache key prefix for SimpleDelegatingSessionFactory cache access --- .../SimpleDelegatingSessionFactory.cs | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Spring/Spring.Data.NHibernate/Data/NHibernate/SimpleDelegatingSessionFactory.cs b/src/Spring/Spring.Data.NHibernate/Data/NHibernate/SimpleDelegatingSessionFactory.cs index 0d62faab..c1836aaf 100644 --- a/src/Spring/Spring.Data.NHibernate/Data/NHibernate/SimpleDelegatingSessionFactory.cs +++ b/src/Spring/Spring.Data.NHibernate/Data/NHibernate/SimpleDelegatingSessionFactory.cs @@ -23,6 +23,11 @@ namespace Spring.Data.NHibernate /// public const string CONNECTION_STRING = "SimpleDelegatingSessionFactory.ConnectionString"; + /// + /// Cache region prefix config element name + /// + public const string CACHE_REGION_PREFIX_STRING = "SimpleDelegatingSessionFactory.CacheRegionPrefix"; + private Configuration _configuration; private string _defaultConnectionString; @@ -66,7 +71,7 @@ namespace Spring.Data.NHibernate string connectionString = LogicalThreadContext.GetData(CONNECTION_STRING) as string; System.Diagnostics.Trace.WriteLine(String.Format("{0} = {1}", System.Threading.Thread.CurrentThread.GetHashCode(), connectionString)); - + if (connectionString == null) { connectionString = _defaultConnectionString; @@ -78,6 +83,22 @@ namespace Spring.Data.NHibernate { System.Diagnostics.Trace.WriteLine(System.Threading.Thread.CurrentThread.GetHashCode().ToString() + " = (created) "); + string originalPrefix = string.Empty; + + if (_configuration.Properties.ContainsKey(NhCfg.Environment.CacheRegionPrefix)) + { + originalPrefix = _configuration.Properties[NhCfg.Environment.CacheRegionPrefix]; + } + + string cacheRegionPrefix = LogicalThreadContext.GetData(CACHE_REGION_PREFIX_STRING) as string; + + if (!string.IsNullOrEmpty(cacheRegionPrefix)) + { + _configuration.Properties[NhCfg.Environment.CacheRegionPrefix] = originalPrefix + cacheRegionPrefix; + System.Diagnostics.Trace.WriteLine(String.Format("{0} = (cache region prefix) {1}", System.Threading.Thread.CurrentThread.GetHashCode(), cacheRegionPrefix)); + + } + _configuration.Properties[NhCfg.Environment.ConnectionString] = connectionString; ISessionFactory sessionFactory = _configuration.BuildSessionFactory(); @@ -87,6 +108,10 @@ namespace Spring.Data.NHibernate dbProviderWrapper.DbProvider = (IDbProvider)ContextRegistry.GetContext().GetObject("DbProvider"); } + //Reset the Cache Region Prefix to the original value + // This is so other cache region prefixes are not appended together. + _configuration.Properties[NhCfg.Environment.CacheRegionPrefix] = originalPrefix; + _targetSessionFactories[connectionString] = sessionFactory; } else @@ -95,7 +120,7 @@ namespace Spring.Data.NHibernate ISessionFactory factory = _targetSessionFactories[connectionString] as ISessionFactory; System.Diagnostics.Trace.WriteLine(String.Format("{0} = {1}", System.Threading.Thread.CurrentThread.GetHashCode(), connectionString)); - + return factory; } }