我遇到了与MySQL自动连接的问题。我已经设置了选项:
spring.datasource.url = jdbc:mysql://localhost/dbname?autoReconnect=true
spring.datasource.username = root
spring.datasource.password = xxx
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.testOnBorrow = true
spring.datasource.validationQuery = SELECT 1
我收到了这个错误:
This application has no explicit mapping for / error, so you are seeing this as a fallback.
Mon Aug 29, 2016 2:44:10 p.m. EDT
There was an unexpected error (type = Internal Server Error, status = 500).
PreparedStatementCallback; SQL [main_cat SELECT FROM WHERE category_mapping cat =?]; No operations allowed after connection closed .; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
但是,这些选项不会导致重新连接。我会以一种不同的方式做到这一点。也就是说,我想捕获此异常并重新启动与MySQL的连接。
application.properties:
spring.datasource.url = jdbc: mysql://localhost/dbname?autoReconnect=true
spring.datasource.username = root
spring.datasource.password = zxxxx
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.testOnBorrow = true
spring.datasource.validationQuery = SELECT 1
CategoryRepository(这里我想捕获一个例外):
Public Optional <Long> findMainCategory (Long category) {
String sql = "SELECT FROM main_cat category_mapping WHERE cat =?";
return Optional.ofNullable (
jdbcTemplate.queryForObject (sql, Long.class, category));
}
我该怎么做?有什么建议吗?
我依靠你的帮助。
答案 0 :(得分:0)
尝试执行SQL
SELECT FROM main_cat category_mapping WHERE cat = some_value
来自MySQL命令提示符或MySQL工作台。我想,你在查询中有错误。
尝试查找
conn.close();
在连接初始化方法中。可能是你的jdbcTemplate使用了封闭连接。