Skip to content

Commit

Permalink
ISPN-6950: Errors thrown with timestamp index in H2.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanemerson authored and anistor committed Aug 17, 2016
1 parent 2743cfd commit 355a1d7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ public abstract class AbstractTableManager implements TableManager {
protected final TableManipulationConfiguration config;
protected final String timestampIndexExt = "timestamp_index";

protected boolean timestampIndexExists = false;
protected String identifierQuoteString = "\"";
protected String cacheName;
protected DbMetaData metaData;
protected TableName tableName;
protected String timestampIndexName;

protected String insertRowSql;
protected String updateRowSql;
Expand Down Expand Up @@ -126,34 +124,35 @@ public void createTable(Connection conn) throws PersistenceException {
protected void createTimestampIndex(Connection conn) throws PersistenceException {
if (metaData.isIndexingDisabled()) return;

boolean indexExists = timestampIndexExists(conn);
if (!indexExists) {
String ddl = String.format("CREATE INDEX %s ON %s (%s)", getIndexName(true), getTableName(), config.timestampColumnName());
if (log.isTraceEnabled()) {
log.tracef("Adding timestamp index with following DDL: '%s'.", ddl);
}
executeUpdateSql(conn, ddl);
}
}

protected boolean timestampIndexExists(Connection conn) throws PersistenceException {
ResultSet rs = null;
try {
TableName table = getTableName();
DatabaseMetaData meta = conn.getMetaData();
rs = meta.getIndexInfo(null, table.getSchema(), table.getName(), false, false);

boolean indexExists = false;
while (rs.next()) {
String indexName = rs.getString("INDEX_NAME");
if (indexName.equalsIgnoreCase(getIndexName())) {
indexExists = true;
break;
if (indexName.equalsIgnoreCase(getIndexName(false))) {
return true;
}
}

if (!indexExists) {
String ddl = String.format("CREATE INDEX %s ON %s (%s)", getIndexName(), getTableName(), config.timestampColumnName());
if (log.isTraceEnabled()) {
log.tracef("Adding timestamp index with following DDL: '%s'.", ddl);
}
executeUpdateSql(conn, ddl);
}
timestampIndexExists = true;
} catch (SQLException e) {
throw new PersistenceException(e);
throw new PersistenceException(e);
} finally {
JdbcUtil.safeClose(rs);
}
return false;
}

public void executeUpdateSql(Connection conn, String sql) throws PersistenceException {
Expand Down Expand Up @@ -181,9 +180,9 @@ public void dropTable(Connection conn) throws PersistenceException {
}

protected void dropTimestampIndex(Connection conn) throws PersistenceException {
if (!timestampIndexExists) return;
if (!timestampIndexExists(conn)) return;

String dropIndexDdl = String.format("DROP INDEX %s ON %s", getIndexName(), getTableName());
String dropIndexDdl = String.format("DROP INDEX %s ON %s", getIndexName(true), getTableName());
executeUpdateSql(conn, dropIndexDdl);
}

Expand Down Expand Up @@ -211,13 +210,14 @@ public TableName getTableName() {
return tableName;
}

public String getIndexName() {
if (timestampIndexName == null) {
TableName table = getTableName();
String tableName = table.toString().replace(table.getIdentifierQuote(), "");
timestampIndexName = tableName + "_" + timestampIndexExt;
public String getIndexName(boolean withIdentifier) {
TableName table = getTableName();
String tableName = table.toString().replace(identifierQuoteString, "");
String indexName = tableName + "_" + timestampIndexExt;
if (withIdentifier) {
return identifierQuoteString + indexName + identifierQuoteString;
}
return timestampIndexName;
return indexName;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.infinispan.persistence.jdbc.table.management;

import java.sql.Connection;

import org.infinispan.persistence.jdbc.configuration.TableManipulationConfiguration;
import org.infinispan.persistence.jdbc.connectionfactory.ConnectionFactory;
import org.infinispan.persistence.jdbc.logging.Log;
import org.infinispan.persistence.spi.PersistenceException;
import org.infinispan.util.logging.LogFactory;

/**
Expand All @@ -24,4 +27,10 @@ public String getUpsertRowSql() {
}
return upsertRowSql;
}

@Override
protected void dropTimestampIndex(Connection conn) throws PersistenceException {
String dropIndexDdl = String.format("DROP INDEX IF EXISTS %s", getIndexName(true));
executeUpdateSql(conn, dropIndexDdl);
}
}

0 comments on commit 355a1d7

Please sign in to comment.