开发者

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

开发者 https://www.devze.com 2023-01-11 11:56 出处:网络
I\'m working on an application which uses the Spring framework in combination with Ibatis and a C3P0 Connection pool. The system connects to ab开发者_JAVA技巧out 12 seperate databases.

I'm working on an application which uses the Spring framework in combination with Ibatis and a C3P0 Connection pool. The system connects to ab开发者_JAVA技巧out 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消