I am new to the field of web based application development.
I would like to know your valuable suggestion on the following issue.
I am developing an application using JSP and Java Beans. As the usual practice is, much of the business logics and computations are done in the Bean components. JDBC is also done using the bean components.
My issue is regarding the making and closing of database connections, i mean the Connection reference which is made using the DriverManager.getConnection().
Is it better to make a Connection object at the beginning of a user session (using a bean of "session" scope), and use that throughout for the rest of the DB access operations ?
Is it better to make Connection objects whenever it is needed?
I have this doubt because I've heard that making and breaking database connections are costly operations. which would be the most efficient one ?
Awaiting you suggestions,
u could do with some sort of connection pooling. create connections at server startup and reuse them as and when needed by various users.
the best is to make/get connections just when u need and break them as soon as u know, u dont them. but yes as u said making/breakin connections is a costly affair. well mostof the times u dont have to worry about that. most of the modern day servlet/jsp engines come with some kind of connection pool. so u can use the servlet engines/application servers underlying connection pool to get the connection and when u dont need them put them back into the pool. this way is pretty fast, beleive me it is.
hope this helps
Thanks for your suggestion. You have suggested to make use of the connection pool which the jsp engine/the application server has, and return it back when the use is over.
could you briefly explain how this is done ? or you have got any sample code for this ?
well most of the times the connection pool code is specific to the application server/servlet-jsp engine ur using. u will have to look into the documentation.
webshere/weblogic extensively use JNDI to hold references to database connections. webshere uses the javax.sql.DataSource class to get connections from the pool.
so u will have to look into the documentation of the server u r using. they surely shud have some sample code
You can find some info and sample code at technet.oracle.com and www.sybase.com
Thanks for all your valuable suggestions.