private void executeBatches(Optional<PreparedStatement> upsertStatement) throws SQLException { if (upsertStatement.isPresent()) { try { upsertStatement.get().executeBatch(); } finally { upsertStatement.get().close(); } } }
protected void insertProperties(Map<String, String> mergedMap, Connection connection, long eventId) throws SQLException { Set<String> propertiesKeys = mergedMap.keySet(); if (propertiesKeys.size() > 0) { PreparedStatement insertPropertiesStatement = null; try { insertPropertiesStatement = connection.prepareStatement(insertPropertiesSQL); for (String key : propertiesKeys) { String value = mergedMap.get(key); insertPropertiesStatement.setLong(1, eventId); insertPropertiesStatement.setString(2, key); insertPropertiesStatement.setString(3, value); if (cnxSupportsBatchUpdates) { insertPropertiesStatement.addBatch(); } else { insertPropertiesStatement.execute(); } } if (cnxSupportsBatchUpdates) { insertPropertiesStatement.executeBatch(); } } finally { closeStatement(insertPropertiesStatement); } } }
@Override public boolean addBatch() throws SQLException { pstmt.addBatch(); pstmt.clearParameters(); batchCount++; if (batchCount % maxBatchSize == 0L) { pstmt.executeBatch(); pstmt.getConnection().commit(); return true; } return false; }
public void insertFlashedTokens(FlashedToken[] flashedTokenList) throws Exception { try (Connection connection = ds.getConnection(); PreparedStatement ps = connection.prepareStatement(insertToken)) { for (FlashedToken flashedToken : flashedTokenList) { insert(ps, flashedToken); ps.addBatch(); } ps.executeBatch(); connection.commit(); } }
private void writeVectorClock(Connection connection, long databaseVersionId, VectorClock vectorClock) throws SQLException { try (PreparedStatement preparedStatement = getStatement(connection, "databaseversion.insert.all.writeVectorClock.sql")) { for (Map.Entry<String, Long> vectorClockEntry : vectorClock.entrySet()) { preparedStatement.setLong(1, databaseVersionId); preparedStatement.setString(2, vectorClockEntry.getKey()); preparedStatement.setLong(3, vectorClockEntry.getValue()); preparedStatement.addBatch(); } preparedStatement.executeBatch(); } }
@Override public void addPatterns(Map<String, Map<Integer, Set<E>>> pats){ try { Connection conn = null; PreparedStatement pstmt = null; conn = SQLConnection.getConnection(); pstmt = getPreparedStmt(conn); for (Map.Entry<String, Map<Integer, Set<E>>> en : pats.entrySet()) { addPattern(en.getKey(), en.getValue(), pstmt); pstmt.addBatch(); } pstmt.executeBatch(); conn.commit(); pstmt.close(); conn.close(); } catch (SQLException | IOException e) { throw new RuntimeException(e); } }
void asProvided(Connection connection) throws SQLException { Statement createStmt = null; PreparedStatement insertStmt = null; try { createStmt = connection.createStatement(); createStmt.executeUpdate("CREATE..."); insertStmt = connection.prepareStatement("INSERT..."); insertStmt.executeBatch(); } finally { if (createStmt != null) createStmt.close(); if (insertStmt != null) { insertStmt.close(); } } } @NoWarning("OBL_UNSATISFIED_OBLIGATION")
stat.execute("CREATE TABLE tiles (key INTEGER PRIMARY KEY, provider TEXT, tile BLOB)"); stat.close(); final PreparedStatement prep = conn.prepareStatement("insert into tiles values (?, ?, ?);"); File[] listFiles = pFolderToPut.listFiles(); if (listFiles != null) { prep.setLong(1, index); final String provider = s[s.length - 4]; prep.setString(2, provider); final byte[] image = new byte[(int) listFiles2[yf].length()]; final FileInputStream str = new FileInputStream(listFiles2[yf]); prep.executeBatch(); conn.setAutoCommit(true); conn.close();
protected void insertThrowable(IThrowableProxy tp, Connection connection, long eventId) throws SQLException { PreparedStatement exceptionStatement = null; try { exceptionStatement = connection.prepareStatement(insertExceptionSQL); short baseIndex = 0; while (tp != null) { baseIndex = buildExceptionStatement(tp, baseIndex, exceptionStatement, eventId); tp = tp.getCause(); } if (cnxSupportsBatchUpdates) { exceptionStatement.executeBatch(); } } finally { closeStatement(exceptionStatement); } } }
@Override public void remove(List<Long> pks) { PreparedStatement statement = null; try { this.connection.setAutoCommit(false); statement = this.connection.prepareStatement("DELETE FROM t_transaction WHERE offset = ?"); for (long pk : pks) { statement.setLong(1, pk); statement.addBatch(); } int[] executeBatch = statement.executeBatch(); this.connection.commit(); } catch (Exception e) { log.warn("createDB Exception", e); } finally { if (null != statement) { try { statement.close(); } catch (SQLException e) { } } } }
public void insertRedeems(List<Redeem> redeemList) { try (Connection connection = ds.getConnection(); PreparedStatement ps = connection.prepareStatement(insertRedeemToken)) { for (Redeem redeem : redeemList) { insert(ps, redeem); ps.addBatch(); } ps.executeBatch(); connection.commit(); } catch (Exception e) { log.error("Error inserting redeems data in DB.", e); } }
public void removeSpecificFileVersions(Map<FileHistoryId, List<FileVersion>> purgeFileVersions) throws SQLException { if (purgeFileVersions.size() > 0) { try (PreparedStatement preparedStatement = getStatement(connection, "fileversion.delete.all.removeSpecificFileVersionsByIds.sql")) { for (FileHistoryId purgeFileHistoryId : purgeFileVersions.keySet()) { for (FileVersion purgeFileVersion : purgeFileVersions.get(purgeFileHistoryId)) { preparedStatement.setString(1, purgeFileHistoryId.toString()); preparedStatement.setLong(2, purgeFileVersion.getVersion()); preparedStatement.addBatch(); } } preparedStatement.executeBatch(); } } }
@Test public void testBatchUpdateWithInClause() throws Exception { @SuppressWarnings("unchecked") Map<String, Object>[] parameters = new Map[2]; parameters[0] = Collections.singletonMap("ids", Arrays.asList(1, 2)); parameters[1] = Collections.singletonMap("ids", Arrays.asList(3, 4)); final int[] rowsAffected = new int[] {1, 2}; given(preparedStatement.executeBatch()).willReturn(rowsAffected); given(connection.getMetaData()).willReturn(databaseMetaData); JdbcTemplate template = new JdbcTemplate(dataSource, false); namedParameterTemplate = new NamedParameterJdbcTemplate(template); int[] actualRowsAffected = namedParameterTemplate.batchUpdate( "delete sometable where id in (:ids)", parameters ); assertEquals("executed 2 updates", 2, actualRowsAffected.length); InOrder inOrder = inOrder(preparedStatement); inOrder.verify(preparedStatement).setObject(1, 1); inOrder.verify(preparedStatement).setObject(2, 2); inOrder.verify(preparedStatement).addBatch(); inOrder.verify(preparedStatement).setObject(1, 3); inOrder.verify(preparedStatement).setObject(2, 4); inOrder.verify(preparedStatement).addBatch(); inOrder.verify(preparedStatement, atLeastOnce()).close(); verify(connection, atLeastOnce()).close(); }
@NoWarning("OBL_UNSATISFIED_OBLIGATION") void falsePositive2(Connection connection) throws SQLException { Statement createStmt = null; PreparedStatement insertStmt = null; try { createStmt = connection.createStatement(); createStmt.executeUpdate("CREATE..."); insertStmt = connection.prepareStatement("INSERT..."); insertStmt.executeBatch(); } finally { try { if (createStmt != null) createStmt.close(); } finally { if (insertStmt != null) { insertStmt.close(); } } } } }
/** * @param start included * @param end excluded */ private static void batchExecute(long start, long end, PreparedStatement preparedStatement, Connection connection, Preparer preparer) throws SQLException { for (long i = start; i < end; i++) { preparer.prepare(preparedStatement, i); preparedStatement.addBatch(); if (i % 250 == 0) { preparedStatement.executeBatch(); connection.commit(); } } preparedStatement.executeBatch(); connection.commit(); }
private void executeBatch(PreparedStatement p, String message) throws SQLException { if (p == null) { return; } try { int[] rc = p.executeBatch(); for (int i = 0; i < rc.length; i++) { int code = rc[i]; if (code < 0 && code != Statement.SUCCESS_NO_INFO) { throw new SQLException(message + ". Response code: " + code); } } } finally { try { p.close(); } catch (Throwable ignored) {} } }
public boolean insertTokenHostBatch(List<ForwardingTokenEntry> entries) { try (Connection connection = ds.getConnection(); PreparedStatement ps = connection.prepareStatement(insertTokenHostProject)) { for (ForwardingTokenEntry entry : entries) { ps.setString(1, entry.token); ps.setString(2, entry.host); ps.setString(3, entry.email); ps.setInt(4, entry.dashId); ps.setInt(5, entry.deviceId); ps.addBatch(); } ps.executeBatch(); connection.commit(); return true; } catch (Exception e) { log.error("Error insert token host. Reason : {}", e.getMessage()); } return false; }
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { if (queryTimeoutSecs > 0) { preparedStatement.setQueryTimeout(queryTimeoutSecs); preparedStatement.addBatch(); int[] results = preparedStatement.executeBatch(); if (Arrays.asList(results).contains(Statement.EXECUTE_FAILED)) { connection.rollback();
/** * Removes all file versions with versions <b>lower or equal</b> than the given file version. * * <p>Note that this method does not just delete the given file version, but also all of its * previous versions. */ public void removeFileVersions(Map<FileHistoryId, FileVersion> purgeFileVersions) throws SQLException { if (purgeFileVersions.size() > 0) { try (PreparedStatement preparedStatement = getStatement(connection, "fileversion.delete.all.removeFileVersionsByIds.sql")) { for (Map.Entry<FileHistoryId, FileVersion> purgeFileVersionEntry : purgeFileVersions.entrySet()) { FileHistoryId purgeFileHistoryId = purgeFileVersionEntry.getKey(); FileVersion purgeFileVersion = purgeFileVersionEntry.getValue(); preparedStatement.setString(1, purgeFileHistoryId.toString()); preparedStatement.setLong(2, purgeFileVersion.getVersion()); preparedStatement.addBatch(); } preparedStatement.executeBatch(); } } }
@Test public void testBatchUpdateWithCollectionOfObjects() throws Exception { final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; final List<Integer> ids = Arrays.asList(100, 200, 300); final int[] rowsAffected1 = new int[] {1, 2}; final int[] rowsAffected2 = new int[] {3}; given(this.preparedStatement.executeBatch()).willReturn(rowsAffected1, rowsAffected2); mockDatabaseMetaData(true); ParameterizedPreparedStatementSetter<Integer> setter = (ps, argument) -> ps.setInt(1, argument.intValue()); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); int[][] actualRowsAffected = template.batchUpdate(sql, ids, 2, setter); assertEquals("executed 2 updates", 2, actualRowsAffected[0].length); assertEquals(rowsAffected1[0], actualRowsAffected[0][0]); assertEquals(rowsAffected1[1], actualRowsAffected[0][1]); assertEquals(rowsAffected2[0], actualRowsAffected[1][0]); verify(this.preparedStatement, times(3)).addBatch(); verify(this.preparedStatement).setInt(1, ids.get(0)); verify(this.preparedStatement).setInt(1, ids.get(1)); verify(this.preparedStatement).setInt(1, ids.get(2)); verify(this.preparedStatement).close(); verify(this.connection, atLeastOnce()).close(); }