Repository: activemq-artemis Updated Branches: refs/heads/1.x 7de8dd77e -> 3d765ae4c
ARTEMIS-938 JDBC persistence-store should use BIGINT type for IDs in database tables (cherry picked from commit 807dbf90510c7dce35fd141f0cd49932d772e115) Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/d0b568c8 Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/d0b568c8 Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/d0b568c8 Branch: refs/heads/1.x Commit: d0b568c895532099f19724f104dc3c543f2c3404 Parents: 7de8dd7 Author: Francesco Nigro <[email protected]> Authored: Wed Jan 18 17:08:11 2017 +0100 Committer: Martyn Taylor <[email protected]> Committed: Fri Feb 10 14:37:16 2017 +0000 ---------------------------------------------------------------------- .../store/drivers/derby/DerbySQLProvider.java | 2 +- .../store/drivers/mysql/MySQLSQLProvider.java | 2 +- .../drivers/postgres/PostgresSQLProvider.java | 2 +- .../artemis/jdbc/store/file/JDBCFileUtils.java | 7 +++- .../jdbc/store/file/JDBCSequentialFile.java | 6 ++-- .../file/JDBCSequentialFileFactoryDriver.java | 38 ++++++++++---------- .../PostgresSequentialSequentialFileDriver.java | 21 ++++++++--- .../jdbc/store/sql/GenericSQLProvider.java | 2 +- 8 files changed, 49 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d0b568c8/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/derby/DerbySQLProvider.java ---------------------------------------------------------------------- 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 281ea88..ab61cc2 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 @@ -32,7 +32,7 @@ public class DerbySQLProvider extends GenericSQLProvider { super(tableName.toUpperCase()); createFileTableSQL = "CREATE TABLE " + tableName + - "(ID INTEGER 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))"; appendToFileSQL = "UPDATE " + tableName + " SET DATA = DATA || ? WHERE ID=?"; http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d0b568c8/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/mysql/MySQLSQLProvider.java ---------------------------------------------------------------------- diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/mysql/MySQLSQLProvider.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/mysql/MySQLSQLProvider.java index 9b0c4e6..a538ebd 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/mysql/MySQLSQLProvider.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/mysql/MySQLSQLProvider.java @@ -33,7 +33,7 @@ public class MySQLSQLProvider extends GenericSQLProvider { super(tName.toLowerCase()); createFileTableSQL = "CREATE TABLE " + tableName + - "(ID INTEGER NOT NULL AUTO_INCREMENT," + + "(ID BIGINT NOT NULL AUTO_INCREMENT," + "FILENAME VARCHAR(255), EXTENSION VARCHAR(10), DATA LONGBLOB, PRIMARY KEY(ID)) ENGINE=InnoDB;"; createJournalTableSQL = new String[] { http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d0b568c8/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/postgres/PostgresSQLProvider.java ---------------------------------------------------------------------- diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/postgres/PostgresSQLProvider.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/postgres/PostgresSQLProvider.java index fb0d34a..1baccae 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/postgres/PostgresSQLProvider.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/postgres/PostgresSQLProvider.java @@ -31,7 +31,7 @@ public class PostgresSQLProvider extends GenericSQLProvider { private PostgresSQLProvider(String tName) { super(tName.toLowerCase()); createFileTableSQL = "CREATE TABLE " + tableName + - "(ID SERIAL, FILENAME VARCHAR(255), EXTENSION VARCHAR(10), DATA OID, PRIMARY KEY(ID))"; + "(ID BIGSERIAL, FILENAME VARCHAR(255), EXTENSION VARCHAR(10), DATA OID, PRIMARY KEY(ID))"; createJournalTableSQL = new String[] { "CREATE TABLE " + tableName + "(id BIGINT,recordType SMALLINT,compactCount SMALLINT,txId BIGINT,userRecordType SMALLINT,variableSize INTEGER,record BYTEA,txDataSize INTEGER,txData BYTEA,txCheckNoRecords INTEGER,seq BIGINT)", http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d0b568c8/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCFileUtils.java ---------------------------------------------------------------------- diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCFileUtils.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCFileUtils.java index 58494b0..b5f1aa6 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCFileUtils.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCFileUtils.java @@ -29,7 +29,12 @@ class JDBCFileUtils { static JDBCSequentialFileFactoryDriver getDBFileDriver(String driverClass, String jdbcConnectionUrl, SQLProvider provider) throws SQLException { - JDBCSequentialFileFactoryDriver dbDriver = new JDBCSequentialFileFactoryDriver(); + final JDBCSequentialFileFactoryDriver dbDriver; + if (provider instanceof PostgresSQLProvider) { + dbDriver = new PostgresSequentialSequentialFileDriver(); + } else { + dbDriver = new JDBCSequentialFileFactoryDriver(); + } dbDriver.setSqlProvider(provider); dbDriver.setJdbcConnectionUrl(jdbcConnectionUrl); dbDriver.setJdbcDriverClass(driverClass); http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d0b568c8/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java ---------------------------------------------------------------------- diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java index 85c4877..7e72785 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java @@ -47,7 +47,7 @@ public class JDBCSequentialFile implements SequentialFile { private boolean isCreated = false; - private int id = -1; + private long id = -1; private long readPosition = 0; @@ -328,11 +328,11 @@ public class JDBCSequentialFile implements SequentialFile { } } - public int getId() { + public long getId() { return id; } - public void setId(int id) { + public void setId(long id) { this.id = id; } http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d0b568c8/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFileFactoryDriver.java ---------------------------------------------------------------------- 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 f9f206a..41ad105 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 @@ -35,19 +35,19 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { protected PreparedStatement deleteFile; - PreparedStatement createFile; + protected PreparedStatement createFile; - private PreparedStatement selectFileByFileName; + protected PreparedStatement selectFileByFileName; - private PreparedStatement copyFileRecord; + protected PreparedStatement copyFileRecord; - private PreparedStatement renameFile; + protected PreparedStatement renameFile; - PreparedStatement readLargeObject; + protected PreparedStatement readLargeObject; - private PreparedStatement appendToLargeObject; + protected PreparedStatement appendToLargeObject; - private PreparedStatement selectFileNamesByExtension; + protected PreparedStatement selectFileNamesByExtension; JDBCSequentialFileFactoryDriver() { super(); @@ -105,7 +105,7 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { * @throws SQLException */ public void openFile(JDBCSequentialFile file) throws SQLException { - int fileId = fileExists(file); + final long fileId = fileExists(file); if (fileId < 0) { createFile(file); } else { @@ -121,13 +121,13 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { * @return * @throws SQLException */ - public int fileExists(JDBCSequentialFile file) throws SQLException { + public long fileExists(JDBCSequentialFile file) throws SQLException { try { synchronized (connection) { connection.setAutoCommit(false); selectFileByFileName.setString(1, file.getFileName()); try (ResultSet rs = selectFileByFileName.executeQuery()) { - int id = rs.next() ? rs.getInt(1) : -1; + final long id = rs.next() ? rs.getLong(1) : -1; connection.commit(); return id; } catch (Exception e) { @@ -150,7 +150,7 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { public void loadFile(JDBCSequentialFile file) throws SQLException { synchronized (connection) { connection.setAutoCommit(false); - readLargeObject.setInt(1, file.getId()); + readLargeObject.setLong(1, file.getId()); try (ResultSet rs = readLargeObject.executeQuery()) { if (rs.next()) { @@ -180,7 +180,7 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { createFile.executeUpdate(); try (ResultSet keys = createFile.getGeneratedKeys()) { keys.next(); - file.setId(keys.getInt(1)); + file.setId(keys.getLong(1)); } connection.commit(); } catch (SQLException e) { @@ -202,7 +202,7 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { try { connection.setAutoCommit(false); renameFile.setString(1, newFileName); - renameFile.setInt(2, file.getId()); + renameFile.setLong(2, file.getId()); renameFile.executeUpdate(); connection.commit(); } catch (SQLException e) { @@ -222,7 +222,7 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { synchronized (connection) { try { connection.setAutoCommit(false); - deleteFile.setInt(1, file.getId()); + deleteFile.setLong(1, file.getId()); deleteFile.executeUpdate(); connection.commit(); } catch (SQLException e) { @@ -245,7 +245,7 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { try { connection.setAutoCommit(false); appendToLargeObject.setBytes(1, data); - appendToLargeObject.setInt(2, file.getId()); + appendToLargeObject.setLong(2, file.getId()); appendToLargeObject.executeUpdate(); connection.commit(); return data.length; @@ -267,11 +267,11 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { public int readFromFile(JDBCSequentialFile file, ByteBuffer bytes) throws SQLException { synchronized (connection) { connection.setAutoCommit(false); - readLargeObject.setInt(1, file.getId()); + readLargeObject.setLong(1, file.getId()); int readLength = 0; try (ResultSet rs = readLargeObject.executeQuery()) { if (rs.next()) { - Blob blob = rs.getBlob(1); + final Blob blob = rs.getBlob(1); readLength = (int) calculateReadLength(blob.length(), bytes.remaining(), file.position()); byte[] data = blob.getBytes(file.position() + 1, readLength); bytes.put(data); @@ -296,8 +296,8 @@ public class JDBCSequentialFileFactoryDriver extends AbstractJDBCDriver { synchronized (connection) { try { connection.setAutoCommit(false); - copyFileRecord.setInt(1, fileFrom.getId()); - copyFileRecord.setInt(2, fileTo.getId()); + copyFileRecord.setLong(1, fileFrom.getId()); + copyFileRecord.setLong(2, fileTo.getId()); copyFileRecord.executeUpdate(); connection.commit(); } catch (SQLException e) { http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d0b568c8/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/PostgresSequentialSequentialFileDriver.java ---------------------------------------------------------------------- diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/PostgresSequentialSequentialFileDriver.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/PostgresSequentialSequentialFileDriver.java index 8c0f975..cdc167f 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/PostgresSequentialSequentialFileDriver.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/PostgresSequentialSequentialFileDriver.java @@ -19,13 +19,14 @@ package org.apache.activemq.artemis.jdbc.store.file; import java.nio.ByteBuffer; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import org.postgresql.PGConnection; import org.postgresql.largeobject.LargeObject; import org.postgresql.largeobject.LargeObjectManager; @SuppressWarnings("SynchronizeOnNonFinalField") -public class PostgresSequentialSequentialFileDriver extends JDBCSequentialFileFactoryDriver { +public final class PostgresSequentialSequentialFileDriver extends JDBCSequentialFileFactoryDriver { private static final String POSTGRES_OID_KEY = "POSTGRES_OID_KEY"; @@ -34,6 +35,18 @@ public class PostgresSequentialSequentialFileDriver extends JDBCSequentialFileFa } @Override + protected void prepareStatements() throws SQLException { + this.deleteFile = connection.prepareStatement(sqlProvider.getDeleteFileSQL()); + this.createFile = connection.prepareStatement(sqlProvider.getInsertFileSQL(), Statement.RETURN_GENERATED_KEYS); + this.selectFileByFileName = connection.prepareStatement(sqlProvider.getSelectFileByFileName()); + 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.selectFileNamesByExtension = connection.prepareStatement(sqlProvider.getSelectFileNamesByExtensionSQL()); + } + + @Override public void createFile(JDBCSequentialFile file) throws SQLException { synchronized (connection) { try { @@ -49,7 +62,7 @@ public class PostgresSequentialSequentialFileDriver extends JDBCSequentialFileFa try (ResultSet keys = createFile.getGeneratedKeys()) { keys.next(); - file.setId(keys.getInt(1)); + file.setId(keys.getLong(1)); } connection.commit(); } catch (SQLException e) { @@ -63,7 +76,7 @@ public class PostgresSequentialSequentialFileDriver extends JDBCSequentialFileFa public void loadFile(JDBCSequentialFile file) throws SQLException { synchronized (connection) { connection.setAutoCommit(false); - readLargeObject.setInt(1, file.getId()); + readLargeObject.setLong(1, file.getId()); try (ResultSet rs = readLargeObject.executeQuery()) { if (rs.next()) { @@ -133,7 +146,7 @@ public class PostgresSequentialSequentialFileDriver extends JDBCSequentialFileFa if (oid == null) { synchronized (connection) { connection.setAutoCommit(false); - readLargeObject.setInt(1, file.getId()); + readLargeObject.setLong(1, file.getId()); try (ResultSet rs = readLargeObject.executeQuery()) { if (rs.next()) { file.addMetaData(POSTGRES_OID_KEY, rs.getLong(1)); http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d0b568c8/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/sql/GenericSQLProvider.java ---------------------------------------------------------------------- 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 a7a4b47..0527c54 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 @@ -61,7 +61,7 @@ public class GenericSQLProvider implements SQLProvider { this.tableName = tableName; createFileTableSQL = "CREATE TABLE " + tableName + - "(ID INT AUTO_INCREMENT, FILENAME VARCHAR(255), EXTENSION VARCHAR(10), DATA BLOB, PRIMARY KEY(ID))"; + "(ID BIGINT AUTO_INCREMENT, FILENAME VARCHAR(255), EXTENSION VARCHAR(10), DATA BLOB, PRIMARY KEY(ID))"; insertFileSQL = "INSERT INTO " + tableName + " (FILENAME, EXTENSION, DATA) VALUES (?,?,?)";
