0

我正在开发一个将 Spring 框架与 Ibatis 和 C3P0 连接池结合使用的应用程序。该系统连接到大约 12 个独立的数据库。

当其中一个数据库无法访问时,我会在 Spring 生成的日志文件中获得堆栈跟踪(见下文)。但是,此日志记录缺少有关无法访问哪些数据库的非常重要的信息。

基本上,我希望在连接错误时记录 jdbc 连接字符串(无密码)。有没有一种简单的方法来告诉 Spring 记录这个?

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)

……

4

1 回答 1

1

您可以在 Spring 的 ORM 层中启用日志记录,如下所示(假设您使用 log4j):

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

您还可以使用P6Spy记录您的数据库连接。

最后,如何在 IDE 中设置 SQLException 断点(抛出此异常时中断)?

于 2010-08-10T13:53:46.157 回答