-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Multi SessionFactory
PostPosted: Tue Nov 24, 2009 5:34 am 
Newbie

Joined: Tue Nov 17, 2009 4:53 am
Posts: 7
Hello

I want to create a hashmap of sessions factory to create one factory for one datasource. but this not works, here the code of my class :

Code:
public class HibernateUtil {

   private static HashMap<String, SessionFactory> sessionFactoryMap = new HashMap<String, SessionFactory>();
   private static Configuration configuration = new Configuration();
   private static String datasource = null;
   public static final ThreadLocal session = new ThreadLocal();


   public static Session currentSession() throws HibernateException {

      System.out.println("currentSession 1 : " + datasource);
      return currentSession(datasource);
   }


   public static Session currentSession(String ds) throws HibernateException {

      datasource = ds;

      System.out.println("currentSession 2 : " + ds);

      SessionFactory s = sessionFactoryMap.get(ds);
      if (s == null) {

         try {
            configuration = new Configuration();
            configuration.configure("/hibernate.cfg.xml");

            //reconfiguration du datasource en fonction de l'utilisateur
            configuration.setProperty("hibernate.connection.datasource", "java:comp/env/" + ds);

            s = (SessionFactory) sessionFactoryMap.get(ds).openSession();
            s = configuration.buildSessionFactory();
            sessionFactoryMap.put(ds, s);
            session.set(sessionFactoryMap.get(ds));
         } catch (Exception ex) {
            System.out.println("ERREUR DE FACTORY : " + ex.getMessage());
         }

      }

      return (Session) s;
   }


   public static void closeSession() throws HibernateException {

      System.out.println("closeSession 1 : " + datasource);
      closeSession(datasource);
   }


   public static void closeSession(String ds) throws HibernateException {

      System.out.println("closeSession 2 : " + ds);

      datasource = ds;

      SessionFactory s = (SessionFactory) sessionFactoryMap.get(ds);
      session.set(null);
      if (s != null)
         s.close();
      sessionFactoryMap.remove(ds);
   }
}



Someone would have an idea of the problem ?

In advance thank you!


Top
 Profile  
 
 Post subject: Re: Multi SessionFactory
PostPosted: Tue Nov 24, 2009 5:45 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You did not post the error you get. Is it a NullPointerException in line
Code:
s = (SessionFactory) sessionFactoryMap.get(ds).openSession();

sessionFactoryMap.get(ds) was null before (you are inside your if- block) so why should it be not null at this line, without putting it in the map before...

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Multi SessionFactory
PostPosted: Tue Nov 24, 2009 9:34 am 
Newbie

Joined: Tue Nov 17, 2009 4:53 am
Posts: 7
Ok i solve the problem,but now : I create my SessionFactory well in my hashmap, but strangely, if I perform a login with a user using the datasource X, then other user with datasource Y and when i reloguing with the user using datasource X, this works partialy because sometimes the data in my datasource Y are displayed, tut it should be the data of datasource X :/


My HibernateUtil :

Code:


public class HibernateUtil {

   private static HashMap<String, SessionFactory> sessionFactoryMap = new HashMap<String, SessionFactory>();
   private static Configuration configuration = new Configuration();
   private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
   private static org.hibernate.SessionFactory sessionFactory;


public static void currentSession(String datasource) throws HibernateException {

      System.out.println("### CONFIG HIBERNATE AVEC DATASOURCE : " + datasource);

      try {

         if (!sessionFactoryMap.containsKey(datasource)) {
            System.out.println("SESSION FACTORY ABSENTE DE LA MAP, CREATION DE LA SESSION FACTORY");

            configuration = new Configuration();
            configuration.configure("/hibernate.cfg.xml");

            //reconfiguration du datasource en fonction de l'utilisateur
            configuration.setProperty("hibernate.connection.datasource", "java:comp/env/" + datasource);

            sessionFactory = configuration.buildSessionFactory();
            //on stock la session factory dans la map
            sessionFactoryMap.put(datasource, sessionFactory);
            System.out.println("NB SESSION FACTORY : " + sessionFactoryMap.size());

         } else {
            System.out.println("SESSION FACTORY PRESENTE DANS LA MAP");

            sessionFactory = sessionFactoryMap.get(datasource);
         }

      } catch (Exception e) {
         System.err.println("%%%% Error Creating SessionFactory %%%%");
         e.printStackTrace();
      }
   }
}


Top
 Profile  
 
 Post subject: Re: Multi SessionFactory
PostPosted: Sat Nov 28, 2009 7:45 pm 
Newbie

Joined: Wed Oct 21, 2009 7:33 am
Posts: 11
The problem lies on how your code manipulates the sessionFactory instance. The SessionFactory in your HibernateUtil is static, meaning the instance of this class member will be available to the whole application. Consider the use case below.

Step 1: User X logs in to the application using datasource X, sessionFactory is created/loaded. Current sessionFactory is tied to datasource X.
Step 2: User X is doing something on the application.
Step 3. User Y logs in to the application using datasource Y, new sessionFactory is created/loaded, replacing the old sessionFactory. Current sessionFactory is tied to datasource Y
Step 4: User X is surprised to see data from datasource Y, because the current sessionFactory is now tied to datasource Y.

I suggest you create separate instances of your sessionFactory per datasource so that there wouldn't be any overlap of your database data/transactions.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.