housepower / ClickHouse-Native-JDBC

ClickHouse Native Protocol JDBC implementation
https://housepower.github.io/ClickHouse-Native-JDBC/
Apache License 2.0
524 stars 145 forks source link

batch insert not support while using spring-jdbc #336

Closed Misaka10091 closed 3 years ago

Misaka10091 commented 3 years ago

Environment

Error logs

Steps to reproduce

Other descriptions

package com.github.housepower.jdbc;

public final class ClickHouseDatabaseMetadata implements SQLDatabaseMetadata {

    @Override
    public boolean supportsBatchUpdates() throws SQLException {
        return false;
    }
}
package org.springframework.jdbc.core;

public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {

    public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException {
        // ***
            if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
                for (int i = 0; i < batchSize; i++) {
                    pss.setValues(ps, i);
                    if (ipss != null && ipss.isBatchExhausted(i)) {
                        break;
                    }
                    ps.addBatch();
                }
                return ps.executeBatch();
            }
            else {
                List<Integer> rowsAffected = new ArrayList<>();
                for (int i = 0; i < batchSize; i++) {
                    pss.setValues(ps, i);
                    if (ipss != null && ipss.isBatchExhausted(i)) {
                        break;
                    }
                    rowsAffected.add(ps.executeUpdate());
                }
                int[] rowsAffectedArray = new int[rowsAffected.size()];
                for (int i = 0; i < rowsAffectedArray.length; i++) {
                    rowsAffectedArray[i] = rowsAffected.get(i);
                }
                return rowsAffectedArray;
            }
        }
        //****

    }
}   

spring-jdbc judge that batch insertion is not supported according to method com.github.housepower.jdbc.ClickHouseDatabaseMetadata.supportsBatchUpdates return false。 I found that the method ru.yandex.clickhouse.ClickHouseDatabaseMetadata.supportsBatchUpdates return true in the official driver; maybe it can return true to support batch insert?

pan3793 commented 3 years ago

Thanks for reporting this issue. Sounds like you are right. I will inspect it later.