8 Replies Latest reply on May 24, 2011 2:39 PM by jean.baldessar

    Change Datasource by HttpSession

    jean.baldessar

      Hi.

       

       

      I have a web application in a three tier architecture with the state kept in HttpSession and the business logic in stateless session beans.

       

       

      The application uses JPA for persistence and the EntityManager is injected in the stateless session beans by the @PersistenceContext annotation.

       

       

      I need to change the datasource used acording with the user logged in the web application and I have no idea of how this can be possible.

      To pass a parameter that identifies the datasource to the stateless session beans is acceptable, but it's not desirable

      Will I have to use statefull session beans to keep the application state?

       

      I can't even change the datasource without change the persistence.xml...

      If the datasource cant be changed, I could change the persistence-unit to. Its all the same. I just dont know how to do it.

       

      some help?

        • 1. Change Datasource by HttpSession
          mp911de

          Hi Jean,

          if you have only a few Data Sources you could lookup your Persistence Context manually, skipping Dependency Injection (@PersistenceContext).

          Dependency Injection isn't built for too much dynamic.

           

          Best regards,

          Mark

          • 2. Re: Change Datasource by HttpSession
            jean.baldessar

            Hi Mark, thanks for replying.

             

             

            I think it will work for me. But there's one more thing to think about, maybe you can help me.

             

            There is a lot of stateless session beans accessing the database by dependency injection.

             

            The state of my application is kept in the HttpSession, so I think lookup the persistence context dynamically would envolve to pass some parameter that identifies the context I want to use and then do the lookup based on it. It requires a lot of refactoring in my stateless session beans methods to adding this parameter.

             

             

            Is it better to do this with some statefull session bean keeping the state of the selected persistence context for each logged user?

             

            I was thinking in implement a session listener that update this statefull session bean. It's a good idea?

             

            I dont have any patice with statefull session beans... how could I do it?

             

            Any suggestion?

            • 3. Change Datasource by HttpSession
              wdfink

              I've done SFSB in a webapp to store login and shopping carts for a session, but you have to cleanup the SFSB or adjust the timeout that it will be removed and the connection is going back to pool.

              Otherwise you get in trouble with it.

              1 of 1 people found this helpful
              • 4. Change Datasource by HttpSession
                mp911de

                Hi Jean,

                try to keep your Session-Data Front-Most. HttpSession is a good place for this (e. g. it's very easy to setup Http-Clustering and sharing this way the Session-Data across your servers). It's also the most common way. Sometime it sucks to pass always and always your Session-Stuff...

                As Wolf-Dieter said, when you go Stateful you need to maintain more things than you do now.

                 

                Best regards,

                Mark

                • 5. Re: Change Datasource by HttpSession
                  jean.baldessar

                  Ok, I think the main question is already responded.

                  Now i just need to think in a good way to modify my application without a big impact

                   

                  thanks Mark and Wolf-Dieter!

                  • 6. Re: Change Datasource by HttpSession
                    jean.baldessar

                    Mark, forgot to ask something important

                     

                    If I get my persistence context by dynamic lookup, will I have to begin and commit transactions and close my EntityManager? Or JBoss still doing it to me even without dependency injection?

                    • 7. Re: Change Datasource by HttpSession
                      mp911de

                      JBoss cares for you :-)

                      • 8. Re: Change Datasource by HttpSession
                        jean.baldessar

                        Good... Thanks Mark!