使用memcached和随机无效强制转换的nHibernate二级缓存

时间:2022-02-02 03:54:21

I have an MVC3 application using nHibernate and memcached as the second level cache provider. We've been intermittently (but much more frequently lately) getting weird casting issues. It happens randomly and invalidating the memcached cache will fix the problem for while.

我有一个使用nHibernate和memcached作为二级缓存提供程序的MVC3应用程序。我们断断续续地(但最近更频繁地)出现了奇怪的铸造问题。它会随机发生并使memcached缓存无效,从而解决问题。

It only happens in our Production environment because we don't run memcached in our other environments. However I run memcached locally and have tried to get this to occur locally with no luck.

它只发生在我们的生产环境中,因为我们没有在其他环境中运行缓存的memcached。然而,我在本地运行memcached,并试图让它在本地发生,但运气不佳。

We are using memcached 1.2.6 on Windows. Here's the stack trace. I know it's not going to be enough information to determine anything but if anyone has any ideas about how I could debug this that would be appreciated. I'm trying to get remote debugging on our Production machine but it's a busy time of year and risky.

我们在Windows上使用memcached 1.2.6。这是堆栈跟踪。我知道这不会是足够的信息来决定任何事情,但是如果有人知道我如何调试这个问题,我将非常感激。我试着在我们的生产机器上进行远程调试,但这是一年中的繁忙时间和风险。

Unable to cast object of type 'System.Int32' to type 'System.String'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.]
   (Object , Object[] , SetterCallback ) +4270
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +80

[PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of MyApplication.Business.Data.Program]
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +207
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values) +97
   NHibernate.Cache.Entry.CacheEntry.Assemble(Object[] values, Object result, Object id, IEntityPersister persister, IInterceptor interceptor, ISessionImplementor session) +306
   NHibernate.Cache.Entry.CacheEntry.Assemble(Object instance, Object id, IEntityPersister persister, IInterceptor interceptor, ISessionImplementor session) +147
   NHibernate.Event.Default.DefaultLoadEventListener.AssembleCacheEntry(CacheEntry entry, Object id, IEntityPersister persister, LoadEvent event) +434
   NHibernate.Event.Default.DefaultLoadEventListener.LoadFromSecondLevelCache(LoadEvent event, IEntityPersister persister, LoadType options) +800
   NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) +560
   NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) +229
   NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) +438
   NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType) +943
   NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType) +99
   NHibernate.Impl.SessionImpl.Get(String entityName, Object id) +117
   NHibernate.Impl.SessionImpl.Get(Object id) +70
   MyApplication.Business.Repositories.Repository`1.Get(Object id) +148

2 个解决方案

#1


3  

We've been encountering the same issue and we have pinpointed the problem to invalid cache entries in the second level cache. In our case the problem arises when we add or remove columns/properties from tables/entities that are cached in the second level cache and deploy the change. The code expects five columns (for argument's sake) whereas the cache stores six columns.

我们已经遇到了同样的问题,并且我们已经在二级缓存中找到了无效的缓存条目。在我们的例子中,当我们添加或移除在二级缓存中缓存并部署更改的表/实体的列/属性时,问题就出现了。代码需要5列(为了讨论),而缓存存储6列。

One strategy that we employed in our repository is to catch the exception and invalidate the cache. We then retry the get.

我们在存储库中使用的一种策略是捕获异常并使缓存无效。然后我们重新尝试get。

    public override T Get(int id)
    {
        try
        {
            return base.Get(id);
        }
        catch (InvalidCastException)
        {
            _sessionFactory.EvictEntity(typeof (T).Name);
            return Get(id);
        }
    }

Hope this helps!

希望这可以帮助!

#2


0  

Most probably this is caused by different dataset returned by the queries on the production environment and your local. The object mapper is getting different data than what its expecting. When trying to convert the data then you are getting these exceptions.

最可能的原因是生产环境和本地查询返回的不同数据集。对象映射器获得的数据与预期的数据不同。当尝试转换数据时,您将获得这些异常。

#1


3  

We've been encountering the same issue and we have pinpointed the problem to invalid cache entries in the second level cache. In our case the problem arises when we add or remove columns/properties from tables/entities that are cached in the second level cache and deploy the change. The code expects five columns (for argument's sake) whereas the cache stores six columns.

我们已经遇到了同样的问题,并且我们已经在二级缓存中找到了无效的缓存条目。在我们的例子中,当我们添加或移除在二级缓存中缓存并部署更改的表/实体的列/属性时,问题就出现了。代码需要5列(为了讨论),而缓存存储6列。

One strategy that we employed in our repository is to catch the exception and invalidate the cache. We then retry the get.

我们在存储库中使用的一种策略是捕获异常并使缓存无效。然后我们重新尝试get。

    public override T Get(int id)
    {
        try
        {
            return base.Get(id);
        }
        catch (InvalidCastException)
        {
            _sessionFactory.EvictEntity(typeof (T).Name);
            return Get(id);
        }
    }

Hope this helps!

希望这可以帮助!

#2


0  

Most probably this is caused by different dataset returned by the queries on the production environment and your local. The object mapper is getting different data than what its expecting. When trying to convert the data then you are getting these exceptions.

最可能的原因是生产环境和本地查询返回的不同数据集。对象映射器获得的数据与预期的数据不同。当尝试转换数据时,您将获得这些异常。