2016-12-08 10 views
0

トピック内のエラーメッセージで開始しました。 DriverManagerDataSourceにconnectionPropertiesを追加したので、今すぐ取得しますSpring JDBCTemplate DBへの一括挿入中の通信リンクのエラー

スレッド "main"の例外org.springframework.jdbc.CannotGetJdbcConnectionException:JDBC接続を取得できませんでした。ネストされた例外はcom.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExceptionです:データベースサーバーへの接続を作成できませんでした。再接続を3回試行しました。あきらめる。

これは、多数の選択と挿入が成功した後に発生します(数千件)。 何が不足していますか、何が間違っていますか? mysqlのコネクタが5.1.40で、MySQLサーバは4.0.1ここ

は私がやっているものです5.5.35およびSpringフレームワークです:

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

DriverManagerDataSource(接続プールなし)の代わりにTomcat JDBC Datasource(接続プーリング)を使用して解決しました。(接続プールなし) How-to