Skip to content

Instantly share code, notes, and snippets.

@brettwooldridge
Last active March 20, 2018 16:40
Show Gist options
  • Save brettwooldridge/2085f7707564a37833cc6212c8485215 to your computer and use it in GitHub Desktop.
Save brettwooldridge/2085f7707564a37833cc6212c8485215 to your computer and use it in GitHub Desktop.
HikariCP Issue1118
package com.zaxxer.hikari.benchmark;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.*;
import java.util.concurrent.ThreadLocalRandom;
/**
* docker run --name some-mysql -e MYSQL_USER=brettw -e MYSQL_PASSWORD=secret -e MYSQL_DATABASE=issue1118 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -p 3306:3306 -d mysql:5.5
*/
public class Issue1118 {
private static final String jdbcUrl = "jdbc:mysql://localhost/issue1118";
private HikariDataSource DS;
public static void main(String[] args) throws Exception {
Issue1118 issue = new Issue1118();
issue.setupHikari();
issue.runTest();
issue.DS.close();
}
private void runTest() throws SQLException {
final ThreadLocalRandom random = ThreadLocalRandom.current();
final String sql = "INSERT INTO CUST_STATUS_CODE_ALARM(ALARM_ID,SCOPE,CACHE_GROUP,ISP,CACHE_IP,CHECK_RESULT,ORI_DATA) VALUES(?,?,?,?,?,?,?)";
try (Connection conn = DS.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
final long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
pstmt.setString(1, random.nextInt() + "bbb" + random.nextInt());
pstmt.setString(2, "");
pstmt.setString(3, "");
pstmt.setString(4, "");
pstmt.setString(5, "");
pstmt.setString(6, "");
pstmt.setString(7, "");
pstmt.addBatch();
}
pstmt.executeBatch();
conn.commit();
pstmt.close();
final long elapsed = (System.currentTimeMillis() - start);
System.out.printf("Insertion time: %dms\n", elapsed);
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM CUST_STATUS_CODE_ALARM")) {
if (rs.next()) {
System.out.printf("Select count(*) returned %d rows\n", rs.getInt(1));
}
}
}
}
private void setupHikari()
{
HikariConfig config = new HikariConfig();
config.setJdbcUrl(jdbcUrl);
config.setUsername("brettw");
config.setPassword("secret");
config.setAutoCommit(false);
config.addDataSourceProperty("rewriteBatchedStatements", "true");
DS = new HikariDataSource(config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment