2016-08-30 73 views
0

我有一個自動連接到MySQL的問題。我設置已經選擇:春季開機捕捉異常autoReconnect 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

請添加完整的stacktrace。另外我很想把它作爲http://stackoverflow.com/questions/30451470/connection-to-db-dies-after-424-in-spring-boot-jpa-hibernate/30455408#30455408的副本來關閉它。 –

+0

我沒有stacktrace。我只有這個錯誤。它不是重複的,因爲我嘗試使用鏈接中的所有選項,但它沒有奏效。我想捕獲異常並手動重新連接 – rad11

+0

由於取決於連接池,因此無法手動重新連接。我也懷疑你真的嘗試過所有的選擇(因爲你的屬性有幾個缺乏)。並且有一個堆棧跟蹤,請檢查您的控制檯而不是Web前端。 –

回答

0
  1. 嘗試執行SQL

    選擇main_cat category_mapping WHERE貓= SOME_VALUE

從MySQL命令提示符或MySQL的工作臺。我猜,你在查詢中有錯誤。

  • 試圖找到

    conn.close();

  • 在連接初始化方法。可能你的jdbcTemplate使用了關閉連接。