Announcement Announcement Module
Collapse
No announcement yet.
Use different db/hibernate sessionfactory dynamically depending on user Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Hi Dan,

    I have exactly the same problem, and I have just one problem. How do you store the user data ?

    I wanted to stock it in the HttpSession, but the HibernateSessionFactoryBuilderService doesn't have access to.

    Thanks for your help.

    PS : I just need to stock a String for the user connected, to load the good hibernate connection properties.

    Comment


    • #17
      Can you explain more about what you are trying to do and what the problem is you are having?

      Comment


      • #18
        My problem is in the SessionFactoryProxy.java of Dan. Here he gets a IServiceContext, containing the sepcific datas of his connected user, allowing him to retrieve/create the good SessionFactory in his sessionFactoryBuilder. But I can't see how he gets his user's data in the ServiceContextHolder class.

        I tried to store my user's data with a ThreadLocal attribute in a class, but the thread are not the same during the navigation, so my data could be lost.

        So my problem is : How/Where to store the current user specific data ? (not in a HttpSession because the SessionFactoryProxy.java doesn't have access to HttpSession). My user logs in, I store somewhere his login or whatever, and at each request, I could retrieve the good SessionFactory in the JashMap with this login.

        Code:
        private SessionFactory getSessionFactory(){
        		if(sessionFactory.get()==null){
        			IServiceContext sc=ServiceContextHolder.getServiceContext();
        			if(sc==null){
        				throw new RuntimeException("ServiceContext is not set in AonSessionUtil class");
        			}
        			SessionFactory sf=sessionFactoryBuilder.getSessionFactory(sc);
        			Class sfInterface = SessionFactory.class;
        			if (sf instanceof SessionFactoryImplementor) {
        				sfInterface = SessionFactoryImplementor.class;
        			}
        			SessionFactory proxy= (SessionFactory) Proxy.newProxyInstance(sfInterface.getClassLoader(),
        					new Class[] {sfInterface}, new TransactionAwareInvocationHandler(sf));
        
        			sessionFactory.set(proxy);
        		}
        		return (SessionFactory) sessionFactory.get();
        	}

        Comment


        • #19
          I'm guessing this was implemented with a simple Filter. The Filter gets the value out of the Session and binds it to a ThreadLocal variable.

          Comment


          • #20
            Hi Dan,

            I am also facing simmillar kind of issue ( want to connect to different database based on the case that the user selects from the list of cases he has ). I looked in to your code that you have attached, can you please attach one of your DAO classe also along with those files, so that it will be helpfull for me to find how u are using this proxy in your DAO's.

            thanks in advance.

            with thanks and regards,
            krishna


            Originally posted by dan_feist View Post
            Ok. My soution is not nice but it works. It is based on the following assumptions...

            1) Spring services and DAO's should be singleton's not protype's.
            2) Multiple databases are used each having SAME SCHEMA.
            3) Database used depends on user but many users use same database.


            - I have ONLY one hibernate configuration file with database info missing.
            - I have a service which given user id returns correct session factory. Session factories are stored in a hashmap within this service. Session facotories are created for each databse using the base configuration file, the databse info is then set using configutation API before session factory is built and stored in hasmap.
            - I have a SessionFactory proxy which implements the SessionFactory interface and delegates to a session factory obtained from above service. As i cannot pass parameters in i have to store the user data needed for passing into above service in a ThreadLocal variablo.
            - I have this sessionfactory proxy defined in spring and injected into my DAO's and into the PlatformTransactionManager.

            Any help?
            Or any better ways to do this?

            Dan

            Comment

            Working...
            X