-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: About sharing session
PostPosted: Sat May 21, 2005 12:54 am 
Regular
Regular

Joined: Thu Dec 02, 2004 10:42 am
Posts: 54
I want to use one session to provide my system with some common codes, like country code, currency code. I just need read-only.

Does the new version solve the thread safe problem of session? or at least correct problem described here:

http://msepc10.scs.ad.cs.cmu.edu/sapphi ... ment_Notes :?:


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 21, 2005 1:05 am 
I think this is scheduled for 0.8.4. Here is the JIRA:

http://jira.nhibernate.org/browse/NH-262


Top
  
 
 Post subject:
PostPosted: Sat May 21, 2005 4:39 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
No, sessions will never be thread-safe, by design. That issue refers to a problem with SessionFactoryImpl, and a strange problem, by the way.

And I don't understand, what problem described on that page do you want to correct?


Top
 Profile  
 
 Post subject: I want to make use of the cache NHnibernate providing alread
PostPosted: Sat May 21, 2005 2:59 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 10:42 am
Posts: 54
In this article:
http://msepc10.scs.ad.cs.cmu.edu/sapphi ... ment_Notes

First approach:

Share the session among clients to have single cache guaranteeing that there is a unique in-memory representation of any domain model object. This approach may not be optimal because a session is not thread-safe. We were planning to ignore this and to add synchronization later. Unfortunately we came across another problem: database access works well for a local client (unit tests). For a remote client, however, the first database access succeeds but later attempts to read from another table fail, NHibernate can't even parse the HQL statement.

The problem was caused by a missing statement in the server object. We only read 2 of the three mapping files.
-----------------------------------------------------------
I want to make use of the caching, which NHibernate session provided already, to cache some common codes, so that every client can just read the code using this session and mostly from the cache. I don't want to create NHibernate session for each client, cause they have to re-load data from database. If I use the same session, I can take adavantage of lazy loading together with caching for the common codes (each code may have 1-many children, I'd like to have lazy loading. And there are lots of codes, I don't want to load them all in one shot. I'd like to make use of cache and lazyloading, and the cache will dump some codes when not using them.)

For upper purpose,cause it's read-only, I think it will be ok even the session is not thread-safe. But I am not sure and I am not sure whether I can do stress test easily. Can you guys confirm it?

For next step, I'd like reuse the session for updating some time. If one admin client come in to modify the common codes, and if he can make use of the same session and the code is cached in the session, he don't need to fetch from the DB again. Why can't NHibernate implement this?


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 21, 2005 4:15 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Well, Hibernate was designed primarily for web applications, those that run for a short time, spit out a page, and then exit. So sessions are fragile, you can't reuse a session after an exception occurs, and they are supposed to be short-lived (spanning only a few related transactions).

If you want to have a cache of reference objects, the easy approach is to use the second-level cache, so that NHibernate doesn't access the database after loading an object once. But it will give you different instances of objects in different sessions (the cache doesn't store the object itself, but only values of its properties in an array).

If you can't live with it, a more complicated approach is to load the reference data on startup, store it somewhere, and reassociate the objects with sessions created afterwards, using Lock(someObject, LockMode.None). This will make sessions a bit less efficient, though, since Flush will have to also traverse your reference data objects to look for modifications, and they will never be modified, so some time will be wasted.


Top
 Profile  
 
 Post subject: 2nd-Level cache
PostPosted: Tue May 24, 2005 3:11 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 10:42 am
Posts: 54
Could someone kindly provide some link about tutorial or samples using 2nd-level cache?

I have been search the web, but seems there are not enough article about it yet.

For Hibernate, it provides some cache providers like: SwarmCacheProvider, EhCacheProvider etc. When will NHibernate provide similar stuff? Or is there any existing interface to use for ASP.Net or EntLib 1.0 cache framework?

Anyways, the question is, how to use the second level cache?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 4:25 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernateContrib contains the ASP.NET cache provider implementation. But I don't know exactly how you set it up, so try looking around for examples, or maybe somebody else will answer here.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:28 am 
Regular
Regular

Joined: Thu Dec 02, 2004 10:42 am
Posts: 54
I made the 2nd level cache working fine.

Thanks for the help.


Top
 Profile  
 
 Post subject: Please share
PostPosted: Thu May 26, 2005 12:38 pm 
Regular
Regular

Joined: Tue May 24, 2005 12:55 pm
Posts: 56
I'm trying to do the same thing. Will you share what you did?


Top
 Profile  
 
 Post subject: Re: Please share
PostPosted: Thu May 26, 2005 1:05 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 10:42 am
Posts: 54
tinomen wrote:
I'm trying to do the same thing. Will you share what you did?


I just use the 2nd level cache, so the code just create a new session every time, and when you set your object's mapping with <jcs-cache>, when you do session.Load() it will just load it from the 2nd level cache.

Check this simple tutorial:
http://bitarray.co.uk/ben/archive/2005/04/09/573.aspx

Do remember that only Load can try to find the object from the cache. If you used find, you will find out it is fetching data from the DB anyways. But it does put the result into the cache. I think the reason is find is query with some creterias may return multiple rows, it's not necessary to check the cache with some rows and read the other rows from DB.

Anyways, the recommend cacheprovider is NHibernateContrib's SysCache, which is using ASP.Net caching, but I am not sure about how to use Pravelent cache yet.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.