From b8595d610df77271029669a3d8b7ed1b6ef9478f Mon Sep 17 00:00:00 2001 From: Martyn Taylor Date: Fri, 10 Feb 2017 14:27:26 +0000 Subject: [PATCH] ARTEMIS-957 Use setBytes JDBC API vs Concat BLOB --- .../store/drivers/derby/DerbySQLProvider.java | 9 -------- .../file/JDBCSequentialFileFactoryDriver.java | 23 ++++++++++++------- .../jdbc/store/sql/GenericSQLProvider.java | 2 +- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/derby/DerbySQLProvider.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/derby/DerbySQLProvider.java index ab61cc2e31..7e0ce58841 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/derby/DerbySQLProvider.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/derby/DerbySQLProvider.java @@ -26,16 +26,12 @@ public class DerbySQLProvider extends GenericSQLProvider { private final String createFileTableSQL; - private final String appendToFileSQL; - private DerbySQLProvider(String tableName) { super(tableName.toUpperCase()); createFileTableSQL = "CREATE TABLE " + tableName + "(ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)," + "FILENAME VARCHAR(255), EXTENSION VARCHAR(10), DATA BLOB, PRIMARY KEY(ID))"; - - appendToFileSQL = "UPDATE " + tableName + " SET DATA = DATA || ? WHERE ID=?"; } @Override @@ -48,11 +44,6 @@ public class DerbySQLProvider extends GenericSQLProvider { return createFileTableSQL; } - @Override - public String getAppendToLargeObjectSQL() { - return appendToFileSQL; - } - @Override public boolean closeConnectionOnShutdown() { return false; diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFileFactoryDriver.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFileFactoryDriver.java index eddf37c22e..a32e4a71a8 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFileFactoryDriver.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFileFactoryDriver.java @@ -74,7 +74,7 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { this.copyFileRecord = connection.prepareStatement(sqlProvider.getCopyFileRecordByIdSQL()); this.renameFile = connection.prepareStatement(sqlProvider.getUpdateFileNameByIdSQL()); this.readLargeObject = connection.prepareStatement(sqlProvider.getReadLargeObjectSQL()); - this.appendToLargeObject = connection.prepareStatement(sqlProvider.getAppendToLargeObjectSQL()); + this.appendToLargeObject = connection.prepareStatement(sqlProvider.getAppendToLargeObjectSQL(), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); this.selectFileNamesByExtension = connection.prepareStatement(sqlProvider.getSelectFileNamesByExtensionSQL()); } @@ -154,7 +154,8 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { try (ResultSet rs = readLargeObject.executeQuery()) { if (rs.next()) { - file.setWritePosition((int) rs.getBlob(1).length()); + Blob blob = rs.getBlob(1); + file.setWritePosition((int) blob.length()); } connection.commit(); } catch (SQLException e) { @@ -242,13 +243,19 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { */ public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException { synchronized (connection) { - try { - connection.setAutoCommit(false); - appendToLargeObject.setBytes(1, data); - appendToLargeObject.setLong(2, file.getId()); - appendToLargeObject.executeUpdate(); + connection.setAutoCommit(false); + appendToLargeObject.setLong(1, file.getId()); + + int bytesWritten = 0; + try (ResultSet rs = appendToLargeObject.executeQuery()) { + if (rs.next()) { + Blob blob = rs.getBlob(1); + bytesWritten = blob.setBytes(blob.length() + 1, data); + rs.updateBlob(1, blob); + rs.updateRow(); + } connection.commit(); - return data.length; + return bytesWritten; } catch (SQLException e) { connection.rollback(); throw e; diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/GenericSQLProvider.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/GenericSQLProvider.java index 0527c5453a..1d9b829352 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/GenericSQLProvider.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/GenericSQLProvider.java @@ -69,7 +69,7 @@ public class GenericSQLProvider implements SQLProvider { selectIdByFileNameSQL = "SELECT ID, FILENAME, EXTENSION, DATA FROM " + tableName + " WHERE fileName=?"; - appendToFileSQL = "UPDATE " + tableName + " SET DATA = CONCAT(DATA, ?) WHERE ID=?"; + appendToFileSQL = "SELECT DATA FROM " + tableName + " WHERE ID=? FOR UPDATE"; readLargeObjectSQL = "SELECT DATA FROM " + tableName + " WHERE ID=?";