2016-12-08 49 views
0

它開始與主題中的錯誤信息。當我將connectionProperties添加到DriverManagerDataSource時,現在我得到質量在JdbcTemplate類的通信鏈路故障插入DB

線程「main」中的異常org.springframework.jdbc.CannotGetJdbcConnectionException:無法獲取JDBC連接;嵌套的例外是com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:無法創建連接到數據庫服務器。嘗試重新連接3次。放棄。

這種情況發生了很多成功的選擇和插入(幾千)之後。 我錯過了什麼,我做錯了什麼? MySQL連接是5.1.40,MySQL服務器是35年5月5日和Spring框架4.0.1

下面是我在做什麼:

private void doTheTransfer() { 
String requestStr = "SELECT edv_nr, edv_var, protokoll FROM HeaderBlock order by edv_nr, edv_var;"; 

JdbcTemplate jdbcTemplate = new JdbcTemplate(); 
jdbcTemplate.setDataSource(getMySQLDriverManagerDatasource()); 

List<HeaderBlockValueObject> rows = jdbcTemplate.query(requestStr, new HeaderBlockRowMapper()); 
int iEDVNr; 
int iEDVVar; 
String sProtokoll; 
String sqlEntries; 
String sqlInsert; 
List<EdvProtocolValueObject> existingEntries; 
RowMapper myRowMapper = new EdvProtocolRowMapper(true); 
for (HeaderBlockValueObject headerBlockValueObject : rows) { 
    iEDVNr  = headerBlockValueObject.getEdvNr(); 
    iEDVVar  = headerBlockValueObject.getEdvVar(); 
    sProtokoll = headerBlockValueObject.getProtokoll(); 

    if ((iEDVNr > 0) && (sProtokoll != null) && (!sProtokoll.trim().isEmpty())){ 
     int iNewProtocolID; 
     sqlEntries = "SELECT MIN(PROTOCOL_ID) FROM EDV_PROTOCOL" 
       + " where edv_nr = " + iEDVNr 
       + " and edv_var = " + iEDVVar; 
     existingEntries = jdbcTemplate.query(sqlEntries, myRowMapper); 
     if (existingEntries == null || existingEntries.isEmpty() 
       || existingEntries.get(0).getProtokoll_ID() > 0) { 
      iNewProtocolID = -1; 
     } else { 
      iNewProtocolID = (int) existingEntries.get(0).getProtokoll_ID() - 1; 
     } 
     sqlInsert = "insert into edv_protocol set EDV_NR = " + iEDVNr 
       + ", EDV_VAR = " + iEDVVar 
       + ", PROTOCOL_ID = " + iNewProtocolID 
       + ", DESCRIPTION = '" + sProtokoll + "'" 
       + ";"; 
     try{ 
      jdbcTemplate.execute(sqlInsert); 
      saveLogEntry(sqlInsert + " successful"); 
     } catch (DataAccessException exception) { 
      saveLogEntry(sqlInsert + " failed"); 
      saveLogEntry(exception.getLocalizedMessage()); 
     } 
    } 
} 

}

public DriverManagerDataSource getMySQLDriverManagerDatasource(){ 
DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
dataSource.setPassword("goforgold"); 
dataSource.setUrl("jdbc:mysql://localhost:3306/edvNewRelease"); 
dataSource.setUsername("root"); 
Properties connectionProperties = new Properties(); 
connectionProperties.setProperty("autoReconnect", "true"); 
connectionProperties.setProperty("maxActive", "750"); 
connectionProperties.setProperty("maxIdle", "30"); 
connectionProperties.setProperty("useUnicode", "true"); 
connectionProperties.setProperty("characterEncoding", "utf8"); 
connectionProperties.setProperty("validationQuery", "Select 1"); 
connectionProperties.setProperty("maxWait", "10000"); 
dataSource.setConnectionProperties(connectionProperties); 
return dataSource; 

}

+0

您是否嘗試過增加服務器服務的連接數? – Migsarmiento

+0

嘗試批量插入而不是運行循環 – kuhajeyan

回答

0

與使用Tomcat JDBC數據源(連接池),而不是使用DriverManagerDataSource(無連接池) How-to

解決了
相關問題