-->
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.  [ 13 posts ] 
Author Message
 Post subject: How about obtaining session through connection pooling
PostPosted: Tue Apr 20, 2004 11:22 am 
Newbie

Joined: Mon Apr 19, 2004 11:27 pm
Posts: 9
Hi folks,

I worry now about the approach of obtaining session which confirmed available connection pooling with c3p0.
Is the connection pool available by the Singleton-Pattern used in order to get session ? (for example, HibernateUtil.java in 'Hibernate2 Reference Documentation')
It seems that only one connection is always reused.
How should the approach of session obtaining for using a connection pooling effectively be realized?

Is the following approach inappropriate?

Code:
public class HibernateUtil2 {
    private static ThreadLocalSession session = null;

    private static class ThreadLocalSession extends ThreadLocal {
        public Object initialValue() {
            System.out.println("invoked : ThreadLocalSession#initialValue()");
            Session ss = null;
            try {
                ss = new Configuration().configure().buildSessionFactory()
                    .openSession();
            }
            catch (HibernateException e) {
                e.printStackTrace();
            }
            return (ss);
        }
    }


    public static Session currentSession() {
        session = new ThreadLocalSession();
        return (Session) session.get();
    }


    public static void closeSession() throws HibernateException {
//            Session s = (Session) session.get();
//        session.set(null);
//        if (s != null) s.close();
    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 7:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I don't understand your problem, why don't you use the proven ThreadLocal pattern used in many samples and tutorials?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 8:19 pm 
Newbie

Joined: Mon Apr 19, 2004 11:27 pm
Posts: 9
I thank to your reply.
That is, can the Singleton pattern using ThreadLocal obtain a session from connection-pool appropriately?
If it which I feel like a problem is overanxiety, it is a good thing(no problems) .

The my first code referred to the tutorial:
Code:
public class HibernateUtil {
    private static HibernateUtil hibernateutilInstance__ = null;
    private static SessionFactory sessionfactoryInstance__ = null;
    private static final ThreadLocal threadlocalSession__ = new ThreadLocal();

    public static Session getCurrentSession() throws HibernateException {

        if (hibernateutilInstance__ == null) {
            hibernateutilInstance__ = new HibernateUtil();
        }

        Session sessionCurrent = (Session) threadlocalSession__.get();

        if (sessionCurrent == null) {
            if (sessionfactoryInstance__ == null) {
                sessionfactoryInstance__ = new Configuration().configure()
                    .buildSessionFactory();
            }
            sessionCurrent = sessionfactoryInstance__.openSession();
            threadlocalSession__.set(sessionCurrent);
        }
        if (!sessionCurrent.isOpen()) {
            sessionCurrent = sessionfactoryInstance__.openSession();
        }
        return sessionCurrent;
    }


    public static void closeCurrentSession() throws HibernateException {
        Session s = (Session) threadlocalSession__.get();
        threadlocalSession__.set(null);
        if (s != null) s.close();
    }
}


and config file(hibernate.cfg.xml):
Code:
<property name="c3p0.min_size">1</property>
<property name="c3p0.max_size">3</property>
<property name="c3p0.max_statements">10</property>
<property name="c3p0.timeout">100</property>
<property name="c3p0.validate">true</property>
<property name="hibernate.connection.provider_class">net.sf.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testdb?useUnicode=true&amp;characterEncoding=SJIS</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">2</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>


In above HibernateUtil class, when I use getCurrentSession() method which obtained session is from connection pool?

Best regards,
thx.
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 5:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hi, the Session is never from the Pool, only the underlying JDBC connection is from the pool. There is no need to pool Sessions, as they are very lightweight.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 10:47 pm 
Newbie

Joined: Mon Apr 19, 2004 11:27 pm
Posts: 9
I thank to your reply.
As for my application, many db-connection is predicted.
In such a case, if Hibernate is used, isn't there necessity of using a pool explicit?
How should I do the implementing if I need connection pooling?
Are the pointers to reference implementation anywhere?

Best regards,
Sho


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 7:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You are already using connection pooling in the config file you posted. I dont get your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 8:15 am 
Newbie

Joined: Mon Apr 19, 2004 11:27 pm
Posts: 9
Hi, michael.

Thank you very match your explanation.
I understand that I have no need especially implementation for pooling.

But I want to understand detailed mechanism of association between connection-pooling and hibernate-sessions.
Is there documents(URL) explained in detail about this matter?

Anyway, I thank to you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 8:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
hibernate session contains persistant objects + is a object oriented tool to query your domain model.

It transform object query in SQL and tranform the resultset in loaded objects.

For querying and persisting, it needs a jdbc connection that is obtained by the configured pool. The session also offers "transaction management" which is the same as pure jdbc transaction.

Now if you want detailed mechanism, you can debug or read the sources....


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 10:41 pm 
Newbie

Joined: Mon Apr 19, 2004 11:27 pm
Posts: 9
Hi, delpouve.

thx your information.
Is Hibernate session object using pooled JDBC-connection if I configured it in config-file?
For the moment, this is the my last question.

Best regards.
Sho


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 10:42 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why don't you read the documentation?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 11:43 pm 
Newbie

Joined: Mon Apr 19, 2004 11:27 pm
Posts: 9
I red documentation that is chapter3(3.2-3.4) in 'Hibernate2 Reference Documentation'.
and, I succeeded to use pooled connection:
Code:
SessionFactory sessions = cfg.buildSessionFactory();
Session sess = sessions.openSession();


If I want to use pooled session, should I use above code every time when getting the session?

very thx.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 11:48 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You should never pool sessions, because this is not how Hibernate is used. Read some of the articles/tutorials about Hibernate or the documentation.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 12:31 am 
Newbie

Joined: Mon Apr 19, 2004 11:27 pm
Posts: 9
Thanx.
I peruse tutorial/article/docs again.
I thank to having spent your time about my question.


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