ARTEMIS-938 JDBC persistence-store should use BIGINT type for IDs in database tables

This commit is contained in:
Francesco Nigro 2017-01-18 17:08:11 +01:00 committed by Clebert Suconic
parent ee9e717b51
commit 807dbf9051
8 changed files with 49 additions and 31 deletions

View File

@ -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=?";

View File

@ -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[] {

View File

@ -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)",

View File

@ -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);

View File

@ -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;
}

View File

@ -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) {

View File

@ -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";
@ -33,6 +34,18 @@ public class PostgresSequentialSequentialFileDriver extends JDBCSequentialFileFa
super();
}
@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) {
@ -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));

View File

@ -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 (?,?,?)";