use of org.apache.qpid.server.protocol.v1_0.LinkDefinition in project qpid-broker-j by apache.
the class BDBLinkStore method save.
private void save(Database database, Transaction txn, final LinkDefinition<Source, Target> link) {
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
LinkKey linkKey = new LinkKey(link);
LinkKeyEntryBinding.getInstance().objectToEntry(linkKey, key);
LinkValueEntryBinding.getInstance().objectToEntry(new LinkValue(link), value);
// TODO: create transaction
OperationStatus status = database.put(txn, key, value);
if (status != OperationStatus.SUCCESS) {
throw new StoreException(String.format("Cannot save link %s", linkKey));
}
}
use of org.apache.qpid.server.protocol.v1_0.LinkDefinition in project qpid-broker-j by apache.
the class JDBCLinkStore method getLinks.
private Collection<LinkDefinition<Source, Target>> getLinks() throws SQLException {
Collection<LinkDefinition<Source, Target>> links = new ArrayList<>();
try (Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(String.format("SELECT remote_container_id, link_name, link_role, source, target FROM %s", getLinksTableName()))) {
while (resultSet.next()) {
String remoteContainerId = getBlobValueAsString(resultSet, 1);
String linkName = getBlobValueAsString(resultSet, 2);
Role role = Role.valueOf(resultSet.getBoolean(3));
Source source = (Source) getBlobAsAmqpObject(resultSet, 4);
Target target = (Target) getBlobAsAmqpObject(resultSet, 5);
links.add(new LinkDefinitionImpl<>(remoteContainerId, linkName, role, source, target));
}
} catch (IllegalArgumentException e) {
throw new StoreException("Cannot load links from store", e);
}
return links;
}
use of org.apache.qpid.server.protocol.v1_0.LinkDefinition 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.protocol.v1_0.LinkDefinition 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.protocol.v1_0.LinkDefinition 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;
}
Aggregations