Search in sources :

Example 6 with LinkDefinition

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));
    }
}
Also used : LinkKey(org.apache.qpid.server.protocol.v1_0.LinkKey) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) StoreException(org.apache.qpid.server.store.StoreException)

Example 7 with LinkDefinition

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;
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) BaseSource(org.apache.qpid.server.protocol.v1_0.type.BaseSource) Source(org.apache.qpid.server.protocol.v1_0.type.messaging.Source) StoreException(org.apache.qpid.server.store.StoreException) Role(org.apache.qpid.server.protocol.v1_0.type.transport.Role) LinkDefinition(org.apache.qpid.server.protocol.v1_0.LinkDefinition) Target(org.apache.qpid.server.protocol.v1_0.type.messaging.Target) BaseTarget(org.apache.qpid.server.protocol.v1_0.type.BaseTarget) ResultSet(java.sql.ResultSet)

Example 8 with LinkDefinition

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);
    }
}
Also used : LinkKey(org.apache.qpid.server.protocol.v1_0.LinkKey) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StoreException(org.apache.qpid.server.store.StoreException)

Example 9 with LinkDefinition

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)));
        }
    }
}
Also used : LinkKey(org.apache.qpid.server.protocol.v1_0.LinkKey) PreparedStatement(java.sql.PreparedStatement) StoreException(org.apache.qpid.server.store.StoreException)

Example 10 with 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;
}
Also used : LinkDefinition(org.apache.qpid.server.protocol.v1_0.LinkDefinition) SQLException(java.sql.SQLException) ModelVersion(org.apache.qpid.server.model.ModelVersion) StoreException(org.apache.qpid.server.store.StoreException)

Aggregations

LinkKey (org.apache.qpid.server.protocol.v1_0.LinkKey)8 StoreException (org.apache.qpid.server.store.StoreException)8 PreparedStatement (java.sql.PreparedStatement)5 Database (com.sleepycat.je.Database)3 DatabaseEntry (com.sleepycat.je.DatabaseEntry)3 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 LinkDefinition (org.apache.qpid.server.protocol.v1_0.LinkDefinition)3 OperationStatus (com.sleepycat.je.OperationStatus)2 ResultSet (java.sql.ResultSet)2 ModelVersion (org.apache.qpid.server.model.ModelVersion)2 BaseSource (org.apache.qpid.server.protocol.v1_0.type.BaseSource)2 BaseTarget (org.apache.qpid.server.protocol.v1_0.type.BaseTarget)2 Source (org.apache.qpid.server.protocol.v1_0.type.messaging.Source)2 Target (org.apache.qpid.server.protocol.v1_0.type.messaging.Target)2 Cursor (com.sleepycat.je.Cursor)1 DatabaseNotFoundException (com.sleepycat.je.DatabaseNotFoundException)1 Transaction (com.sleepycat.je.Transaction)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1