Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused servlet newbie

 
�yvind Valland
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find myself officially confused :-) Say, I have a web application that uses a controller servlet, and is session based. Different JSPs speak to this same servlet, and it directs requests based on a form parameter (type hidden, 'action'). Now, does every single request to the servlet create a new thread/instance? Or does the same servlet respond to all requests within that one session?
I want to use this controller servlet to establish one database connection (through a database pool) for each logged in user, and it seems to me that I can get into an awful lot of trouble if a new thread of the servlet runs for every request... but like I said, I am confused
If someone's got a spare minute to clarify how this works I'll buy them a virtual beer :-)
Thanks!
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by �yvind Valland:
I find myself officially confused :-) Say, I have a web application that uses a controller servlet, and is session based. Different JSPs speak to this same servlet, and it directs requests based on a form parameter (type hidden, 'action'). Now, does every single request to the servlet create a new thread/instance? Or does the same servlet respond to all requests within that one session?

The way I understand it:
- You have one servlet instance.
- You have a pool of threads, also known as "Worker thread pool"
when a request comes in, the container picks up one threat from the pool. That thread executes the service method of your servlet (which internally decides whether is a doPost() or doGet().
If you get another requests, the container picks up another worker thread from the pool and this thread executes in the same servlet.
So, you have one single instance.
When you're using the SingleThreadedModel interface (don't use it , performance is hit drastically), The container might create multiple instances of the same servlet, giving the impression that multiple requests are processed in parallel.

I want to use this controller servlet to establish one database connection (through a database pool) for each logged in user, and it seems to me that I can get into an awful lot of trouble if a new thread of the servlet runs for every request... but like I said, I am confused

You wanna establish 1 single database connection (connection pool), and let all the users use a connection from the pool whenever they need it. Was this what you tried to say?
hope this helps
 
�yvind Valland
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input! Things are much clearer now.
As for my database pool: This is maintained by WebLogic, my application server. Like you correctly deduced: I want to allow each connected user to have one open connection to this pool while logged in. The connection should be opened when the user logs in, and closed only when the user logs out. How can I do this?
Cheers!
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're using weblogic, I think you should refer to the documentation on how to create a datasource.
The connection should be opened when the user logs in, and closed only when the user logs out.
hmm.. I'd say :
"the user should get a connection from the pool, use it, and release when it is done. In this way other uses will be able to *reuse* the same connection."
this is an example in tomcat on how datasources work.
Hope this helps
 
�yvind Valland
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again!
I previously wrote a small web application with WebLogic where I did use a connection pool. In this application the user gets a connection from the pool everytime a query is performed. Once the query is done, the connection was returned to the pool.
Do I correctly deduce - from your last reply - that this is, after all, the better practice of the two?
Cheers,
�yvind
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by �yvind Valland:
Do I correctly deduce - from your last reply - that this is, after all, the better practice of the two?
Cheers,
�yvind

Well, this is the best practice, IMO (connection pool). But I'm not sure which two practices you're referring to.
 
�yvind Valland
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please excuse my english with stink (Eddie Murphy quote - from Trading Places)
I am not at all coherent today, it seems. My two 'practices' was
1. Getting a connection from the pool at login, and returning it upon logout.
2. Getting a connection when needed, and returning it as soon as it is no longer needed.
I'll go with number 2! Thanks again for your help!
�yvind
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# 2. You still owe me the virtual beer
 
�yvind Valland
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's yours mate, it's all yours (*hands you the beer*).
Now, if you can fix my problem with ISAM files in the JDBC thread (or find someone who can), I'll give you a case worth of those beers.
Thanks, Andres
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic