Search in sources :

Example 1 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractDerbyMessageStore method storedSizeChange.

@Override
protected void storedSizeChange(final int delta) {
    if (getPersistentSizeHighThreshold() > 0) {
        synchronized (this) {
            // the delta supplied is an approximation of a store size change. we don;t want to check the statistic every
            // time, so we do so only when there's been enough change that it is worth looking again. We do this by
            // assuming the total size will change by less than twice the amount of the message data change.
            long newSize = _totalStoreSize += 3 * delta;
            Connection conn = null;
            try {
                if (!_limitBusted && newSize > getPersistentSizeHighThreshold()) {
                    conn = newAutoCommitConnection();
                    _totalStoreSize = getSizeOnDisk(conn);
                    if (_totalStoreSize > getPersistentSizeHighThreshold()) {
                        _limitBusted = true;
                        _eventManager.notifyEvent(Event.PERSISTENT_MESSAGE_SIZE_OVERFULL);
                    }
                } else if (_limitBusted && newSize < getPersistentSizeLowThreshold()) {
                    long oldSize = _totalStoreSize;
                    conn = newAutoCommitConnection();
                    _totalStoreSize = getSizeOnDisk(conn);
                    if (oldSize <= _totalStoreSize) {
                        reduceSizeOnDisk(conn);
                        _totalStoreSize = getSizeOnDisk(conn);
                    }
                    if (_totalStoreSize < getPersistentSizeLowThreshold()) {
                        _limitBusted = false;
                        _eventManager.notifyEvent(Event.PERSISTENT_MESSAGE_SIZE_UNDERFULL);
                    }
                }
            } catch (SQLException e) {
                JdbcUtils.closeConnection(conn, getLogger());
                throw new StoreException("Exception while processing store size change", e);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) StoreException(org.apache.qpid.server.store.StoreException)

Example 2 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractJDBCConfigurationStore method upgradeIfNecessary.

protected void upgradeIfNecessary(ConfiguredObject<?> parent) throws StoreException {
    Connection connection = null;
    try {
        connection = newAutoCommitConnection();
        boolean tableExists = tableExists(getConfigurationVersionTableName(), connection);
        if (tableExists) {
            int configVersion = getConfigVersion(connection);
            getLogger().debug("Upgrader read existing config version {}", configVersion);
            switch(configVersion) {
                case 7:
                    upgradeFromV7(parent);
                    break;
                default:
                    throw new UnsupportedOperationException("Cannot upgrade from configuration version : " + configVersion);
            }
        }
    } catch (SQLException se) {
        throw new StoreException("Failed to upgrade database", se);
    } finally {
        JdbcUtils.closeConnection(connection, getLogger());
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) StoreException(org.apache.qpid.server.store.StoreException)

Example 3 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractJDBCConfigurationStore method insertConfiguredObject.

private void insertConfiguredObject(ConfiguredObjectRecord configuredObject, final Connection conn) throws StoreException {
    try {
        try (PreparedStatement stmt = conn.prepareStatement("SELECT object_type, attributes FROM " + getConfiguredObjectsTableName() + " where id = ?")) {
            stmt.setString(1, configuredObject.getId().toString());
            boolean exists;
            try (ResultSet rs = stmt.executeQuery()) {
                exists = rs.next();
            }
            // If we don't have any data in the result set then we can add this configured object
            if (!exists) {
                try (PreparedStatement insertStmt = conn.prepareStatement("INSERT INTO " + getConfiguredObjectsTableName() + " ( id, object_type, attributes) VALUES (?,?,?)")) {
                    insertStmt.setString(1, configuredObject.getId().toString());
                    insertStmt.setString(2, configuredObject.getType());
                    if (configuredObject.getAttributes() == null) {
                        insertStmt.setNull(3, Types.BLOB);
                    } else {
                        final Map<String, Object> attributes = configuredObject.getAttributes();
                        final ObjectMapper objectMapper = ConfiguredObjectJacksonModule.newObjectMapper(true);
                        byte[] attributesAsBytes = objectMapper.writeValueAsBytes(attributes);
                        ByteArrayInputStream bis = new ByteArrayInputStream(attributesAsBytes);
                        insertStmt.setBinaryStream(3, bis, attributesAsBytes.length);
                    }
                    insertStmt.execute();
                }
                writeHierarchy(configuredObject, conn);
            }
        }
    } catch (IOException | SQLException e) {
        throw new StoreException("Error inserting of configured object " + configuredObject + " into database: " + e.getMessage(), e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) StoreException(org.apache.qpid.server.store.StoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractJDBCConfigurationStore method create.

@Override
public void create(ConfiguredObjectRecord object) throws StoreException {
    assertState(OPEN);
    try {
        Connection conn = newConnection();
        try {
            insertConfiguredObject(object, conn);
            conn.commit();
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        throw new StoreException("Error creating ConfiguredObject " + object, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) StoreException(org.apache.qpid.server.store.StoreException)

Example 5 with StoreException

use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.

the class AbstractJDBCConfigurationStore method remove.

@Override
public UUID[] remove(ConfiguredObjectRecord... objects) throws StoreException {
    assertState(OPEN);
    Collection<UUID> removed = new ArrayList<UUID>(objects.length);
    try {
        try (Connection conn = newAutoCommitConnection()) {
            for (ConfiguredObjectRecord record : objects) {
                if (removeConfiguredObject(record.getId(), conn) != 0) {
                    removed.add(record.getId());
                }
            }
        }
    } catch (SQLException e) {
        throw new StoreException("Error deleting of configured objects " + Arrays.asList(objects) + " from database: " + e.getMessage(), e);
    }
    return removed.toArray(new UUID[removed.size()]);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) UUID(java.util.UUID) 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