我有一个问题,当我在服务器上运行它时,我的sessionFactory.openSession()返回null。在本地机器上一切都很好,但我不知道服务器上的问题是什么。 (我甚至尝试用iptables关闭,所以防火墙不是它)
本地:一切正常 - > Win8,Tomcat 7.0.55,MySQL 5.6.20,JavaJDK1.7
服务器:不行 - > CentOS 6.5,Tomcat 6/7/8,MySql 5.1.73,Java 1.7.0_71
我正在使用hibernate和Struts 2。 我现在尝试使用Tomcat 6 - 8,但每次我都收到相同的错误..
Stacktraces
java.lang.NullPointerException
com.XXX.util.SessionUtil.getSession(SessionUtil.java:15)
SessionUtil:
package com.XXX.util;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.XXX.listener.HibernateListener;
public class SessionUtil {
public static Session getSession(){
SessionFactory sessionFactory =
(SessionFactory) ServletActionContext.getServletContext()
.getAttribute(HibernateListener.KEY_NAME);
return sessionFactory.openSession();
}
}
要初始化Hibernate工厂,我使用一个Listener,它在web.xml中的Startup上初始化。
HibernateListener:
package com.XXX.listener;
import java.net.URL;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateListener implements ServletContextListener{
private static final Logger logger = Logger.getLogger(HibernateListener.class);
private Configuration config;
private SessionFactory factory;
private String path = "/hibernate.cfg.xml";
private static Class clazz = HibernateListener.class;
public static final String KEY_NAME = clazz.getName();
public void contextDestroyed(ServletContextEvent event) {
//
}
public void contextInitialized(ServletContextEvent event) {
try {
URL url = HibernateListener.class.getResource(path);
config = new Configuration().configure(url);
factory = config.buildSessionFactory();
//save the Hibernate session factory into serlvet context
event.getServletContext().setAttribute(KEY_NAME, factory);
} catch (Exception e) {
logger.fatal("HibernateListener!!! Exception!!!", e);
System.out.println(e.getMessage());
}
}
}
和我的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">
<hibernate-configuration>
<session-factory >
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">PASSWORD</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/XXX</property>
<property name="hibernate.connection.username">USER</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- c3p0 -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.timeout">30</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">30</property>
<!-- ========== Mappings ========== -->
.
.
.
<!-- ========== /Mappings ========== -->
</session-factory>
</hibernate-configuration>
我的怀疑是
1. SessionUtil无法找到HibernateListener
2. jar文件可能有问题。 (我的所有jar文件都在WEBROOT - &gt; WEB-INF - &gt; LIB文件夹中)
我只是不知道该怎么办......
任何帮助都非常感谢!
谢谢你们!
答案 0 :(得分:0)
在struts.xml中使用basicStackHibernate拦截器,因为要访问Hibernate(Session和/或Transaction)。
<interceptor-ref name="defaultStackHibernate" />
可能会为你使用