wood burning stoves 2.0*
The moose likes Servlets and the fly likes Connection in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Connection in servlets" Watch "Connection in servlets" New topic
Author

Connection in servlets

Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
Hi,

In which method we should create a connection object in a servlet ?

Akshay.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
What is the purpose of creating a Connection?
If you really need to connect to another URL, you should encapsulate creating the connection and getting the data in a helper class, not in a servlet method. That way you can test it outside the servlet environment - MUCH simpler.
Bill
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by William Brogden:
What is the purpose of creating a Connection?
If you really need to connect to another URL, you should encapsulate creating the connection and getting the data in a helper class, not in a servlet method. That way you can test it outside the servlet environment - MUCH simpler.
Bill


This also holds true if you are talking about a Database Connection.


GenRocket - Experts at Building Test Data
B Wiley Snyder
Ranch Hand

Joined: Nov 26, 2003
Posts: 50
Priyadharshini Nagarajan
Greenhorn

Joined: Sep 24, 2004
Posts: 22
Create a url connection in the service init() method so that it will be there all the time.

otherwise put the connection thing in the web.xml file ie deployment descriptor
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
Thanx Adeel..

However, if the same user will get 2 different connection objects, the data can be in a inconsistent state..


Akshay.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
no it is not about the user, it is about the thread.

if user1 request for the connection he/she will get connOne. next time if the same user1 request the connection he/she might get the other one, say, connTwo. it doesn't make any difference.

got it??
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
thanks for the inputs.


is it a good practice to create url/db connections in init() method ? plz. provide reasons for either case. Since I have seeen codes with and without connection being created in init() method.

Akshay
Jyothi Lature
Ranch Hand

Joined: Oct 31, 2001
Posts: 60
Hi,

Its a good practise to create a database connection in the init() method. The init() allows us to specify the DB URL, userid/password in the deployment descriptor(i.e., web.xml file). The servlet can get the values from the deployment descriptor at the time of initializing. We can avoid hardcoding these values in the servlet file. The values in the deployment descriptor can be changed as needed without affecting the servlet code. The servlet container initializes the servlet only once by calling the init().

By having the code of creating dbconnection in the service(), the connection object will be created for every request as service() gets called for every request recieved from the browser. Thus this will turn out to be very expensive and the performance will go down. This is a bad practise of coding.


Jyothi<br />Sun Certified Java Programmer<br />Brainbench Certified for Javascript
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
-
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
Thanx for the inputs Jyothi.

Please help me to clear my 2 doubts :-

1.)how will you manage multiple threads using the same db connection... don't you think it can corrupt the data.. or any of the running thread might cause an invalid transaction or something which causes the connection to close or puts it into an invalid state. will it not have an effect on the whole application..

2.)Suppose we synchronize the connection object...will it not degrade the performance.


Akshay.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
watch this simple traditional way,

get connection object in your method and close it before exiting the method. code that you would think must be threadsafe place that in your synchronize block. it is better to use synchronize blocks rahter than methods. it wouldn't render -ve impression on your perfomrance

note: for this you can search threads and synchronization forum


watch this pooling of connection using stack,

we have a stack of, say, 10 connections. we have two methods,

push()
pop()

now just make your push() and pop() methods threadsafe. so everythread would get a seperate conn object.
Akshay Bhatia
Greenhorn

Joined: Sep 24, 2004
Posts: 26
thanx adeel...

I need some help on..

1.)will the container will have number of threads=number of request.

if yes, what will happen if the same user opens 2 windows for buying 2 products from my shopping cart.

2.)which is greater.. Number of threads/ database connections..

I suppose Number of threads..plz.correct me if I am wrong.

watch this pooling of connection using stack,

we have a stack of, say, 10 connections. we have two methods,

push()
pop()

now just make your push() and pop() methods threadsafe. so everythread would get a seperate conn object.


what sort of technique does the container uses to manage a condition where number of request/threads is more than the number of db connections.

3.)After how much load(number of request) we should consider making an application as distributed.


Akshay.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
no no its not like that. in stack we can have 10 connections. say app users are 100. but it is very unlikely that all 100 users logged in at the same time, right. so 10 to 20 conn would be enough for the pool.

then it would be like
- get the connection from the pool
- use it
- put it back

so, it means all the users share the connection objects but not at the same time.

1. request for a servlet is not always a request for the connection.

2. usually there is a thread for a request, but we can not say this always. it depends on container implementation, how container manages the requests.

3. if the user opens 2 browser windows then the case could be either, that he would get 2 different conn objects, or he would get same conn object but not at the same time.
[ September 28, 2004: Message edited by: adeel ansari ]
 
jQuery in Action, 2nd edition
 
subject: Connection in servlets