you definately don't want 1 session per application. 1 session factory
yes.

the main problems most encounter with NH/AR is
select n+1
and too many db writes

select n+1 can be avoided by using fetchmodes (eager, select, etc)
db writes can be reduced (depending on pk type) by use ado.net batch
sizes

there are also issues of loading too much data, or loading the same
entity/collection multiple times.

as for cache: NH has 2 levels of cache
level 1 is automatically used when you call Get<>(id) or Load<>(id).
this cache exists for the scope of the session.
level 2 is disabled by default, you must configure that it is used,
what engine to use and what queries/entities to cache. this cache
exists for the scope of the session factory. this is consider an
advanced feature and should not be used until absolutely necessary.

I use NH for all my projects, of which are small in scale. there are
only a handful of members I call for managing the session:
SessionFactory.OpenSession()
Session.BeginTransaction();
Session.Dispose();
Transaction.Commit();
Transaction.Rollback();
Transaction.Dispose();

then there are the members for accessing the data
Save();
Delete();
Get();
Load();
CreateCriteria();
GetExecutableCriteria();

that's about it when working in a web environment. Never working with
win forms I haven't had a need for Merge() or other functions for
attaching entities to a session.

also once you get an idea of what's going on with AR/NH I would post a
question in the nhuser google group. While you're using AR the
problems exist with the usage of NH, no the AR attributes or BaseClass/
Mediator.

On May 26, 4:17 pm, [email protected] wrote:
> I know what you mean when you talk about confusion. There is not
> much real information out there about session management in windows
> forms applications. I by now use a Session per use case and/or
> a Session per form and it works good. Sure it has a little overhead
> since you have to re - Get an object if you use it as parameter
> for a form, but this acceptable. Every use case or every form has
> an IPersistenceContext - object and every IRepository used can be
> obtained form that Context. The IPersistenceContext Implementation
> manages the session and the IRepository - Implementations are working
> in that context. So you can easily decide and control, how long a Session 
> will live. I don't know if this is the "right" way to do
> it and maybe someone her has another suggestion, but for me it works
> fine.
>
> Regards,
> Volker
>
>
>
> >Hi Jason,
>
> >Thank you for your reply. That is not harsh at all. I am well aware of
> >the mistake I made. I was so frustrated with linq, that
> >I just had to drop it and quickly replace it with something else. Even
> >without any previous experience with NHibernate/AR,
> >it seemed far more intuitive and capable than Linq, and I decided to use
> >it. Even with the problems I have, I don't regret one bit.
>
> >I read just about everything I could find on the web about AR/NH session
> >management, but most of it is talking about web apps.
> >Also, a lot of it is just adding to my confusion. For example, some
> >people say using one session for application is a 'time bomb',
> >other people ('Hibernate in Action', IIRC) say it is just the right
> >way... Another example. I found many articles suggesting Session.Clear()
> >should be called after Session.Flush(), but then I find Ayende's '...if
> >you're calling Clear(), you're doing something weird...'
>
> >If something I wrote sounds rude or in any way inappropriate, my apologies.
>
> >Again, thanks for your reply. Downloading nhprof it right away.
>
> >Jason Meckley wrote:
> >> This may be harsh, but your first mistake was blindly applying a
> >> framework to a project which is almost complete.
>
> >>> I made a quick switch from Linq to Sql to AR near the end of a project, 
> >>> and
> >didn't have the time to properly learn AR/NHibernate before I started using
> >it.
>
> >> As for advice on what the problem may be:
> >> 1. Start by reading the documentation on AR and NH session management.
> >> (AR is primarily about attributes, so I venture to say most of the
> >> problems are with session management). AR is not a silver bullet or
> >> magic potion for data access you must understand the tool to use it
> >> properly.
> >> 2. Get a license for NHProf (www.nhprof.com) and profile your
> >> application. This will tell you the cause of your problems. Since you
> >> have now read the documentation you can begin to solve the pain
> >> points.
>
> >> On May 26, 12:40 pm, vekaz turkovic <[email protected]> wrote:
>
> >>> Hi all,
>
> >>> I am using Castle AR in a WinForms application. I made a quick switch
> >>> from Linq to Sql to AR near the end of a project, and didn't have the
> >>> time to properly learn AR/NHibernate before I started using it.
> >>> So, be gentle, this may be a really stupid question...
>
> >>> Several users run the application simultaneously. I want to reload the
> >>> data from the database each time a user saves something. I am getting
> >>> the data using the same code when a form is first
> >>> loaded, and later, after data is saved. This seemed to work ok, until I
> >>> tested the app with two users using the app simultaneously. First user
> >>> saves stuff, second user tries to reload the data,
> >>> but he doesn't see the changes made by the first user until the app is
> >>> restarted.
>
> >>> If i dispose the current scope and create a new one, everything works
> >>> perfectly, but it takes forever to complete. If I run the application on
> >>> my desktop computer, the same computer where the SQL Server is,
> >>> everything is fine. But, when I run it on my laptop, and connect to the
> >>> server through the network, the process of saving, disposing the current
> >>> scope, creating a new one and reloading the data (which is usually just
> >>> several records) takes about 10 seconds.
>
> >>> Is there any other way of forcing AR to get the data from the DB, not
> >>> the cache? Or, am i doing all this completely wrong?
>
> >>> I need to find solution to this asap, any suggestions are very welcome.
>
> >>> Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" 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/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to