Alachisoft / NCache

NCache: Highly Scalable Distributed Cache for .NET
http://www.alachisoft.com
Apache License 2.0
647 stars 123 forks source link

NHibernate.Cache.CacheException: 'Unable to acquire lock on the key provided.' #53

Closed andrevoltolini closed 4 years ago

andrevoltolini commented 4 years ago

Hello, I have successfully used NCache 4.9, I am now upgraded to version 5.0.2, but integration with NHibernate is not working as in the previous version. I am getting the following error:

NHibernate.Cache.CacheException: 'Unable to acquire lock on the key provided.'

This exception was originally thrown at this call stack: Alachisoft.NCache.Integrations.NHibernate.Cache.NCache.Lock(object) NHibernate.Cache.ObsoleteCacheWrapper.Lock(object)
NHibernate.Cache.ReadWriteCache.Put(NHibernate.Cache.CacheKey, obje NHibernate.Engine.TwoPhaseLoad.InitializeEntity(object, bool, NHibe NHibernate.Loader.Loader.InitializeEntitiesAndCollections(System.Co NHibernate.Loader.Loader.DoQuery(NHibernate.Engine.ISessionImplemen NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(NHi NHibernate.Loader.Loader.LoadEntity(NHibernate.Engine.ISessionImple NHibernate.Loader.Entity.AbstractEntityLoader.Load(NHibernate.Engin NHibernate.Loader.Entity.AbstractEntityLoader.Load(object, object,

Could anyone help me?

Thanks.

Mark-NCache commented 4 years ago

Hi @andrevoltolini. I'm going to try and help you with this. I am listing down locking behavior and based on this you could be getting this exception

Here is a code snippet from our NCache NHibernate integration source code and you can see that error is thrown only when you get null for locking API used here.

public void Lock(object key)
       {
           if (key == null)
               throw new ArgumentNullException("key", "null key not allowed");
           string cacheKey = ConfigurationManager.Instance.GetCacheKey(key);
           Alachisoft.NCache.Client.LockHandle lockHandle = new Alachisoft.NCache.Client.LockHandle();
           try
           {
               if (!_cacheHandler.Cache.Lock(cacheKey, TimeSpan.MaxValue, out lockHandle))
               {
                   throw new CacheException("Unable to acquire lock on the key provided.");
               }
           }
           catch (Exception e)
           {
               throw new CacheException(e.Message);
         }
     }

Moreover, if this is not the case in your application then we will have to further review and diagnose this issue. please share with a working sample me that I can use to reproduce the issue in our environment. I will then get back to you with feedback.

andrevoltolini commented 4 years ago

Hi, the problem is in the acquisition of the lock for entities that have a cyclical reference.

For example: Person.Company -> Company.People.

Thank you for your help