-->
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.  [ 8 posts ] 
Author Message
 Post subject: How do you use NHibernate?
PostPosted: Mon Feb 13, 2006 12:25 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi.

Could you show me (with code) how are you using the NHibernate? I'd like to know how do you manage SessionFactory, Sessions and etc.

I think this sould be a fixed topic (not this, but other clearer..) to tell the users what is the correct (or the best) way to use NHibernate.

My way:

Nowadays, I just keep a singleton pattern for SessionFactory. I have a GetSession method that every time returns a new Session.

Code:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using MapOR;

public class NHibernateHelper
{
    private static ISessionFactory _sessionFactory;
   
    public static ISession GetSession()
    {
        if (_sessionFactory == null)
        {
            lock (typeof(NHibernateHelper))
            {
                if (_sessionFactory == null)
                {
                    Configuration cfg = new Configuration();
                    cfg.AddAssembly("MapOR");
                    _sessionFactory = cfg.BuildSessionFactory();
                }
            }
        }
        return _sessionFactory.OpenSession();
    }

    private NHibernateHelper()
    {
    }
}


Using this, I have to control the Transactrion and Sessions in the Interface Layer.

What are you using? Can you tell me? Thanks


Top
 Profile  
 
 Post subject: Castle
PostPosted: Tue Feb 14, 2006 12:30 am 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
Have you seen the NHibernate facility (an extension to the Windsor IOC container) available at http://www.castleproject.org/index.php/Main_Page

There is another interesting project built over NHibernate called ActiveRecord.
Check these out.....


Top
 Profile  
 
 Post subject: Re: Castle
PostPosted: Tue Feb 14, 2006 7:24 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
samujob wrote:
Have you seen the NHibernate facility (an extension to the Windsor IOC container) available at http://www.castleproject.org/index.php/Main_Page

There is another interesting project built over NHibernate called ActiveRecord.
Check these out.....


Hi Samujob!

Are you talking about Castle.Facilities.NHibernateExtension ?


Top
 Profile  
 
 Post subject: Deprecated
PostPosted: Tue Feb 14, 2006 7:32 am 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
I believe Castle.Facilities.NHibernateExtension has been deprecated and replaced by Castle.Facilities.NHibernateIntegration. It has some nice features like automatic session management, automatic transaction handling.


Top
 Profile  
 
 Post subject: Re: Deprecated
PostPosted: Tue Feb 14, 2006 8:03 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
samujob wrote:
I believe Castle.Facilities.NHibernateExtension has been deprecated and replaced by Castle.Facilities.NHibernateIntegration. It has some nice features like automatic session management, automatic transaction handling.


I've also found this: http://www.castleproject.org/index.php/ ... NHibernate

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 12:37 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Looks interesting, but it seems more awkward than the approach in .NET 2.0 with System.Transactions, using TransactionScope. With Castle.Facilities.NHibernateIntegration and Castle.Facilities.AutomaticTransactionManagement, apparently you need to make your methods virtual because your DAOs will be proxied. You need to mark your classes with the [Transactional] attribute, and define transaction boundaries on a per-method basis with [Transaction(TransactionMode.Whatever)]. I don't think System.Transactions has the same limitations.

In the example they give,
Code:
[Transactional]
public class BlogDao
{
  [Transaction(TransactionMode.Requires)]
  public virtual Blog CreateBlog( String name )
  {
    using(ISession session = SessionManager.OpenSession())
    {
      // This session is going to have a transaction associated

      Blog blog = new Blog();
      blog.Name = name;

      session.Save(blog);

      return blog;
    }
  }
}

the session is implictly disposed. I'm confused -- I thought we wanted to not only be able to join an ambient transaction, but an ambient session. I.e. SessionManager.OpenSession() would return the ambient session if one exists, instead of a new one. If so, the ambient session would be disposed by this block of code! I'm confused -- how can a transaction live across multiple NHibernate sessions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 11:05 pm 
Newbie

Joined: Tue Feb 14, 2006 10:46 pm
Posts: 1
Hi there,

You can customize the NHibernateIntegration facility to suit your needs, by removing the Castle's Automatic Transaction Managemenet feature.

Instead, you can define your own transactional attributes to mark you business methods, intercept the business method calls of you business components (by placing your components in the Caste's Micro Kernel) and, in you interceptor, use the transaction management of the .net 2.0 System.Transactions namespace using the TransactionScope. :)

Concerning the SessionManager of the NHibernateIntegration facility, it returns you a wrapper of the ISession that is not actually disposed when the 'using block' ends, so you can use the same underlying ISession during a transaction lifetime.

Using a Generic DAO component that uses the SessionManager & an interceptor for your business components to take care of transaction management, you can have clean code in your business components (no session management / data access code, only calls to a generic DAO, and transactional attributes on your business classes / methods).

Stiiifff


Top
 Profile  
 
 Post subject: NHibernate facility
PostPosted: Wed Feb 15, 2006 12:13 am 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
Let me clarify few things regarding the NHibernate facility in Castle

Even when you are using the session in this manner:

Code:
using(ISession session = SessionManager.OpenSession())
    {
      // This session is going to have a transaction associated

      Blog blog = new Blog();
      blog.Name = name;

      session.Save(blog);

      return blog;
    }


the session will not be disposed if there is a CurrentTransaction in progress (see the Dispose implementation in SessionDelegate class).

Now the second point:
After a call to OpenSession on SessionManager any further call to OpenSession would return the same session. for e.g. if in MyDAO you get a session and call a method on MySecondDAO, the call to OpenSession in MySecondDAO will return the same Session.

Now regarding auto transaction:
As I said the transaction is not living across Sessions as the session is not disposed even when the Using block completes if there is a current transaction.
Whenever a transactional method calls another transactional method they share the same CurrentTransaction (this is achieved by the PerThread lifestyle of the NHibernateTransactionManager class) and the transaction gets committed or rolledback only when the root method finishes however the child transactios can vote for rollback.

Hope this helps....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.