You don't have transactions here.
Note the recent thread about it in nhcdev

On Thu, Aug 28, 2008 at 10:46 AM, Andrew Melnichuk <
[EMAIL PROTECTED]> wrote:

> Ok, i have played with unit test more and it seems that query caching works
> only when connection.release_mode is set to "on_close" value.  Here is
> updated test. If i have this config parameter
> cfg.Properties.Add("connection.release_mode", "on_close"); - test passes.
> But if i set it to "auto" or "after_transaction" test fails. Can't
> understand how query cache correlates with connection management.
>
>     [TestFixture]
>     public class QueryCacheTestFixture
>     {
>         private ISessionFactory factory;
>
>         [SetUp]
>         public void SetUp()
>         {
>             Configuration cfg = new Configuration();
>             cfg.Properties.Add("connection.driver_class",
> "NHibernate.Driver.SQLite20Driver");
>             cfg.Properties.Add("dialect",
> "NHibernate.Dialect.SQLiteDialect");
>             cfg.Properties.Add("connection.provider",
> "NHibernate.Connection.DriverConnectionProvider");
>             cfg.Properties.Add("connection.connection_string", "Data
> Source=D:\\Temp\\QueryCacheTest\\bin\\Debug\\db3;Version=3;New=True;");
>             cfg.Properties.Add("connection.release_mode", "on_close"); //
> TEST PASSES for on_close only
>             cfg.Properties.Add("cache.use_second_level_cache", "true");
>             cfg.Properties.Add("cache.use_query_cache", "true");
>             cfg.Properties.Add("cache.provider_class",
> "NHibernate.Cache.HashtableCacheProvider, NHibernate");
>             cfg.Properties.Add("show_sql", "true");
>
>             //cfg.Properties.Add("cache.use_structured_entries", "true");
>             //cfg.Properties.Add("generate_statistics", "true");
>
>             cfg.AddAssembly(typeof(QueryCacheTestFixture).Assembly);
>
>             factory = cfg.BuildSessionFactory();
>             new SchemaExport(cfg).Execute(true, true, false, true);
>         }
>
>         [TearDown]
>         public void TearDown()
>         {
>         }
>
>         [Test]
>         public void Test()
>         {
>             Customer c = new Customer();
>             c.Name = "Customer";
>             Resource r1 = new Resource();
>             r1.Customer = c;
>             r1.Name = "Resource1";
>             Resource r2 = new Resource();
>             r2.Customer = c;
>             r2.Name = "Resource2";
>
>             using (ISession session = factory.OpenSession())
>             {
>                 session.Save(c);
>                 session.Save(r1);
>                 session.Save(r2);
>                 session.Flush();
>             }
>
>             long count1 = 0;
>             long count2 = 0;
>
>             using (ISession session = factory.OpenSession())
>             {
>                 count1 = session
>                     .CreateQuery("select count(r) from Resource r where
> r.Customer = :customer")
>                     .SetCacheable(true)
>                     .SetParameter("customer", c)
>                     .UniqueResult<long>();
>             }
>
>             using (ISession session = factory.OpenSession())
>             {
>                 IDbCommand cmd = session.Connection.CreateCommand();
>                 cmd.CommandText = "delete from Resource";
>                 cmd.ExecuteNonQuery();
>             }
>
>             using (ISession session = factory.OpenSession())
>             {
>                 count2 = session
>                     .CreateQuery("select count(r) from Resource r where
> r.Customer = :customer")
>                     .SetCacheable(true)
>                     .SetParameter("customer", c)
>                     .UniqueResult<long>();
>             }
>
>             Assert.That(count1 == count2);
>         }
>     }
>
> On Thu, Aug 28, 2008 at 10:17 AM, Andrew Melnichuk <
> [EMAIL PROTECTED]> wrote:
>
>> You were right! It works for different sessions! Thanks.
>>
>>
>> On Tue, Aug 26, 2008 at 4:23 PM, Ayende Rahien <[EMAIL PROTECTED]> wrote:
>>
>>> You cannot use the query cache on the same session, create a new one and
>>> see what is going on.
>>>
>>>
>>> On Tue, Aug 26, 2008 at 9:17 AM, Andrew Melnichuk <
>>> [EMAIL PROTECTED]> wrote:
>>>
>>>> I feel that i'm doing something wrong, or i have some concept
>>>> miunderstanding of how it all should work. Ok here my code.
>>>>
>>>>     [TestFixture]
>>>>     public class QueryCacheTestFixture
>>>>     {
>>>>         private ISession sess;
>>>>
>>>>         [SetUp]
>>>>         public void SetUp()
>>>>         {
>>>>             Configuration cfg = new Configuration();
>>>>             cfg.Properties.Add("connection.driver_class",
>>>> "NHibernate.Driver.SQLite20Driver");
>>>>             cfg.Properties.Add("dialect",
>>>> "NHibernate.Dialect.SQLiteDialect");
>>>>             cfg.Properties.Add("connection.provider",
>>>> "NHibernate.Connection.DriverConnectionProvider");
>>>>             cfg.Properties.Add("connection.connection_string", "Data
>>>> Source=:memory:;Version=3;New=True;");
>>>>             cfg.Properties.Add("connection.release_mode", "on_close");
>>>>             cfg.Properties.Add("cache.use_second_level_cache", "true");
>>>>             cfg.Properties.Add("cache.use_query_cache", "true");
>>>>             cfg.Properties.Add("cache.provider_class",
>>>> "NHibernate.Cache.HashtableCacheProvider, NHibernate");
>>>>             cfg.Properties.Add("show_sql", "true");
>>>>
>>>>             //cfg.Properties.Add("generate_statistics", "true"); -->
>>>> causes NullReference exception
>>>>             cfg.Properties.Add("cache.use_structured_entries", "true");
>>>>
>>>>             cfg.AddAssembly(typeof(QueryCacheTestFixture).Assembly);
>>>>
>>>>             ISessionFactory factory = cfg.BuildSessionFactory();
>>>>             sess = factory.OpenSession();
>>>>             new SchemaExport(cfg).Execute(true, true, false, true,
>>>> sess.Connection, null);
>>>>         }
>>>>
>>>>         [TearDown]
>>>>         public void TearDown()
>>>>         {
>>>>             sess.Close();
>>>>         }
>>>>
>>>>         [Test]
>>>>         public void Test()
>>>>         {
>>>>             Customer c = new Customer();
>>>>             c.Name = "Customer";
>>>>             Resource r1 = new Resource();
>>>>             r1.Customer = c;
>>>>             r1.Name = "Resource1";
>>>>             Resource r2 = new Resource();
>>>>             r2.Customer = c;
>>>>             r2.Name = "Resource2";
>>>>
>>>>             sess.Save(c);
>>>>             sess.Save(r1);
>>>>             sess.Save(r2);
>>>>             sess.Flush();
>>>>
>>>>             // select and cache value type result (count)
>>>>             long count1 = sess
>>>>                 .CreateQuery("select count(r) from Resource r where
>>>> r.Customer = :customer")
>>>>                 .SetCacheable(true)
>>>>                 .SetParameter("customer", c)
>>>>                 .UniqueResult<long>();
>>>>
>>>>             // remove resources, cache not involved
>>>>             IDbCommand cmd = sess.Connection.CreateCommand();
>>>>             cmd.CommandText = "delete from Resource";
>>>>             cmd.ExecuteNonQuery();
>>>>
>>>>             // should get cached value, but it still goes do DB
>>>>             long count2 = sess
>>>>                 .CreateQuery("select count(r) from Resource r where
>>>> r.Customer = :customer")
>>>>                  .SetCacheable(true)
>>>>                 .SetParameter("customer", c)
>>>>                 .UniqueResult<long>();
>>>>
>>>>             Assert.That(count1 == count2); // fails 2 == 0
>>>>         }
>>>>     }
>>>> }
>>>>
>>>> Mappings are simple:
>>>>
>>>>     public class Customer
>>>>     {
>>>>         private int _id;
>>>>         private string _name;
>>>>
>>>>         public Customer() { }
>>>>
>>>>         public virtual int Id
>>>>         {
>>>>             get { return _id; }
>>>>             set { _id = value; }
>>>>         }
>>>>
>>>>         public virtual string Name
>>>>         {
>>>>             get { return _name; }
>>>>             set { _name = value; }
>>>>         }
>>>>     }
>>>>
>>>> <?xml version="1.0" encoding="utf-8" ?>
>>>> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
>>>>     assembly="QueryCacheTest" namespace="QueryCacheTest">
>>>>     <class name="Customer" table="Customer" lazy="false">
>>>>         <cache usage="read-only" region="customer" />
>>>>         <id name="Id">
>>>>             <generator class="identity" />
>>>>         </id>
>>>>         <property name="Name" />
>>>>     </class>
>>>> </hibernate-mapping>
>>>>
>>>>     public class Resource
>>>>     {
>>>>         private int _id;
>>>>         private string _name;
>>>>         private Customer _customer;
>>>>
>>>>         public Resource() { }
>>>>
>>>>         public virtual int Id
>>>>         {
>>>>             get { return _id; }
>>>>             set { _id = value; }
>>>>         }
>>>>
>>>>         public virtual string Name
>>>>         {
>>>>             get { return _name; }
>>>>             set { _name = value; }
>>>>         }
>>>>
>>>>         public virtual Customer Customer
>>>>         {
>>>>             get { return _customer; }
>>>>             set { _customer = value; }
>>>>         }
>>>>     }
>>>>
>>>> <?xml version="1.0" encoding="utf-8" ?>
>>>> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
>>>>     assembly="QueryCacheTest" namespace="QueryCacheTest">
>>>>     <class name="Resource" table="Resource">
>>>>         <cache usage="read-only"/>
>>>>         <id name="Id">
>>>>             <generator class="identity" />
>>>>         </id>
>>>>         <property name="Name" />
>>>>         <many-to-one name="Customer" column="CustomerId" />
>>>>     </class>
>>>> </hibernate-mapping>
>>>>
>>>>
>>>> On Mon, Aug 25, 2008 at 9:16 PM, Andrew Melnichuk <
>>>> [EMAIL PROTECTED]> wrote:
>>>>
>>>>> Sure will do it tomorrow.
>>>>>
>>>>>
>>>>> On Mon, Aug 25, 2008 at 7:34 PM, Ayende Rahien <[EMAIL PROTECTED]>wrote:
>>>>>
>>>>>> Can you create a small test case that demonstrate the issue?
>>>>>>
>>>>>> On Sun, Aug 24, 2008 at 6:25 AM, Andrew Melnichuk <
>>>>>> [EMAIL PROTECTED]> wrote:
>>>>>>
>>>>>>> Does anybody had same problems?
>>>>>>>
>>>>>>>
>>>>>>> On Fri, Aug 22, 2008 at 10:13 PM, Andrew Melnichuk <
>>>>>>> [EMAIL PROTECTED]> wrote:
>>>>>>>
>>>>>>>> No i do not create process again, so cache exists between 2 calls in
>>>>>>>> my web app. As i told, caching of classes and collections works as 
>>>>>>>> expected.
>>>>>>>> Maybe i missed something in config?
>>>>>>>>
>>>>>>>>
>>>>>>>> On Fri, Aug 22, 2008 at 10:04 PM, Andrew Melnichuk <
>>>>>>>> [EMAIL PROTECTED]> wrote:
>>>>>>>>
>>>>>>>>> In fact, launches are web requests (session-per-request model). I
>>>>>>>>> just posted logs from 1st and 2nd requests to show that 1st request 
>>>>>>>>> hits DB
>>>>>>>>> than caches data, and 2nd request hits cache only, but it is not :(
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Fri, Aug 22, 2008 at 7:45 PM, Ayende Rahien <[EMAIL 
>>>>>>>>> PROTECTED]>wrote:
>>>>>>>>>
>>>>>>>>>> What do you mean by second launch?Are you using separate sessions
>>>>>>>>>> ? Are you creating the process again?
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> On Fri, Aug 22, 2008 at 12:29 PM, Andrew Melnichuk <
>>>>>>>>>> [EMAIL PROTECTED]> wrote:
>>>>>>>>>>
>>>>>>>>>>> Hello everybody.
>>>>>>>>>>>
>>>>>>>>>>> I'm trying to use second-level query cache, but my query just
>>>>>>>>>>> misses cache and always goes to database. Here is my code:
>>>>>>>>>>>
>>>>>>>>>>> Configuration (castle's nhibernate facility)
>>>>>>>>>>>     ...
>>>>>>>>>>>     <item
>>>>>>>>>>> key="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider,
>>>>>>>>>>> NHibernate.Caches.SysCache</item>
>>>>>>>>>>>     <item key="cache.use_query_cache">true</item>
>>>>>>>>>>>     <item key="cache.use_second_level_cache">true</item>
>>>>>>>>>>>    ...
>>>>>>>>>>>
>>>>>>>>>>> Query
>>>>>>>>>>>            ......
>>>>>>>>>>>             using (ISession session =
>>>>>>>>>>> _sessionManager.OpenSession())
>>>>>>>>>>>             {
>>>>>>>>>>>                 return session.CreateQuery(
>>>>>>>>>>>                     "select count(r) from Resource r where
>>>>>>>>>>> r.Customer = :customer")
>>>>>>>>>>>                     .SetParameter("customer", customer)
>>>>>>>>>>>                     .SetCacheable(true)
>>>>>>>>>>>                     .SetCacheRegion("resource-count")
>>>>>>>>>>>                     .UniqueResult<long>();
>>>>>>>>>>>             }
>>>>>>>>>>>             .......
>>>>>>>>>>>
>>>>>>>>>>> And here is what in the log
>>>>>>>>>>>
>>>>>>>>>>> 1st launch, nothing cached yet
>>>>>>>>>>>
>>>>>>>>>>> SQL:
>>>>>>>>>>> select count_big(resource0_.ResourceKey) as x0_0_ from Resource
>>>>>>>>>>> resource0_ where ([EMAIL PROTECTED] ); @p0 = '2'
>>>>>>>>>>>
>>>>>>>>>>> CACHE:
>>>>>>>>>>> Fetching object 'NHibernate-Cache:resource-count:sql: select
>>>>>>>>>>> count_big(resource0_.ResourceKey) as x0_0_ from Resource resource0_ 
>>>>>>>>>>> where
>>>>>>>>>>> (resource0_.CustomerKey=? ); parameters: []; named parameters:
>>>>>>>>>>> [EMAIL PROTECTED]' from the
>>>>>>>>>>> cache.
>>>>>>>>>>>
>>>>>>>>>>> adding new data: key=NHibernate-Cache:resource-count:sql: select
>>>>>>>>>>> count_big(resource0_.ResourceKey) as x0_0_ from Resource resource0_ 
>>>>>>>>>>> where
>>>>>>>>>>> (resource0_.CustomerKey=? ); parameters: []; named parameters:
>>>>>>>>>>> [EMAIL PROTECTED]
>>>>>>>>>>> &value=System.Collections.ArrayList
>>>>>>>>>>>
>>>>>>>>>>> 2nd launch, should be cached (if i understood all correct :))
>>>>>>>>>>>
>>>>>>>>>>> SQL:
>>>>>>>>>>> select count_big(resource0_.ResourceKey) as x0_0_ from Resource
>>>>>>>>>>> resource0_ where ([EMAIL PROTECTED] ); @p0 = '2'
>>>>>>>>>>>
>>>>>>>>>>> CACHE:
>>>>>>>>>>> Fetching object 'NHibernate-Cache:resource-count:sql: select
>>>>>>>>>>> count_big(resource0_.ResourceKey) as x0_0_ from Resource resource0_ 
>>>>>>>>>>> where
>>>>>>>>>>> (resource0_.CustomerKey=? ); parameters: []; named parameters:
>>>>>>>>>>> [EMAIL PROTECTED]' from the
>>>>>>>>>>> cache.
>>>>>>>>>>>
>>>>>>>>>>> updating value of key 'NHibernate-Cache:resource-count:sql:
>>>>>>>>>>> select count_big(resource0_.ResourceKey) as x0_0_ from Resource 
>>>>>>>>>>> resource0_
>>>>>>>>>>> where (resource0_.CustomerKey=? ); parameters: []; named parameters:
>>>>>>>>>>> [EMAIL PROTECTED]' to
>>>>>>>>>>> 'System.Collections.ArrayList'.
>>>>>>>>>>>
>>>>>>>>>>> So, query to database is always issuing. Caching for classes and
>>>>>>>>>>> collections works as expected. Not sure, maybe i missed something?
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>> Best regards,
>>>>>>>>>>> Andrew Melnichuk
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> Best regards,
>>>>>>>>> Andrew Melnichuk
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> Best regards,
>>>>>>>> Andrew Melnichuk
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Best regards,
>>>>>>> Andrew Melnichuk
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Best regards,
>>>>> Andrew Melnichuk
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Best regards,
>>>> Andrew Melnichuk
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>
>>
>> --
>> Best regards,
>> Andrew Melnichuk
>>
>
>
>
> --
> Best regards,
> Andrew Melnichuk
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to