I have a problem with an automatic connection to MySQL. I set up already options:
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
I got this error:
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.
These options, however, do not cause reconnect. I would do it in a little different way. Namely, I want to catch this exception and restart connection to 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 (here I would like to catch an exception):
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));
}
How can I do this? Any suggestions ?
I'm counting on your help.