2

Continue solving this situation, I've updated hibernate ond sqljdbc4 jars and the exception is changed now (please take a look bellow).

I'm using

  • Hibernate v4.1.4 FINAL
  • JSF 2.1 (mojarra)
  • all my @ManagedBeans implements Serializable

It has some relation to @ViewScoped, because @SessionScoped beans are OK

What I've tried:

  • enable <Manager pathname=""/> in context.xml, and yes, the exception disappears, but I really lose persistent sessions, so I have to login again after tomcat server restart.

  • make changes in hiberanate.cfg.xml file (e.g. add name="java:hibernate/SessionFactory" to <session-factory> tag), but without success

  • play with SESSIONS.ser file, which is created after STOP the tomcat server.

    • When I open ser file, I can find there something like: .... uuidq ~ xppt $e8906373-f31b-4990-833c-c7680b2a77ce ...
    • if i START tomcat server again, it reports Could not find a SessionFactory [uuid=8906373-f31b-4990-833c-c7680b2a77ce,name=null] - WHY??? The session seems to be present in SESSION.ser file...

My session manager:

@SessionScoped
@ManagedBean(name="userManager")
public class UserManager implements Serializable {
  private static final long serialVersionUID = 1L;
  private transient HttpSession session;
  private String username;
  private String password;
  private User user;

  public String login() {
    // here is seraching for user in database (based on username and password)

    if (user != null) {
      FacesContext context = FacesContext.getCurrentInstance();
      session = (HttpSession) context.getExternalContext().getSession(true);
      session.setAttribute("id", user.getId());
      session.setAttribute("username", user.getName());
      ...
      return "success";
    } 
  }

  public String logout() {
    user = null;
    FacesContext context = FacesContext.getCurrentInstance();
    session = (HttpSession) context.getExternalContext().getSession(true);
    ...     
    session.invalidate();
    return "success";
  }
  // getters and setters ----------------------------------------------------
}

my hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
 <session-factory>
  <property name="connection.url">jdbc:sqlserver://localhost;databaseName=RXP</property>
  <property name="connection.username">user</property>
  <property name="connection.password">password</property>
  <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
  <property name="current_session_context_class">thread</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.hbm2ddl.auto">update</property>

  <mapping class="tables.User"/>

 </session-factory>
</hibernate-configuration>

and my session factory:

public final class DaoSF implements Serializable {
  private static final long serialVersionUID = 1L;

  private static SessionFactory sessionFactory;
  private static ServiceRegistry serviceRegistry;

  public static SessionFactory getSessionFactory() {
    try {
      Configuration configuration = new Configuration();
      configuration.configure("hibernate.cfg.xml");
      serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
      return sessionFactory;
    }
    catch (HibernateException he) { 
      ...
    }
  }
}

exception:

30.6.2012 13:29:07 org.apache.catalina.session.StandardManager doLoad
SEVERE: IOException while loading persisted sessions: java.io.InvalidObjectException: Could not find a SessionFactory [uuid=f9c33312-cf7f-4f65-ae5f-44261705c18e,name=null]
java.io.InvalidObjectException: Could not find a SessionFactory [uuid=f9c33312-cf7f-4f65-ae5f-44261705c18e,name=null]
    at org.hibernate.internal.SessionFactoryImpl.locateSessionFactoryOnDeserialization(SessionFactoryImpl.java:2007)
    at org.hibernate.internal.SessionFactoryImpl.deserialize(SessionFactoryImpl.java:2037)
....

I've tried to edit my hibernate.cfg.xml file this way:

<session-factory name="java:hibernate/SessionFactory">

And the error reportd has been changed to:

30.6.2012 13:38:23 org.apache.catalina.session.StandardManager startInternal
SEVERE: Exception loading sessions from persistent storage
java.lang.NullPointerException
    at java.util.concurrent.ConcurrentHashMap.get(Unknown Source)
    at org.hibernate.internal.SessionFactoryRegistry.getSessionFactory(SessionFactoryRegistry.java:140)
    at org.hibernate.internal.SessionFactoryRegistry.getNamedSessionFactory(SessionFactoryRegistry.java:135)
    at org.hibernate.internal.SessionFactoryImpl.locateSessionFactoryOnDeserialization(SessionFactoryImpl.java:2000)
    at org.hibernate.internal.SessionFactoryImpl.deserialize(SessionFactoryImpl.java:2037)
....

Don't you ever met with this error reports?

Community
  • 1
  • 1
gaffcz
  • 3,469
  • 14
  • 68
  • 108
  • Are you assigning the Hibernate session as a property of a view/session scoped managed bean? This is really strange design, but this exception suggests so. – BalusC Jun 30 '12 at 11:59
  • BalusC, could you take a look at my updated question, mainly to part about SER file? It seems to be ok, but it isn't :( – gaffcz Jul 01 '12 at 11:37
  • @gaffcz: You don't want instances of Session, SessionFactory or DaoSF to be serialized. They are transient and cannot be restored from a file. You better figure out where and why they are serialized to a file in the first place. That's where you need to make a fix. – Codo Jul 01 '12 at 12:09
  • @Codo: you're right, I'm checking this so long, that all my classes are serializabled now.. A few more minutes and I'll start to try to serialize js and css files :-P Thanks, I'll think about it – gaffcz Jul 01 '12 at 12:17
  • @Codo: Your advice helped me very much. UserManager seems not to have to be serializabled! I'll try it and exception is gone! Answer my question if you want and I'll accept it – gaffcz Jul 01 '12 at 12:44

2 Answers2

2

You don't want instances of Session, SessionFactory or DaoSF to be serialized. They are transient and cannot be restored from a file. You better figure out where and why they are serialized to a file in the first place. That's where you need to make a fix.

Codo
  • 75,595
  • 17
  • 168
  • 206
2

Another function is to update the context.xml under tomcat/conf/, the detail as below:

   <Manager className="org.apache.catalina.session.PersistentManager" saveOnRestart="false"   maxActiveSessions="-1" minIdleSwap="-1" maxIdleSwap="-1" maxIdleBackup="-1">
   <Store className="org.apache.catalina.session.FileStore"/>
   </Manager>
Tim Post
  • 33,371
  • 15
  • 110
  • 174
smallfatter
  • 171
  • 1
  • 1
  • 7
  • Hint: Highlight your code using the formatting tools in the editor. Your code wasn't visible because of this. – Tim Post Jan 29 '13 at 13:56