Spring JDBCTemplate批量插入DB时的通信链接失败

时间:2016-12-08 11:03:25

标签: java mysql spring connection

它从主题中的错误消息开始。当我将connectionProperties添加到DriverManagerDataSource时,现在我得到了

线程“main”中的异常org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接;嵌套异常是com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建与数据库服务器的连接。尝试重新连接3次。放弃。

这在很多成功的选择和插入(数千)之后发生。 我错过了什么,我做错了什么? mysql Connector是5.1.40,mysql Server是5.5.35和Spring Framework 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;

}

1 个答案:

答案 0 :(得分:2)

使用Tomcat JDBC数据源(连接池)而不是DriverManagerDataSource解决(没有连接池) How-to