2014-11-01 57 views
0

我有一个问题,当我在服务器上运行它时,sessionFactory.openSession()返回null。在本地机器上一切都很好,但我不知道服务器上的问题是什么。 (我甚至使用iptables关试了一下,所以防火墙是不是)Hibernate sessionFactory.openSession()返回null

地方:一切OK了 - >在Win8下,Tomcat 7.0.55中,MySQL 5.6.20,JavaJDK1.7
服务器:NOT OK - > CentOS 6.5,Tomcat 6/7/8,MySql 5.1.73,Java 1.7.0_71

我正在使用hibernate和Struts 2.我现在已经用Tomcat 6-8试过了,但每次我得到相同的错误..

踪迹
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的工厂,我使用它在web.xml上启动初始化监听器。

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 - > WEB-INF - > lib文件夹)


我只是不知道该怎么办......

任何帮助强烈感谢!
谢谢你们!

+0

您可以配置实用工具类使用静态会话工厂和更新休眠版本。 – 2014-11-01 19:10:40

+0

嘿,谢谢你!这是正确的方向!我现在已经把所有东西都变成了静态变体和evola! – AirmanAir 2014-11-01 22:59:26

回答

0

在struts.xml中使用basicStackHibernate拦截器是因为访问Hibernate(Session和/或Transaction)。

<interceptor-ref name="defaultStackHibernate" /> 

可能它会用全力为您