Search in sources :

Example 56 with StoreException

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);
    }
}
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 57 with StoreException

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

Example 58 with StoreException

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());
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) StoreException(org.apache.qpid.server.store.StoreException)

Example 59 with StoreException

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

Example 60 with StoreException

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

Aggregations

StoreException (org.apache.qpid.server.store.StoreException)70 SQLException (java.sql.SQLException)28 Connection (java.sql.Connection)23 PreparedStatement (java.sql.PreparedStatement)21 DatabaseEntry (com.sleepycat.je.DatabaseEntry)20 IOException (java.io.IOException)18 OperationStatus (com.sleepycat.je.OperationStatus)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 ResultSet (java.sql.ResultSet)10 UUID (java.util.UUID)8 Database (com.sleepycat.je.Database)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ModelVersion (org.apache.qpid.server.model.ModelVersion)7 LinkKey (org.apache.qpid.server.protocol.v1_0.LinkKey)7 HashSet (java.util.HashSet)5 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)5 Cursor (com.sleepycat.je.Cursor)4 DatabaseConfig (com.sleepycat.je.DatabaseConfig)4