-->
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.  [ 9 posts ] 
Author Message
 Post subject: A coffee during the creation of the session ?
PostPosted: Fri Jul 13, 2007 12:27 pm 
Newbie

Joined: Fri Jul 13, 2007 11:53 am
Posts: 4
Hi,

I test NHibernate 1.2 with VS2005 and Oracle10g. I wrote this code :

Code:
namespace ormsamples {
    public class CreateDonneesData {
        private void CreateData()
        {
            ISessionFactory sessions = new Configuration().Configure().BuildSessionFactory();
            ISession s = sessions.OpenSession();
            ITransaction t = s.BeginTransaction();
            try
            {
                //...

                t.Commit();
            }
            catch(Exception e) {
                t.Rollback();
            }
            s.Close();
            sessions.Close();
        }
        // ...
    }
}


hibernate.cfg.xml :

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.0" >
   <session-factory name="localfactory">
      <!-- properties -->
      <property name="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="hibernate.dialect">NHibernate.Dialect.Oracle9Dialect</property>
      <property name="hibernate.connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
      <property name="hibernate.connection.connection_string">User ID=Owner_Sgl_6;Password=idem;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp) (HOST=10.99.100.5)(PORT=1520))(CONNECT_DATA=(SERVICE_NAME=ANAPDEV)))</property>
      <property name="show_sql">false</property>
      <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<!--      <property name="use_outer_join">false</property> -->
<!--      <property name="max_fetch_depth">3</property> -->
<!--      <property name="use_reflection_optimizer">false</property> -->
      <!-- mapping files -->
      <mapping assembly="NHibernate.Test" />
   </session-factory>
</hibernate-configuration>


When I create the ISessionFactory variable, it takes 30-50 SECONDS !!

I test some options in the cfg.xml file but it's still the same. Have you got an idea ?

Thank's
Xavier.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 14, 2007 3:29 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Creating a SessionFactory *is* expensive and should only be done once for the lifetime of the application (in general). You then create Sessions from the factory when you need them.

You certainly shouldn't create a new SessionFactory for every Session.

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 14, 2007 10:24 am 
Beginner
Beginner

Joined: Tue Jul 10, 2007 5:27 am
Posts: 34
Location: Belgium
creating a SessionFactory is expensive, but 30-50 seconds is a long time. Do you have a very large amount of mapping files or something?

_________________
Davy Brion
http://ralinx.wordpress.com


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 15, 2007 5:10 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
I think we need to be more precise here? How do we know exactly what's taking 50 seconds - there's no timings in your code.
It could be taking 50 seconds to log in to your database.
Have you eliminated obvious problems with your database connection that you might be having from your machine? Like by using a profiler or something?


Top
 Profile  
 
 Post subject: Precisions and ideas
PostPosted: Mon Jul 16, 2007 3:43 am 
Newbie

Joined: Fri Jul 13, 2007 11:53 am
Posts: 4
30-50 seconds is only on the BuildSessionFactory(). I've tested the SQLPlus connection and it's fast. I have 220 tables in the database schema and some transitive relations between tables.

So, it is possible to create the SessionFactory at the server's start and to get it on the client side each time it'is necessary ?

Xavier.


Top
 Profile  
 
 Post subject: Precisions and ideas
PostPosted: Mon Jul 16, 2007 3:44 am 
Newbie

Joined: Fri Jul 13, 2007 11:53 am
Posts: 4
30-50 seconds is only on the BuildSessionFactory(). I've tested the SQLPlus connection and it's fast. I have 220 tables in the database schema and some transitive relations between tables.

So, is it possible to create the SessionFactory at the server's start and to get it on the client side each time it'is necessary ?

Xavier.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 3:48 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
I think it might be necessary to explain the architecture of your application for anyone to answer that properly. Is it a web app, are you using remoting, etc.

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 4:17 am 
Newbie

Joined: Fri Jul 13, 2007 11:53 am
Posts: 4
You're right.

It's a 3-layers C# WinForm project. Servers and PCs are on the same local network. There's no remoting.
After, we will develop a web module.

Xavier.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 4:27 am 
Beginner
Beginner

Joined: Tue Jul 10, 2007 5:27 am
Posts: 34
Location: Belgium
if i were you i'd go for the 'disconnected' approach... keep the SessionFactory and the Sessions completely server side, and return your objects to the client layer... let the client layer manipulate the objects in whichever way needed and then send them back to the server. Re-attach your objects to a new nhibernate session server-side and persist the objects. Keep the session lifetimes as short as possible (eg, one session per round-trip)

There are some pitfalls to this approach... You need to explicitely load every lazy-loaded association that will be needed in the current user story server-side before sending your objects back to the client. If you try to access a lazy-loaded property which wasn't initialized server-side you'll get a LazyInitializationException. Another way is to simply go back to the server if you realize you need more related data. Also, you'll lose the NHibernate default dirty tracking. When you reattach your objects to a new session, NHibernate has no way of checking which ones are dirty and which ones aren't. So you'll probably want to implement your own dirty tracking mechanism (which is fairly easy to do even though it is a bit tedious).

For some, this might not seem like a good approach... it's certainly not the easiest way to use nhibernate, but this way you can make sure your clients can't access the database without going through the application server first.

_________________
Davy Brion
http://ralinx.wordpress.com


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