[英]SQLNonTransientConnectionException when database is unreachable using JDBC template
assuming the following:假设如下:
private static NamedParameterJdbcTemplate jdbcTemplate;
public static SqlRowSet foo(String value) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("paramName", value);
String query = "SELECT * from myTable WHERE paramName = :value";
try {
SqlRowSet rs = jdbcTemplate.queryForRowSet(query, params);
return rs;
} catch (CannotGetJdbcConnectionException | DataAccessException e) {
// do something
}
}
This use the Spring library JdbcTemplate to interact with a Database.这使用 Spring 库 JdbcTemplate 与数据库交互。 It works great when the connectivity to the database is up, but create a
SQLNonTransientConnectionException
exception if it is down (at line SqlRowSet rs = jdbcTemplate.queryForRowSet(query, params);
)当与数据库的连接启动时,它工作得很好,但如果它关闭,则会创建一个
SQLNonTransientConnectionException
异常(在SqlRowSet rs = jdbcTemplate.queryForRowSet(query, params);
)
java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=localhost)(port=63306)(type=master) : Connection refused
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:234)
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.connException(ExceptionMapper.java:95)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1203)
at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:560)
at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:174)
at org.mariadb.jdbc.Driver.connect(Driver.java:92)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:611)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:668)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:693)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForRowSet(NamedParameterJdbcTemplate.java:307)
Instead of printing the stack trace, I would like to catch the exception.我不想打印堆栈跟踪,而是想捕获异常。 However, adding a
catch (SQLNonTransientConnectionException)
give me an error saying that this exception is never thrown.但是,添加一个
catch (SQLNonTransientConnectionException)
会给我一个错误,说这个异常永远不会被抛出。 If I understand correctly, that mean there is a printStackTrace
somewhere in the Spring library and the exception is never thrown up to my method, so I can't do anything to catch it except by modifying the Spring library?如果我理解正确,那意味着 Spring 库中的某处有一个
printStackTrace
并且该异常永远不会被抛出到我的方法中,所以除了修改 Spring 库之外,我无法做任何事情来捕获它?
Thank you in advance for your help.预先感谢您的帮助。
The problem comes from the class HikariPool.java
at line 390.问题来自第 390 行的
HikariPool.java
类。
private void throwPoolInitializationException(Throwable t) {
this.LOGGER.error("{} - Exception during pool initialization.", this.poolName, t);
this.destroyHouseKeepingExecutorService();
throw new HikariPool.PoolInitializationException(t);
}
This log the stack trace and display it to the console.这会记录堆栈跟踪并将其显示到控制台。 Disable log in
application.properties
for this class solve the problem.禁用该类的登录
application.properties
解决问题。
logging.level.com.zaxxer.hikari.pool=OFF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.