SPRNET-1522 added cache key prefix for SimpleDelegatingSessionFactory cache access

This commit is contained in:
Steve Bohlen
2012-09-13 12:39:54 -04:00
parent 0ccf893341
commit eb59b8f26e

View File

@@ -23,6 +23,11 @@ namespace Spring.Data.NHibernate
/// </summary> /// </summary>
public const string CONNECTION_STRING = "SimpleDelegatingSessionFactory.ConnectionString"; public const string CONNECTION_STRING = "SimpleDelegatingSessionFactory.ConnectionString";
/// <summary>
/// Cache region prefix config element name
/// </summary>
public const string CACHE_REGION_PREFIX_STRING = "SimpleDelegatingSessionFactory.CacheRegionPrefix";
private Configuration _configuration; private Configuration _configuration;
private string _defaultConnectionString; private string _defaultConnectionString;
@@ -78,6 +83,22 @@ namespace Spring.Data.NHibernate
{ {
System.Diagnostics.Trace.WriteLine(System.Threading.Thread.CurrentThread.GetHashCode().ToString() + " = (created) "); 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; _configuration.Properties[NhCfg.Environment.ConnectionString] = connectionString;
ISessionFactory sessionFactory = _configuration.BuildSessionFactory(); ISessionFactory sessionFactory = _configuration.BuildSessionFactory();
@@ -87,6 +108,10 @@ namespace Spring.Data.NHibernate
dbProviderWrapper.DbProvider = (IDbProvider)ContextRegistry.GetContext().GetObject("DbProvider"); 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; _targetSessionFactories[connectionString] = sessionFactory;
} }
else else