I am stuck with this very interesting requirement and need your inputs!
In our web application, the goal is to pass a user's company to a static method getSession() of a ConnectDatabase class that returns a hibernate session. One common area, the HttpSession is where we store the user's companyName as it is user specific but am unable to find a way to access it outside of it when needed.
Our entire application was originally built for handling just a single database and all works fine until we decided that we will begin handling multiple databases from the same application by passing the property, user's company name, based on which we would return the user's database specific sessionFactory session.
This implementation too was quite simple in the static method
changed to
where we would create a hashMap<
String, SessionFactory> where the key string is the company name.
Now comes the problem of changing the entire application where there are references to this method usually accessed as
ConnectDatabase.getSession()
to be changed to
ConnectDatabase.getSession(companyName) and what I was trying to achieve is a way to store this companyName in a dummy class initialized in a block of a final variable using a run time declaration as follows.
If I were able to achieve this I would be able to access the companyName of the user from any instance of this class CompanyName, however, the method that assigns the companyName it's value changes too every time this method a new instance of this class is created.
And, I cannot create a singleton class public static final companyName INSTANCE = new companyName(); as it would then refer to the latest added companyName instance
However, coming up with that method that initializes this final variable this way failed, and I don't see any way to achieve something similar to the following:
Or is there any other way to achieve this, please don't suggest me to change all of the 1000 methods to send the companyName to get the right sessionFactory as sometimes there is no access to get the companyName at all as there is no request, session variables available in utility classes needing access to the session factory and that have nothing to do with the presentation layer where you usually have access to request, session?
Cheers
Krishna