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: Want to Building Session Factory once only for each run
PostPosted: Tue Jun 13, 2006 4:29 am 
Beginner
Beginner

Joined: Mon May 22, 2006 12:12 am
Posts: 23
Hi,

I've another problem...wanted to create a session helper, which should store ISessionFactory by calling one time only. I've declared static variables holding the Assembly name and ISessioniFactory info etc. But it seems not the case.

I use the following lines to invoke and get a session. If I didn't call the setAssembly function, it will have error stating that no assembly name defined. But as mentioned, the assembly name should have been stored after called once when program starts up, the result is unexpected to me.:

NHibernateSessionHelper.setAssembly("MyAppl.Business");
sess = NHibernateSessionHelper.openSessionFactory().OpenSession();

Learned from the forum that building Session Factory is resources-intensive. That's the reason I wanted to have Session Factory invoked once for the whole application run-time. Pls point me out which part I should remedy to have the info stored and desired. Thanks Again!

The Helper code:

public class NHibernateSessionHelper
{
private static string _assembly;
private static Configuration _cfg;
private static ISessionFactory _sf;
private static ISession _sess;

public static Configuration openConfig()
{
if (_cfg == null)
{
Configuration cfg = new Configuration();
cfg.Configure();
_cfg = cfg;
}
return _cfg;
}

public static void setAssembly(String value)
{
_assembly = value;
}

public static string getAssembly()
{
return _assembly;
}

public static ISessionFactory openSessionFactory()
{
if (_sf == null)
{
Configuration cfg = openConfig();

cfg.AddAssembly(_assembly);
_sf = cfg.BuildSessionFactory();
}
return _sf;
}



}


Top
 Profile  
 
 Post subject: Design pattern
PostPosted: Tue Jun 13, 2006 5:38 am 
Newbie

Joined: Fri May 13, 2005 3:54 pm
Posts: 6
Location: Istanbul / Turkey
You can use singleton pattern in your helper class.

Detail about singleton pattern : http://www.dofactory.com/Patterns/PatternSingleton.aspx

It will have only one instence and global point of access to it.

_________________
I am not a great programmer, i am a good programmer with great programming habits.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 5:45 am 
Newbie

Joined: Mon Jun 05, 2006 8:51 am
Posts: 18
Hi,
First off, are you aware of the mapping element in hibernate.cfg.xml? This element has a single attribute assembly, which tells NHibernate in which assembly to look for mappings. So you should probably exploit this feature. If you do, your code will look like this:

Code:
// Don't partculary like "Helper" postifx :)
public static class NHibernateSessionUtil
{
    // You don't need to store Configuration object,
    // but you _do_ need to store Session Factory.
    private static ISessionFactory _sessionFactory;
    // Are you sure you want to store a Session?
    private static ISession _session;

    // Returns current Session Factory.
    public static ISessionFactory SessionFactory
    { get { return _sessionFactory; } }

    // Returns current Session.
    public static ISession Session
    {
        get
        {
            if(_session == null)
                _session = SessionFactory.OpenSession();

            return _session;
         }
    }

    // Static constructor. Read more on that in MSDN.
    static NHibernateSessionUtil()
    {
        Configuration configuration = new Configuration();
        configuration.Configure();

        _sessionFactory = configuration.BuildSessionFactory();
    }
}


Top
 Profile  
 
 Post subject: lazy loading on properties
PostPosted: Thu Jun 15, 2006 11:25 am 
Newbie

Joined: Thu Jun 15, 2006 11:15 am
Posts: 3
Hi,
I've just upgraded to nhibernate 1.2.0,
generics works nice, woo hoo!!!

But I have a problem:
if I do this:
NHibernate.ISession session = NHibernate.ISession)NHibernateHelper.NHibernateHelper.GetCurrentSession();


MyObject o = session.Load<MyObject>(PK);
///////////////////////////////////////////////////
//NHibernateUtil.Initialize(o.Name);
//NHibernateUtil.Initialize(o.IsActive);
///////////////////////////////////////////////////

session.Flush();

NHibernateHelper.NHibernateHelper.CloseSession();

return o;

I get a lazy loading error on the fields o.Name and o.IsActive!!

If I NHibernateUtil.Initialize(o.Name), I can access this field!!

It's the default behaviour???
How can I specify this lazy="false" in the mapping property??


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 10:48 pm 
Newbie

Joined: Wed Jun 21, 2006 2:45 pm
Posts: 16
"are you aware of the mapping element in hibernate.cfg.xml"

I did not know, so I went and did some research:

http://www.hibernate.org/hib_docs/nhibe ... start.html

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Add this element -->
<configSections>
<section
name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
/>
</configSections>

<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Server=(local);initial catalog=quickstart;Integrated Security=SSPI</property>

<mapping assembly="QuickStart" />
</session-factory>
</hibernate-configuration>

<!-- Leave the system.web section unchanged -->
<system.web>
...
</system.web>
</configuration>

Once I setup my config as above, I was able to call the cfg.Configure()

the main key is the mapping assembly above.

Thanks for the tip to help find this and I hope if others come here they can see this as well (assuming it's correct, I am new)

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 11:16 pm 
Newbie

Joined: Wed Jun 21, 2006 2:45 pm
Posts: 16
a.gogolev,

I need a bit of assistance understanding making the session itself static like you have done:

Is it safe what you are doing above with the ISession not getting closed?

That is what I think I'm seeing?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 11:57 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
Awesome... this just cleaned my code a bit. I was using the name-value section handler and manually adding my assembly in the initializer. I like this much more.


Top
 Profile  
 
 Post subject: Re: lazy loading on properties
PostPosted: Thu Jun 22, 2006 12:02 am 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
gaddoz wrote:
Hi,
It's the default behaviour???
How can I specify this lazy="false" in the mapping property??


There's much more detailed post in the thread about 1.2.0... but the easiest way to enforce lazy behavior in your mapping is something like:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-lazy="false">

or

<class name="MobysThinkTank.mobyProject.Account, mobyProject" table="Account" lazy="false">

Otherwise, you'll need to mark your properties in your class as virtual

so,

public string Name
{ get; set; }

becomes

public virtual string Name
{ get; set; }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 6:29 pm 
Newbie

Joined: Wed Jun 21, 2006 2:45 pm
Posts: 16
After some further reading ( http://www.theserverside.net/tt/article ... ibernateP2 )

It would not be a good idea to use the static ISession as noted above.

From the article:

"This solution might look like it will work for Session management as well. Why not just add a static Session to the class, and create and open it just once in the static constructor? Then, all the methods could use the same Session. The answer is twofold: first, a statically shared Session might be accessed by multiple threads concurrently, which could interfere with transactional semantics. Second, a statically shared Session will spend most of its life sitting idle, and an idle Session is still holding open a physical database connection. Database connections are expensive, rare and valuable. Every data access strategy after server-side cursors has had as a goal minimizing the length of time an application holds open a connection, and NHibernate is no different. Therefore, having a static Session sitting around open and available is a poor strategy."

I think the manual has a good example of a way to design this http://www.hibernate.org/hib_docs/nhibe ... index.html as well as the article posted above.

Just thought I'd post back to answer my own question and anyone else who had the same question :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 10:25 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
Fwiw, I've got a session-in-view implementation from long ago that I'd be happy to share. I also have a much newer version that I'm a lot happier with but haven't really stressed yet. The new one is a modified version of the one Billy McCafferty uses in his Code Project article


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.