使用Hibernate进行聚类:SEVERE:加载持久会话时出现IOException:java.io.InvalidObjectException:找不到SessionFactory

时间:2013-09-12 18:42:12

标签: hibernate tomcat jboss7.x

我让Hibernate使用单个Tomcat节点,但是当我尝试在群集中运行时,我收到此错误:

SEVERE: IOException while loading persisted sessions: java.io.InvalidObjectException: Could not find a SessionFactory [uuid=158c1e24-039b-4406-9a9b-cebb836100d1,name=null]
java.io.InvalidObjectException: Could not find a SessionFactory [uuid=158c1e24-039b-4406-9a9b-cebb836100d1,name=null]
at org.hibernate.internal.SessionFactoryImpl.locateSessionFactoryOnDeserialization(SessionFactoryImpl.java:1767)
at org.hibernate.internal.SessionFactoryImpl.readResolve(SessionFactoryImpl.java:1747)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeReadResolve(ObjectStreamClass.java:1091)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1786)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1970)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1894)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1777)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1970)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1894)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1777)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1970)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1894)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1777)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1970)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1894)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1777)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at org.apache.catalina.session.StandardSession.readObject(StandardSession.java:1485)
at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:987)
at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:394)
at org.apache.catalina.session.StandardManager.load(StandardManager.java:321)
at org.apache.catalina.session.StandardManager.start(StandardManager.java:648)
at org.apache.catalina.core.ContainerBase.setManager(ContainerBase.java:446)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
at org.mobicents.servlet.sip.startup.SipHostConfig.deployWAR(SipHostConfig.java:134)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
at org.mobicents.servlet.sip.startup.SipHostConfig.deployWARs(SipHostConfig.java:205)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
at org.mobicents.servlet.sip.startup.SipHostConfig.deployApps(SipHostConfig.java:55)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.mobicents.servlet.sip.startup.SipStandardEngine.start(SipStandardEngine.java:98)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.mobicents.servlet.sip.startup.SipStandardService.start(SipStandardService.java:290)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

这是我的HibernateUtil类,名为RecordingHibernateUtil:

public class RecordingHibernateUtil {

public static final String CONFIG_FILENAME = "hibernate-recording.cfg.xml";
public static final String RECORDING_DB_URL = "recording.db.url";
public static final String RECORDING_DB_DRIVER = "recording.db.driver";
public static final String RECORDING_DB_USERNAME = "recording.db.username";
public static final String RECORDING_DB_PASSWORD = "recording.db.password";

private static SessionFactory sessionFactory = null;

public static void buildSessionFactory() {
    if (sessionFactory != null) {
        //Already configured
        return;
    }
    Properties props;
    try {
        props = DBConfiguration.getInstance().getProperties();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Configuration config = new Configuration().configure(CONFIG_FILENAME);
    config.setProperty("hibernate.connection.url", 
            props.getProperty(RECORDING_DB_URL, 
                    props.getProperty(DBConfiguration.DB_URL)));
    config.setProperty("hibernate.connection.driver_class", 
            props.getProperty(RECORDING_DB_DRIVER, 
                    props.getProperty(DBConfiguration.DB_DRIVER)));
    config.setProperty("hibernate.connection.username", 
            props.getProperty(RECORDING_DB_USERNAME, 
                    props.getProperty(DBConfiguration.DB_USERNAME)));
    config.setProperty("hibernate.connection.password", 
            props.getProperty(RECORDING_DB_PASSWORD, 
                    props.getProperty(DBConfiguration.DB_PASSWORD)));
    try {
        sessionFactory = config.buildSessionFactory();
    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
}
}

从互联网搜索来看,它听起来像是在尝试序列化Hibernate SessionFactory,并且为了防止这种情况,建议使用JNDI查找来获取SessionFactory。它是否正确?我不知道为什么它还在尝试序列化SessionFactory。 (现在我将SessionFactory存储在HibernateUtil类的静态变量中......我更喜欢这样保留它。)

是否真的有必要使用JNDI?我该怎么做呢?我需要一个适用于Tomcat和JBoss(最好)的解决方案,或者每个都有一个单独的解决方案。

1 个答案:

答案 0 :(得分:2)

问题是,在某些时候,你的http会话会保留对SessionFactory的引用,并且它不会出现。 SessionFactory是从每个Hibernate Session引用的,所以我猜你在http会话中引用了一个Hibernate Session ...这很糟糕。

请记住:

  • http-session是一个长时间的生活对象(多个请求 - 响应循环)
  • 一个hibernate会话是短时间的生活对象(即使它可以完成:尝试在多个请求中重用相同的Hibernate会话是相当复杂的。)

所以你必须在Http Session中找到对Hibernate Session的引用:使用你的调试器并探索你的http会话的内容