I have a website i am running using tomcat 7 and MYSQL. It runs on a dedicated server. I am using the simple connection pool and simple DAO's
classes outlined below:
I have also attached the screen shot of all the localhost connections on the server. I run another application on a different server using similar DAO's and the same connection pool and i only ever see 4 connections on that server.
Why would there be so many connections and is there a way to trace it back to my application to see why so many are spawning or is this normal?
public class ConnectionPool {
private static ConnectionPool pool = null;
private static DataSource dataSource = null;
public synchronized static ConnectionPool getInstance()
{
if (pool == null)
{
pool = new ConnectionPool();
}
return pool;
}
private ConnectionPool()
{
try
{
InitialContext ic = new InitialContext();
dataSource = (DataSource) ic.lookup("java:comp/env/jdbc/DBNAME");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Connection getConnection()
{
try
{
return dataSource.getConnection();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
return null;
}
}
public void freeConnection(Connection c)
{
try
{
c.close();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
catch (NullPointerException npe){
npe.printStackTrace();
}
}
}
DAO
public static DefaultMetaInfo loadDefaultMetaData() {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT META_TAG, TITLE_TAG FROM SITE_SETTINGS";
try {
ps = connection.prepareStatement(query);
rs = ps.executeQuery();
if (rs.next()){
DefaultMetaInfo settings = new DefaultMetaInfo();
settings.setMetaTag(rs.getString("META_TAG"));
settings.setTitleTag(rs.getString("TITLE_TAG"));
return settings;
}
} catch(SQLException e) {
e.printStackTrace();
return null;
} finally {
DaoUtil.closeResultSet(rs);
DaoUtil.closePreparedStatement(ps);
pool.freeConnection(connection);
}
return new DefaultMetaInfo();
}
context
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/DBNAME"
auth="Container"
type="javax.sql.DataSource"
maxActive="1200"
maxIdle="30"
maxWait="10000"
testOnBorrow="true"
validationQuery="select 1"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
username="user"
password="pw"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/dbname?autoReconnect=true"/>
</Context>