I'm working on a project using a Mysql database as the back end. My partner and I are wondering how best to handle the connections to the database. We have a class that handles creating objects populated with values from the database, and would also take objects to write them to the database.
We seem to have a disagreement about how to deal with the actual connection. She thinks we should do something like:
Where as I think inside of each method in the DBAO file we should use con=dataSource.getConnection; do stuff...; con.close();
We both think we should keep a copy of the DBAO object in the application or session. I'm just concerend about how and when the remove function would get called, that if it never got called the connection wouldn't be closed and would cause problems. She's concerned that opening and closing the connection too many times will cause performance problems.
why dont you use connection pooling. creat some connections cache it in HashMap and reuse it using JDCConnectionPool.java it will return available connection and if not available it will creat one go through for more detail..
Originally posted by Tad Dicks: Doesn't or whatever JSP container create a dbcp for you? and when you create the connection from the datasource isn't this coming from the dbcp?
Yes, in practice when you get a hold of a DataSource object, it is strapped on top of a connection pool of some sort. However, there are cases where the said connection pool has been configured with a limit of maximum number of connections set to just 1