use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class JDBCLinkStore method doSaveLink.
@Override
protected void doSaveLink(final LinkDefinition<Source, Target> link) throws StoreException {
String linkKey = generateLinkKey(link);
Connection connection = getConnection();
try {
connection.setAutoCommit(false);
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
try (PreparedStatement preparedStatement = connection.prepareStatement(String.format("SELECT remote_container_id, link_name, link_role, source, target FROM %s WHERE link_key = ?", getLinksTableName()))) {
preparedStatement.setString(1, linkKey);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
update(connection, linkKey, link);
} else {
insert(connection, linkKey, link);
}
}
}
connection.commit();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException re) {
LOGGER.debug("Rollback failed on rolling back saving link transaction", re);
}
throw new StoreException(String.format("Cannot save link %s", new LinkKey(link)), e);
} finally {
JdbcUtils.closeConnection(connection, LOGGER);
}
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class JDBCLinkStore method update.
private void update(final Connection connection, final String linkKey, final LinkDefinition<? extends BaseSource, ? extends BaseTarget> linkDefinition) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(String.format("UPDATE %s SET source = ?, target = ? WHERE link_key = ?", getLinksTableName()))) {
saveObjectAsBlob(statement, 1, linkDefinition.getSource());
saveObjectAsBlob(statement, 2, linkDefinition.getTarget());
statement.setString(3, linkKey);
if (statement.executeUpdate() != 1) {
throw new StoreException(String.format("Cannot save link %s", new LinkKey(linkDefinition)));
}
}
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class JDBCLinkStore method generateLinkKey.
private String generateLinkKey(final LinkDefinition<?, ?> linkDefinition) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new StoreException("Cannot generate SHA-256 checksum", e);
}
md.update(linkDefinition.getRemoteContainerId().getBytes(UTF_8));
md.update(linkDefinition.getName().getBytes(UTF_8));
md.update(linkDefinition.getRole().getValue() ? (byte) 1 : (byte) 0);
return Base64.getEncoder().encodeToString(md.digest());
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class JDBCLinkStore method doOpenAndLoad.
@Override
protected Collection<LinkDefinition<Source, Target>> doOpenAndLoad(final LinkStoreUpdater updater) throws StoreException {
Collection<LinkDefinition<Source, Target>> linkDefinitions;
try {
checkTransactionIsolationLevel();
createOrOpenStoreDatabase();
linkDefinitions = getLinks();
ModelVersion storedVersion = getStoredVersion();
ModelVersion currentVersion = new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION, BrokerModel.MODEL_MINOR_VERSION);
if (storedVersion.lessThan(currentVersion)) {
linkDefinitions = performUpdate(updater, linkDefinitions, storedVersion, currentVersion);
} else if (currentVersion.lessThan(storedVersion)) {
throw new StoreException(String.format("Cannot downgrade the store from %s to %s", storedVersion, currentVersion));
}
} catch (SQLException e) {
throw new StoreException("Cannot open link store", e);
}
return linkDefinitions;
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class JDBCLinkStore method updateVersion.
private void updateVersion(final Connection connection, final ModelVersion currentVersion) throws SQLException {
String version = currentVersion.toString();
try (PreparedStatement statement = connection.prepareStatement(String.format("INSERT INTO %s (version, version_time) VALUES (?,?)", getVersionTableName()))) {
statement.setString(1, version);
statement.setDate(2, new java.sql.Date(System.currentTimeMillis()));
if (statement.executeUpdate() != 1) {
throw new StoreException(String.format("Cannot insert version '%s' into version table", version));
}
}
}
Aggregations