This is quite weird situation and i think its most common when handling mysql connection object. The below is the scenario
I get a connection object using apache DBCP connection pooling before executing sql statements. Everything works fine until the mysql connection timeout (8 hrs) occurs. So after this connection pooling does not return me any connection object, which makes any sql operation wait until the next restart of the tomcat server. Without DBCP we may get communicationException saying that no packets sent or received. That's the reason i've opted for apache DBCP thinking that it will manage the connection pooling. Below is my code
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.apache.commons.dbcp.BasicDataSource;
DataSource dataSource
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(driver);
ds.setUsername(username);
ds.setPassword(password);
ds.setUrl(url + "/" + dbname);
dataSource = ds;
logger.info("Getting connection from DBCP connection pool...");
con = dataSource.getConnection();
logger.info("MYSQL DB Connection Creation Successful...");
I've noticed that apache DBCP is using singleton pattern to create connection so new connection object is not created everytime i request. Kindly suggest me a way out and clarify me if i am doing something wrong. And i am not for the following solutions
- Increasing mysql timeout (bcoz mysql is common and i don't think increasing will be a good solution)
- Restarting tomcat server when problem occurs ( not a pretty solution as we can not be there always and no scripting too... :)
Kindly suggest me a way out. Thanks