When to open and when to close the mysql connection while using Java servlets? -


is ok open connection in init method , close in destroy method? what's best way open connection mysql database. i'm using this:

class.forname("com.mysql.jdbc.driver");  connection connect = drivermanager.getconnection("jdbc:mysql://" + ipaddress + "?user=" + user + "&password=" + pass); 

but read somewhere inefficient , should use connection pool. how that?

i suggest using connection pool, either explicitly (such c3p0) or 1 provided servlet container.

open database connection when need it, close can - connection pool take care of real network connection.

unless this, you'll end 1 connection whole application - means can process 1 query @ time, , code needs synchronize around database queries. surely want multiple entirely-independent queries able execute simultaneously? can't single connection.

as best way of opening connection having configured appropriate connection pool - may still end using drivermanager.getconnection(), specifying connection pool instead of mysql directly.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -