web-dev-qa-db-fra.com

Comment déboguer/journaliser les connexions du pool de connexions JDBC Tomcat?

J'utilise le pool de connexions Tomcat JDBC avec le démarrage Spring, le modèle JDBC et SQL Server. J'ai besoin de savoir ce qui se passe dans le pool de connexion pendant que l'application attend la connexion à la base de données. Tel que....

  • Nombre de connexions actives
  • Nombre de connexions inactives
  • Nombre de connexions bloquées, informations supplémentaires pour lesquelles cette connexion est bloquée
  • Nombre de connexions disponibles
  • et ...

Existe-t-il un moyen d'obtenir ces informations en déboguant ou en utilisant des infrastructures de journalisation comme log4j?

Toute idée sera appréciée.

20

Merci @Sundararaj Govindasamy pour une excellente réponse. Sur cette base, j'ai créé un composant dans mon application de démarrage Spring pour déboguer les informations de mon pool de bases de données.

import org.Apache.Tomcat.jdbc.pool.DataSource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DataSourceAspectLogger {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DataSource ds;

    @Before("execution(* br.com.foo.core.repository.*.*(..))")
    public void logBeforeConnection(JoinPoint jp) throws Throwable {
        logDataSourceInfos("Before", jp);
    }

    @After("execution(* br.com.foo.core.repository.*.*(..)) ")
    public void logAfterConnection(JoinPoint jp) throws Throwable {
        logDataSourceInfos("After", jp);
    }

    public void logDataSourceInfos(final String time, final JoinPoint jp) {
        final String method = String.format("%s:%s", jp.getTarget().getClass().getName(), jp.getSignature().getName());
        logger.info(String.format("%s %s: number of connections in use by the application (active): %d.", time, method, ds.getNumActive()));
        logger.info(String.format("%s %s: the number of established but idle connections: %d.", time, method, ds.getNumIdle()));
        logger.info(String.format("%s %s: number of threads waiting for a connection: %d.", time, method, ds.getWaitCount()));
    }
}
2
falvojr