views:

228

answers:

1

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)

....

A: 

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?

Thierry-Dimitri Roy
Thanks for the tips. However I failed to mention that this is mainly a problem in the production environment, where trace logging and breakpoints are not allowed. Maybe I should file a change request on Spring because I think it is pretty reasonable to ask for informative logging.
Rolf