初学者问题:
1)web开发,哪里有靠谱的HibernateSessionFactory.java 及其对应的hibernate.cfg.xml?感觉自己手上的这两个文件的问题多多,各种debug,很耗时间
2) hibernate的jar包要放WEB-INF/lib目录下?又或者跟hibernate.cfg.xml里面的某个property的设置有关?
sever一开始起不来,网上搜了下,把hibernate的jar包要放WEB-INF/lib目录下,server就可以起来了。奇怪,struts的jar放WEB-INF/lib目录下可以理解,怎么hibernate的也要放在这个目录下?
开发环境
eclipse: Luna Service Release 1a (4.4.1)
mysql: 5.6
struts: 2.3.20
hibernate: 4.3.8
现象:
第一次从web访问,正常。说明struts, mysql工作都是正常的。
第二次起从web访问,报下面的错误。
将下面的代码注释掉后,再从web访问,每次正常了。问题是:一直不close SessionFactory 不会有问题吗? 是否要优化HibernateSessionFactory.java?
HibernateTestAction.java
//sf.close(); 即SessionFactory一直不close
Debug - execute - after getSessionFactory
Debug - execute - throw exception
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:201)
at org.hibernate.internal.AbstractSessionImpl.getJdbcConnectionAccess(AbstractSessionImpl.java:341)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.<init>(JdbcCoordinatorImpl.java:114)
at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.<init>(TransactionCoordinatorImpl.java:89)
at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:258)
代码:
HibernateSessionFactory.java
package hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
/**
* Configures and provides access to Hibernate sessions, tied to the current
* thread of execution. Follows the Thread Local Session pattern, see
* {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
System.out.println("sessionFactory - on buildSessionFactory");
try {
Configuration cfg = new Configuration().configure();
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();
SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.out.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
System.out.println("sessionFactory - on getSessionFactory");
if (sessionFactory == null)
System.out.println("build sessionFactory - fail");
else
System.out.println("build sessionFactory - succe");
return sessionFactory;
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!--
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
-->
<property name="cache.use_query_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/db_database10
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</property>
<property name="show_sql">true</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<mapping resource="mapping.xml" />
</session-factory>
</hibernate-configuration>
HibernateTestAction.java
public class HibernateTestAction extends ActionSupport implements ModelDriven<Book> {
...
public String execute() {
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
try {
System.out.println("Debug - execute - after getSessionFactory ");
Session s = sf.openSession();
s.beginTransaction();
System.out.println("Debug - execute - after beginTransaction ");
String hql = "from Book";
Query query;
query = s.createQuery(hql);
List<Book> books = query.list();
dataMap.put("data", books);
System.out.println("Debug - execute - query.list ");
for(Book bookTemp: books)
{
System.out.println(bookTemp.toString());
}
s.getTransaction().commit();
System.out.println("Debug - execute - after commit");
s.close();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Debug - execute - throw exception ");
e.printStackTrace();
}
sf.close();
dataMap.put("status", "SUCCESS");
return SUCCESS;
}
...
}