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&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]