简体   繁体   中英

How to log JDBC connection info on org.springframework.jdbc.CannotGetJdbcConnectionException

I'm working on an application which uses the Spring framework in combination with Ibatis and a C3P0 Connection pool. The system connects to about 12 seperate databases.

When one of the databases is unreachable, I get a stacktrace in the logfile which is generated by Spring (see below). However, this logging is missing very vital information on WHICH of the databases can not be reached.

Basically, I would like the jdbc connection string (sans Passwords) to be logged upon connection errors. Is there an easy way to tell Spring to log this?

Invocation of init method failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
        at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
        at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy.getConnection(TransactionAwareDataSourceProxy.java:109)
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
        at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:183)
        at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(SqlMapClientTemplate.java:242)

....

You could enable logging in the ORM layer of Spring, like so (assuming you use log4j):

log4j.logger.org.springframework.orm=trace,debugLog

You can also use P6Spy to log your database connections.

Lastly, how about setting a breakpoints on SQLException (break when this exception is thrown) in your IDE?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM