ARTEMIS-957 Use setBytes JDBC API vs Concat BLOB

(cherry picked from commit b8595d610d)
This commit is contained in:
Martyn Taylor 2017-02-10 14:27:26 +00:00
parent f231fe4e9b
commit 3d765ae4cf
3 changed files with 16 additions and 18 deletions

View File

@ -26,16 +26,12 @@ public class DerbySQLProvider extends GenericSQLProvider {
private final String createFileTableSQL; private final String createFileTableSQL;
private final String appendToFileSQL;
private DerbySQLProvider(String tableName) { private DerbySQLProvider(String tableName) {
super(tableName.toUpperCase()); super(tableName.toUpperCase());
createFileTableSQL = "CREATE TABLE " + tableName + createFileTableSQL = "CREATE TABLE " + tableName +
"(ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)," + "(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))"; "FILENAME VARCHAR(255), EXTENSION VARCHAR(10), DATA BLOB, PRIMARY KEY(ID))";
appendToFileSQL = "UPDATE " + tableName + " SET DATA = DATA || ? WHERE ID=?";
} }
@Override @Override
@ -48,11 +44,6 @@ public class DerbySQLProvider extends GenericSQLProvider {
return createFileTableSQL; return createFileTableSQL;
} }
@Override
public String getAppendToLargeObjectSQL() {
return appendToFileSQL;
}
@Override @Override
public boolean closeConnectionOnShutdown() { public boolean closeConnectionOnShutdown() {
return false; return false;

View File

@ -74,7 +74,7 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver {
this.copyFileRecord = connection.prepareStatement(sqlProvider.getCopyFileRecordByIdSQL()); this.copyFileRecord = connection.prepareStatement(sqlProvider.getCopyFileRecordByIdSQL());
this.renameFile = connection.prepareStatement(sqlProvider.getUpdateFileNameByIdSQL()); this.renameFile = connection.prepareStatement(sqlProvider.getUpdateFileNameByIdSQL());
this.readLargeObject = connection.prepareStatement(sqlProvider.getReadLargeObjectSQL()); 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()); this.selectFileNamesByExtension = connection.prepareStatement(sqlProvider.getSelectFileNamesByExtensionSQL());
} }
@ -154,7 +154,8 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver {
try (ResultSet rs = readLargeObject.executeQuery()) { try (ResultSet rs = readLargeObject.executeQuery()) {
if (rs.next()) { if (rs.next()) {
file.setWritePosition((int) rs.getBlob(1).length()); Blob blob = rs.getBlob(1);
file.setWritePosition((int) blob.length());
} }
connection.commit(); connection.commit();
} catch (SQLException e) { } catch (SQLException e) {
@ -242,13 +243,19 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver {
*/ */
public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException { public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException {
synchronized (connection) { synchronized (connection) {
try { connection.setAutoCommit(false);
connection.setAutoCommit(false); appendToLargeObject.setLong(1, file.getId());
appendToLargeObject.setBytes(1, data);
appendToLargeObject.setLong(2, file.getId()); int bytesWritten = 0;
appendToLargeObject.executeUpdate(); 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(); connection.commit();
return data.length; return bytesWritten;
} catch (SQLException e) { } catch (SQLException e) {
connection.rollback(); connection.rollback();
throw e; throw e;

View File

@ -69,7 +69,7 @@ public class GenericSQLProvider implements SQLProvider {
selectIdByFileNameSQL = "SELECT ID, FILENAME, EXTENSION, DATA FROM " + tableName + " WHERE fileName=?"; 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=?"; readLargeObjectSQL = "SELECT DATA FROM " + tableName + " WHERE ID=?";