my dog learned polymorphism
The moose likes Servlets and the fly likes on servlets and jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "on servlets and jdbc" Watch "on servlets and jdbc" New topic

on servlets and jdbc

pratap kumar

Joined: Nov 09, 2000
Posts: 2
1.when u create a connection to database for e-mail services
how many connection objects are created?
can i establish the connection in init()method or service()method ?
2.with loading one driver can i establish different connection
with diff database. is it possible pl give the code also?
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
I'm not really sure what you are asking. You may find it useful to ask again in different words.
1. Databases have very little to do with e-mail. When you create a database connection through JDBC, you get a single connection to the specified database.
Whether you establish the connection in the init() method or the service() method depends on both the database driver and the way your servlet is to be used. If your database driver allows connections to be opened and left open for long periods, it may be feasible to open the connection in init(). If opening a connection is quick (say if you are using an in-VM database like InstantDB or HypersonicSQL) it may be better to open a connection each time in the service() method.
In general, the best method is to use a Connection Pool, which you initialize in the init(), and then request a connection from it in each service() call. The pool should take care of connections which are "timed-out" by the database driver, and create connections as necessary.
2. You can create several different database connections with most database drivers. You load the driver once, but create as many connections as you like, specifying the same driver, but different database, user and password.
I hope this has helped.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
I agree. Here's the link:
subject: on servlets and jdbc
jQuery in Action, 3rd edition