Factory inside a web application. Design issue.
Hi all,
My web application has a SearchManager. (which obviously doing search).
class SearchManager{
SearchFactory factory =new MySearchFactory();
public SomeClass onSubmit(HttpRequest request, HttpResponse response, SomeCl obj){
factory.search();
}
}
I need a connection (instance of MyConnection) for doing the search which is kept in the session.
My question is: which is the best option from the following.
Option 1
Send the HttpSession into the SearchFactory and retrieve MYConnection instance from session, and perform the search.
factory.search(searchCriteria, session)
Option 2
Retrieve the MYConnection instance from session inside the SearchController and send the MyConnection to SearchFactory and perform the search.
MyConnection connection = session.getAttribute("connection");
factory.search(searchCriteria, connection);
One point to note:
SearchManager class does not require the MyConnectioin instance for any reason. But the Factory also does not need the session except to get the connection instance.
Could you guys please discuss this and show me which is good and why?
Thanks in advance.
Kamal
# 1
Since this is your first post, would you dicsuss it with us "guys"? What do you think of option #3 and don't store connection in the session? Why do you store connection in session?
# 2
Hi,
Sorry, I couldn't think of any other way like option #3? My application does login to a separate application and do modifications into their db. Login info is retrieved at the login page. I have a separate class, say MyConnection for login and keeping that session from the external app. So I thought it would be good to store that MyConnection instance of a particular login in that user's session.
Please explain what is the best way to follow in this type of a situation?
Thanks.
# 3
> Please explain what is the best way to follow in this
> type of a situation?
There is no "best" way. There are many different ways to implement software. There may be some designs that are more efficient than others, but none can be the "best" because what is "best" is only an opinion. There is no metric that can prove "best". If you can't prove "best" then it is irrelevant and only an opinion of a human.
Everything you do should be the "best". I can't tell you want is "best" for you because I'm not you.
As far as your dilemma, a JNDI Datasource is another option. What is the maximum number of users that will use this application simultaneously?
# 4
Ok !!!
I'm looking for some solution for this and I have already come up with 2 ways. So that's why I wanted to check whether there is another way which would be better than this.
I'm not connecting to a db directly, as I mentioned previously. I'm connecting to another application. So I have to have the session that I got from the fore mentioned application.
Any suggestion?
Kamal
# 5
> I'm not connecting to a db directly, as I mentioned
> previously. I'm connecting to another application. So
> I have to have the session that I got from the fore
> mentioned application.
>
> Any suggestion?
As you mentioned, you are not connecting to a db directly, what suggestion about what are you asking? Are you using an application server?
# 6
I do a web service call using MyConnection class. Actually I have to use a class (WSClient class, which is already available for use) that calls the ws and returns the result.
So Inside MyConnection class I uses an instance of WSClient. So all the login, add, search and delete are done by MyConnection through the WSClient instance.
That's why I was thinking on keeping the instance of MyConnection in user's session which has been used for login by this particular user. As the WSClient instance inside MyConnection has been able to login (say for user1), it's ready for other operations, so user1's operations need to have a reference to that.
# 7
> So Inside MyConnection class I uses an instance of
> WSClient. So all the login, add, search and delete
> are done by MyConnection through the WSClient
> instance.
Does the MyConnection class have any JDBC code in it? Does it import any JDBC classes? Are you using an application server?
# 8
> > So Inside MyConnection class I uses an instance of
> > WSClient. So all the login, add, search and delete
> > are done by MyConnection through the WSClient
> > instance.
>
> Does the MyConnection class have any JDBC code in it?
> Does it import any JDBC classes? Are you using an
> application server?
No JDBC inside MyConnection. The WSClient class handles all the WS calls and we have to provide an instance of SearchBean class for that like below.
SearchBean s = new EmployeeSearch();
s.setValue1();
s.setValue2();.
.....
wsClient.search(searchBean);
# 9
Either of your options will work. Not much of a difference between them.
I would design the SearchManager class with a reference to the connection class and store a reference to the SearchManager in the Session object. It makes sense to me that the manager have a connection so that searches can be executed.
The manager will have references to the search factory and the search connection.
# 10
What is the difference between keeping the connection or the searchManager in the session? If the searchManager has a reference to connection wouldn't it be an extra burden to have the searchManager in the session rather than just having the connection object?
Now my question is: when we store an object in the session, what get stored in the session actually?
class SearchManager{
private MyConnection connection;
.....
}
Say above is my searchManager. If I store an instance of this will it store the connection attribute also inside the session? What actually get stored?
# 11
> If the searchManager has a reference to connection wouldn't
> it be an extra burden to have the searchManager in
> the session rather than just having the connection
> object?
Extra burden on what?
If you store a reference to the SearchManager or if you store a refernce to the connection object, there is no difference?
I personally feel that it is better to store the SearchManager. You can personally feel different.
> Now my question is: when we store an object in the
> session, what get stored in the session actually?
A reference to the object in the JVM gets stored.
> Say above is my searchManager. If I store an instance
> of this will it store the connection attribute also
> inside the session? What actually get stored?
When you store the reference to the SearchManager, the SearchManager has a reference to the connection object.
Only the reference to the SearchManager is stored in the sesssion.
and the connection reference is part of the SearchManager.
If you store the SearchManager reference in the session object, there is no need to explicity store a reference to the connection object becuase the SearchManager has this already.
