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

on servlets and jdbc

 
pratap kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
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?
thanku
by
pratap
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
2.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic