• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connection in servlets

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Akshay.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Adeel..

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


Akshay.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-
 
Akshay Bhatia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic