Saturday 22 December 2012

A read lock may not be acquired with the write lock, Error retrieving values from ObjectStateEntry. See inner exception for details. held in this mode.

Today , i've got this error when trying to insert record in new database.
I'm using entity framework 4.1, code first approach.

A read lock may not be acquired with the write lock,
Error retrieving values from ObjectStateEntry. See inner exception for details.

At first glance, i've thought that there's a current transaction that locks the access on the table when trying to insert a new record. So i check all actives transactions but any of them get a lock on my table.
As I use a transactionscope during the insert operation, i've removed them but the issue still not resolved.

Stop, I decided to go in deeper in the error message , that was the first thing that I had to do....

Take a close look on the InnerException.InnerException object.


Everything becomes clear now. there a violation of constraint that entity framework has detected and it raises an error when constraint has been violated.
Once those constraint have been updated, the problem is resolved !!!


Wednesday 19 December 2012

My old vw combi split bus


NHibernate tunning

 private static ISessionFactory CreateSessionFactory1()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("Sql2008")))
                //.Cache(c=>c.UseSecondLevelCache().ProviderClass(typeof(NHibernate.Caches.SysCache.SysCacheProvider).AssemblyQualifiedName))               
                //.Cache(c => c.UseQueryCache().ProviderClass(typeof(NHibernate.Caches.SysCache.SysCacheProvider).AssemblyQualifiedName))                               
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
                .BuildConfiguration()

            #region cache configuration
                .SetProperty(NHibernate.Cfg.Environment.UseSecondLevelCache, "true")
                .SetProperty(NHibernate.Cfg.Environment.UseQueryCache, "true")
                .Cache(c =>
                {
                    c.Provider<NHibernate.Caches.SysCache.SysCacheProvider>();
                    //    c.DefaultExpiration = 1;
                })
            #endregion
            #region configure cache on Movies Entity
            .EntityCache<Movies>(c =>
            {
                c.Strategy = EntityCacheUsage.ReadWrite;
                c.RegionName = "Region1";
            })
                        #endregion
            .BuildSessionFactory();
        }
      

   using (var session = sessionFactory.OpenSession())
           {
               // var m8 = session.CreateCriteria(typeof(Movies)).Add(Restrictions.Eq("Genre", "Guerre1234")).UniqueResult<Movies>();
               //var m8 = session.Get<Movies>(2);
               var m8 = session.CreateQuery("from Parent10 p where p.id1 =: id1 and p.id2 =: id2")
                       .SetString("id1", "1")
                       .SetString("id2", "2")
                       .SetCacheable(true)
                       .SetCacheRegion("Region1")
                       .Future<Parent10>()
                       //.Enumerable<Parent10>()
                       .First<Parent10>();
              // Console.WriteLine("Id > " + m8.id1 + "Genre > " + m8.id2 + " Price >  " + m8.ParentName);

               //Console.ReadLine();
              // m8.Price = 2; session.Flush();
              // session.Close();
               Console.WriteLine("End Session 1");
           }
           using (var session = sessionFactory.OpenSession())
           {
               //var m8 = session.CreateCriteria(typeof(Movies)).Add(Restrictions.Eq("Genre", "Guerre1234")).UniqueResult<Movies>();
               var m8 = session.CreateQuery("from Parent10 p where p.id1 =: id1 and p.id2 =: id2")
                       .SetString("id1", "1")
                       .SetString("id2", "2")
                       .SetCacheable(true)
                       .SetCacheRegion("Region1")
                       .Future<Parent10>()
                       //.Enumerable<Parent10>()
                       .First<Parent10>();
             //  Console.WriteLine("Id > " + m8.id1 + "Genre > " + m8.id2 + " Price >  " + m8.ParentName);
               var m9 = session.CreateQuery("from Parent10 p where p.id1 =: id1 and p.id2 =: id2")
                      .SetString("id1", "1")
                      .SetString("id2", "3")
                      .SetCacheable(true)
                      .SetCacheRegion("Region1")
                      .Future<Parent10>()
                   //.Enumerable<Parent10>()
                      .First<Parent10>();
               Console.Read();
               var m10 = session.CreateQuery("from Parent10 p where p.id1 =: id1 and p.id2 =: id2")
                      .SetString("id1", "1")
                      .SetString("id2", "2")
                      .SetCacheable(true)
                      .SetCacheRegion("Region1")
                      .Future<Parent10>()
                   //.Enumerable<Parent10>()
                      .First<Parent10>();
              // Console.ReadLine();
               //session.Close();
               Console.WriteLine("End Session 2");
           }