【问题标题】:nHibernate second-level cache with memcached and random invalid castsnHibernate 具有 memcached 和随机无效强制转换的二级缓存
【发布时间】:2013-03-12 16:30:33
【问题描述】:

我有一个 MVC3 应用程序,它使用 nHibernatememcached 作为二级缓存提供程序。我们间歇性地(但最近更频繁地)遇到奇怪的铸造问题。它是随机发生的,使 memcached 缓存失效将暂时解决问题。

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

我们在 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

【问题讨论】:

  • 什么是 MyApplication.Business.Data.Program ?它总是相同的异常吗? String=>Int32 让我(随机)想到 Enum,或者 query.substitutions,隐式转换……
  • 总是同样的例外。程序是 nHibernate 实体。一旦错误开始,它就会一直存在,直到缓存失效。然而,最初的错误似乎是随机发生的。
  • 您能详细说明一下您的设置吗?什么版本的nHibernate?哪个缓存提供者? memcached有哪些配置?等
  • 如果没有更多细节,我认为您将很难在这里找到解决方案。
  • 我只能说,我们已经在一个非常繁忙的生产环境中运行来自couchbase.com/download 的 1.8.1 发行版已经有一段时间了,并且从未遇到过问题。

标签: asp.net-mvc-3 nhibernate caching memcached


【解决方案1】:

我们遇到了同样的问题,我们已将问题定位为二级缓存中的无效缓存条目。在我们的例子中,当我们从缓存在二级缓存中的表/实体中添加或删除列/属性并部署更改时,就会出现问题。代码需要五列(为了参数),而缓存存储六列。

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

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

希望这会有所帮助!

【讨论】:

  • 请注意,您也可以使用以下内容:stackoverflow.com/questions/2660714/… 使缓存无效。我们在后端管理面板中公开它,并在更改存储在二级缓存中的实体的部署后运行它。
  • 我们已经切换到一个新平台,但我会接受这个答案,因为它本来是我可以实际尝试的!
【解决方案2】:

这很可能是由于生产环境和本地查询返回的数据集不同造成的。对象映射器获取的数据与预期不同。尝试转换数据时,您会遇到这些异常。

【讨论】:

    猜你喜欢
    • 2011-03-08
    • 2013-02-26
    • 2011-05-13
    • 2010-10-13
    • 2011-09-02
    • 2014-08-27
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多