sessionFactory 只需要 build 一次,用个 静态的 变量保存起来, 或者 <session-factory name="hibernate/sessionfactory" >
<!-- 这里给个 name , hibernate buildSessionFactory 之后就会把这个 sessionFactory 放置到 jndi 中去,他的 jndi 名就用这里指定的 name , 以后在其他地方就能够用 jndi 来取得 sessionFactory , 如: new InitialContext().lookup("java:comp/env/hibernate/sessionFactory");
<!-- .... -->
</session-factory >连接池 就是 在 hibernate.cfg.xml
中添加 <properties name="hibernate.datasource">jdbc/datasource</properties>
<!-- 这里给个 name , hibernate buildSessionFactory 之后就会把这个 sessionFactory 放置到 jndi 中去,他的 jndi 名就用这里指定的 name , 以后在其他地方就能够用 jndi 来取得 sessionFactory , 如: new InitialContext().lookup("java:comp/env/hibernate/sessionFactory");
<!-- .... -->
</session-factory >连接池 就是 在 hibernate.cfg.xml
中添加 <properties name="hibernate.datasource">jdbc/datasource</properties>
他仍然不能达到我的目的。Hibernate在每次查询的时候都会配置一次,很慢的说
package pojo;
import org.hibernate.*;
import org.hibernate.cfg.*;public class HibernateUtil {
private static final SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory
sessionFactory = new Configuration().configure("xml/hibernate.cfg.xml").buildSessionFactory();
} catch (HibernateException ex)
{
throw new RuntimeException( "Configuration problem: " + ex.getMessage(),ex );
}
}
public static Session currentSession() throws HibernateException
{
System.out.println("here is hibernateUtil ") ;
Session s = sessionFactory.openSession();
return s;
}
}