From fddb2102776b69fbbf8222302cb94279f86bd421 Mon Sep 17 00:00:00 2001 From: franz1981 Date: Wed, 23 Sep 2020 18:29:35 +0200 Subject: [PATCH] ARTEMIS-2911 DB2 isn't replacing Blob data --- .../jdbc/store/file/Db2SequentialFileDriver.java | 13 +++++++++---- .../artemis/jdbc/store/sql/PropertySQLProvider.java | 5 +++++ .../artemis/jdbc/store/sql/SQLProvider.java | 2 ++ .../src/main/resources/journal-sql.properties | 1 + 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/Db2SequentialFileDriver.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/Db2SequentialFileDriver.java index 1043002f9f..c0066ab495 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/Db2SequentialFileDriver.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/Db2SequentialFileDriver.java @@ -19,6 +19,7 @@ package org.apache.activemq.artemis.jdbc.store.file; import javax.sql.DataSource; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.activemq.artemis.jdbc.store.sql.SQLProvider; @@ -26,6 +27,8 @@ import org.apache.activemq.artemis.jdbc.store.sql.SQLProvider; @SuppressWarnings("SynchronizeOnNonFinalField") public final class Db2SequentialFileDriver extends JDBCSequentialFileFactoryDriver { + private PreparedStatement replaceLargeObject; + public Db2SequentialFileDriver() { super(); } @@ -46,22 +49,24 @@ public final class Db2SequentialFileDriver extends JDBCSequentialFileFactoryDriv this.copyFileRecord = connection.prepareStatement(sqlProvider.getCopyFileRecordByIdSQL()); this.renameFile = connection.prepareStatement(sqlProvider.getUpdateFileNameByIdSQL()); this.readLargeObject = connection.prepareStatement(sqlProvider.getReadLargeObjectSQL()); + this.replaceLargeObject = connection.prepareStatement(sqlProvider.getReplaceLargeObjectSQL()); this.appendToLargeObject = connection.prepareStatement(sqlProvider.getAppendToLargeObjectSQL()); this.selectFileNamesByExtension = connection.prepareStatement(sqlProvider.getSelectFileNamesByExtensionSQL()); } @Override - public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException { + public int writeToFile(JDBCSequentialFile file, byte[] data, boolean append) throws SQLException { if (data == null || data.length == 0) { return 0; } + final PreparedStatement largeObjectStatement = append ? appendToLargeObject : replaceLargeObject; synchronized (connection) { try { connection.setAutoCommit(false); int bytesWritten; - appendToLargeObject.setBytes(1, data); - appendToLargeObject.setLong(2, file.getId()); - final int updatesFiles = appendToLargeObject.executeUpdate(); + largeObjectStatement.setBytes(1, data); + largeObjectStatement.setLong(2, file.getId()); + final int updatesFiles = largeObjectStatement.executeUpdate(); assert updatesFiles <= 1; connection.commit(); if (updatesFiles == 0) { diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProvider.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProvider.java index 118725a90d..7d461842e9 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProvider.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/PropertySQLProvider.java @@ -136,6 +136,11 @@ public class PropertySQLProvider implements SQLProvider { return format(sql("select-file-by-filename"), tableName); } + @Override + public String getReplaceLargeObjectSQL() { + return format(sql("replace-file"), tableName); + } + @Override public String getAppendToLargeObjectSQL() { return format(sql("append-to-file"), tableName); diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/SQLProvider.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/SQLProvider.java index 66af24be57..5f2f26f281 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/SQLProvider.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/SQLProvider.java @@ -44,6 +44,8 @@ public interface SQLProvider { String getSelectFileByFileName(); + String getReplaceLargeObjectSQL(); + String getAppendToLargeObjectSQL(); String getReadLargeObjectSQL(); diff --git a/artemis-jdbc-store/src/main/resources/journal-sql.properties b/artemis-jdbc-store/src/main/resources/journal-sql.properties index 831d4bd32d..43123792ba 100644 --- a/artemis-jdbc-store/src/main/resources/journal-sql.properties +++ b/artemis-jdbc-store/src/main/resources/journal-sql.properties @@ -91,6 +91,7 @@ table-names-case.oracle=upper create-journal-table.db2=CREATE TABLE %s(id BIGINT,recordType SMALLINT,compactCount SMALLINT,txId BIGINT,userRecordType SMALLINT,variableSize INTEGER,record BLOB(2G),txDataSize INTEGER,txData BLOB(2G),txCheckNoRecords INTEGER,seq BIGINT NOT NULL, PRIMARY KEY(seq)) max-blob-size.db2=2147483647 create-file-table.db2=CREATE TABLE %s (ID BIGINT GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1), FILENAME VARCHAR(255), EXTENSION VARCHAR(10), DATA BLOB(2G), PRIMARY KEY(ID)) +replace-file.db2=UPDATE %s SET DATA=? WHERE ID=? append-to-file.db2=UPDATE %s SET DATA = (DATA || ?) WHERE ID=? table-names-case.db2=upper