tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Martin O'Shea" <app...@dsl.pipex.com>
Subject Re: Connection pooling issue with MySQLNonTransientConnectionException and Java webapp
Date Mon, 21 Nov 2011 13:11:55 GMT
Usually the connection is initialised as null and then assigned inside

the try block. What happens if the method above throws an error after

a connection is removed from the pool?

 

 

To try to answer this, the sample code provided is illustrative of my DAO
classes generally. The following is a listing of my connection pool class:

 

package visualRSS.database;

 

import java.sql.*;

import javax.sql.DataSource;

import javax.naming.InitialContext;

import org.apache.log4j.Logger;

import visualRSS.entity_misc_classes.PropertiesFile;

 

public class ConnectionPool_DB {

 

    static final Logger logger =
Logger.getLogger(ConnectionPool_DB.class.getName());

 

    private static ConnectionPool_DB pool = null;

    private static DataSource dataSource = null;

    

    public synchronized static ConnectionPool_DB getInstance() {

        if (pool == null) {

            pool = new ConnectionPool_DB();

        }

        return pool;

    }

 

    private ConnectionPool_DB() {

        try {

            InitialContext ic = new InitialContext();

            dataSource = (DataSource)
ic.lookup(PropertiesFile.getProperty("visualRSS", "DATASOURCE"));

            // dataSource = (DataSource)
ic.lookup("java:/comp/env/jdbc/visualRSS");

        }

        catch(Exception ex) {

            logger.error("Error getting a connection pool's datasource\n",
ex);

        }

    }

 

    public void freeConnection(Connection c) {

        try {

            c.close();

        }

        catch (Exception ex) {

            logger.error("Error terminating a connection pool connection\n",
ex);           

        }

    }

    

    public Connection getConnection() {

        try {

            return dataSource.getConnection();

        }

        catch (Exception ex) {

            logger.error("Error getting a connection pool connection\n",
ex);            

            return null;

        }

    }    

}

 

For a typical error, I get a chain of stacktrace as follows:

 

ERROR|21 11 2011|12 49 53|http-8080-7|visualRSS.database.ConnectionPool_DB|
- Error getting a connection pool connection

 

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could
not create connection to database server. Attempted reconnect 3 times.
Giving up.

                at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

                at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAcces
sorImpl.java:39)

                at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstruc
torAccessorImpl.java:27)

                at
java.lang.reflect.Constructor.newInstance(Constructor.java:513)

                at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)

                at com.mysql.jdbc.Util.getInstance(Util.java:384)

                at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)

                at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)

                at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)

                at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)

                at
com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2226)

                at
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2127)

                at
com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)

                at
com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)

                at
sun.reflect.GeneratedConstructorAccessor11.newInstance(Unknown Source)

                at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstruc
torAccessorImpl.java:27)

                at
java.lang.reflect.Constructor.newInstance(Constructor.java:513)

                at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)

                at
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)

                at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)

                at
org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverC
onnectionFactory.java:38)

                at
org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableCon
nectionFactory.java:294)

                at
org.apache.tomcat.dbcp.pool.impl.GenericObjectPool.borrowObject(GenericObjec
tPool.java:1148)

                at
org.apache.tomcat.dbcp.dbcp.AbandonedObjectPool.borrowObject(AbandonedObject
Pool.java:84)

                at
org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSourc
e.java:96)

                at
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.ja
va:880)

                at
visualRSS.database.ConnectionPool_DB.getConnection(ConnectionPool_DB.java:47
)

                at visualRSS.database.User_DB.get(User_DB.java:127)

                at
visualRSS.database.Dataset_DB.mapDataset(Dataset_DB.java:580)

                at visualRSS.database.Dataset_DB.get(Dataset_DB.java:112)

                at
visualRSS.database.Visualisation_DB.mapVisualisation(Visualisation_DB.java:5
99)

                at
visualRSS.database.Visualisation_DB.get(Visualisation_DB.java:101)

                at
visualRSS.database.Visualisation_Recent_List_DB.mapVisualisationRecentList(V
isualisation_Recent_List_DB.java:205)

                at
visualRSS.database.Visualisation_Recent_List_DB.getVisualisationRecentList(V
isualisation_Recent_List_DB.java:108)

                at
org.apache.jsp.jsp.index.index_jsp._jspService(index_jsp.java:166)

                at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

                at
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

                at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:3
77)

                at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)

                at
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)

                at
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

                at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:290)

                at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:206)

                at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja
va:233)

                at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja
va:191)

                at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase
.java:558)

                at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)

                at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127
)

                at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102
)

                at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java
:109)

                at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)

                at
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:
859)

                at
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(H
ttp11AprProtocol.java:579)

                at
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)

                at java.lang.Thread.run(Thread.java:619)

Caused by:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data
source rejected establishment of connection,  message from server: "Too many
connections"

                at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

                at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAcces
sorImpl.java:39)

                at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstruc
torAccessorImpl.java:27)

                at
java.lang.reflect.Constructor.newInstance(Constructor.java:513)

                at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)

                at com.mysql.jdbc.Util.getInstance(Util.java:384)

                at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)

                at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)

                at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)

                at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1104)

                at
com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2172)

                ... 44 more

ERROR|21 11 2011|12 49 53|http-8080-7|visualRSS.database.User_DB| - Error
getting User 978937_19082010_1282218386857

 

java.lang.NullPointerException

                at visualRSS.database.User_DB.get(User_DB.java:137)

                at
visualRSS.database.Dataset_DB.mapDataset(Dataset_DB.java:580)

                at visualRSS.database.Dataset_DB.get(Dataset_DB.java:112)

                at
visualRSS.database.Visualisation_DB.mapVisualisation(Visualisation_DB.java:5
99)

                at
visualRSS.database.Visualisation_DB.get(Visualisation_DB.java:101)

                at
visualRSS.database.Visualisation_Recent_List_DB.mapVisualisationRecentList(V
isualisation_Recent_List_DB.java:205)

                at
visualRSS.database.Visualisation_Recent_List_DB.getVisualisationRecentList(V
isualisation_Recent_List_DB.java:108)

                at
org.apache.jsp.jsp.index.index_jsp._jspService(index_jsp.java:166)

                at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

                at
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

                at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:3
77)

                at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)

                at
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)

                at
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

                at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:290)

                at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:206)

                at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja
va:233)

                at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja
va:191)

                at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase
.java:558)

                at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)

                at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127
)

                at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102
)

                at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java
:109)

                at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)

                at
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:
859)

                at
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(H
ttp11AprProtocol.java:579)

                at
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)

                at java.lang.Thread.run(Thread.java:619)

ERROR|21 11 2011|12 49 53|http-8080-7|visualRSS.database.ConnectionPool_DB|
- Error terminating a connection pool connection

 

java.lang.NullPointerException

                at
visualRSS.database.ConnectionPool_DB.freeConnection(ConnectionPool_DB.java:3
8)

                at visualRSS.database.User_DB.get(User_DB.java:154)

                at
visualRSS.database.Dataset_DB.mapDataset(Dataset_DB.java:580)

                at visualRSS.database.Dataset_DB.get(Dataset_DB.java:112)

                at
visualRSS.database.Visualisation_DB.mapVisualisation(Visualisation_DB.java:5
99)

                at
visualRSS.database.Visualisation_DB.get(Visualisation_DB.java:101)

                at
visualRSS.database.Visualisation_Recent_List_DB.mapVisualisationRecentList(V
isualisation_Recent_List_DB.java:205)

                at
visualRSS.database.Visualisation_Recent_List_DB.getVisualisationRecentList(V
isualisation_Recent_List_DB.java:108)

                at
org.apache.jsp.jsp.index.index_jsp._jspService(index_jsp.java:166)

                at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

                at
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

                at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:3
77)

                at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)

                at
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)

                at
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

                at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:290)

                at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:206)

                at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja
va:233)

                at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja
va:191)

                at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase
.java:558)

                at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)

                at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127
)

                at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102
)

                at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java
:109)

                at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)

                at
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:
859)

                at
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(H
ttp11AprProtocol.java:579)

                at
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)

                at java.lang.Thread.run(Thread.java:619)

 

I hope this helps.


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message