Search in sources :

Example 41 with StoreException

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

the class AbstractJDBCConfigurationStore method doVisitAllConfiguredObjectRecords.

private Collection<ConfiguredObjectRecordImpl> doVisitAllConfiguredObjectRecords(ConfiguredObjectRecordHandler handler) throws SQLException {
    Map<UUID, ConfiguredObjectRecordImpl> configuredObjects = new HashMap<UUID, ConfiguredObjectRecordImpl>();
    final ObjectMapper objectMapper = new ObjectMapper();
    try (Connection conn = newAutoCommitConnection()) {
        PreparedStatement stmt = conn.prepareStatement("SELECT id, object_type, attributes FROM " + getConfiguredObjectsTableName());
        try {
            ResultSet rs = stmt.executeQuery();
            try {
                while (rs.next()) {
                    String id = rs.getString(1);
                    String objectType = rs.getString(2);
                    String attributes = getBlobAsString(rs, 3);
                    final ConfiguredObjectRecordImpl configuredObjectRecord = new ConfiguredObjectRecordImpl(UUID.fromString(id), objectType, objectMapper.readValue(attributes, Map.class));
                    configuredObjects.put(configuredObjectRecord.getId(), configuredObjectRecord);
                }
            } catch (IOException e) {
                throw new StoreException("Error recovering persistent state: " + e.getMessage(), e);
            } finally {
                rs.close();
            }
        } finally {
            stmt.close();
        }
        stmt = conn.prepareStatement("SELECT child_id, parent_type, parent_id FROM " + getConfiguredObjectHierarchyTableName());
        try {
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    UUID childId = UUID.fromString(rs.getString(1));
                    String parentType = rs.getString(2);
                    UUID parentId = UUID.fromString(rs.getString(3));
                    ConfiguredObjectRecordImpl child = configuredObjects.get(childId);
                    ConfiguredObjectRecordImpl parent = configuredObjects.get(parentId);
                    if (child != null && parent != null) {
                        child.addParent(parentType, parent);
                    }
                }
            }
        } finally {
            stmt.close();
        }
    }
    return configuredObjects.values();
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StoreException(org.apache.qpid.server.store.StoreException)

Example 42 with StoreException

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

the class AbstractJDBCConfigurationStore method updateConfiguredObject.

private void updateConfiguredObject(ConfiguredObjectRecord configuredObject, boolean createIfNecessary, Connection conn) throws SQLException, StoreException {
    try (PreparedStatement stmt = conn.prepareStatement("SELECT object_type, attributes FROM " + getConfiguredObjectsTableName() + " where id = ?")) {
        stmt.setString(1, configuredObject.getId().toString());
        try (ResultSet rs = stmt.executeQuery()) {
            final ObjectMapper objectMapper = ConfiguredObjectJacksonModule.newObjectMapper(true);
            if (rs.next()) {
                try (PreparedStatement stmt2 = conn.prepareStatement("UPDATE " + getConfiguredObjectsTableName() + " set object_type =?, attributes = ? where id = ?")) {
                    stmt2.setString(1, configuredObject.getType());
                    if (configuredObject.getAttributes() != null) {
                        byte[] attributesAsBytes = objectMapper.writeValueAsBytes(configuredObject.getAttributes());
                        ByteArrayInputStream bis = new ByteArrayInputStream(attributesAsBytes);
                        stmt2.setBinaryStream(2, bis, attributesAsBytes.length);
                    } else {
                        stmt2.setNull(2, Types.BLOB);
                    }
                    stmt2.setString(3, configuredObject.getId().toString());
                    stmt2.execute();
                }
            } else if (createIfNecessary) {
                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();
                        byte[] attributesAsBytes = objectMapper.writeValueAsBytes(attributes);
                        ByteArrayInputStream bis = new ByteArrayInputStream(attributesAsBytes);
                        insertStmt.setBinaryStream(3, bis, attributesAsBytes.length);
                    }
                    insertStmt.execute();
                }
                writeHierarchy(configuredObject, conn);
            }
        }
    } catch (IOException e) {
        throw new StoreException("Error updating configured object " + configuredObject + " in database: " + e.getMessage(), e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StoreException(org.apache.qpid.server.store.StoreException)

Example 43 with StoreException

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

the class AbstractJDBCMessageStore method recordXid.

private List<Runnable> recordXid(ConnectionWrapper connWrapper, long format, byte[] globalId, byte[] branchId, Transaction.EnqueueRecord[] enqueues, Transaction.DequeueRecord[] dequeues) throws StoreException {
    Connection conn = connWrapper.getConnection();
    try {
        try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO " + getXidTableName() + " ( format, global_id, branch_id ) values (?, ?, ?)")) {
            stmt.setLong(1, format);
            stmt.setBytes(2, globalId);
            stmt.setBytes(3, branchId);
            stmt.executeUpdate();
        }
        for (Transaction.EnqueueRecord enqueue : enqueues) {
            StoredMessage storedMessage = enqueue.getMessage().getStoredMessage();
            if (storedMessage instanceof StoredJDBCMessage) {
                ((StoredJDBCMessage) storedMessage).store(conn);
            }
        }
        try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO " + getXidActionsTableName() + " ( format, global_id, branch_id, action_type, " + "queue_id, message_id ) values (?,?,?,?,?,?) ")) {
            stmt.setLong(1, format);
            stmt.setBytes(2, globalId);
            stmt.setBytes(3, branchId);
            if (enqueues != null) {
                stmt.setString(4, "E");
                for (Transaction.EnqueueRecord record : enqueues) {
                    stmt.setString(5, record.getResource().getId().toString());
                    stmt.setLong(6, record.getMessage().getMessageNumber());
                    stmt.executeUpdate();
                }
            }
            if (dequeues != null) {
                stmt.setString(4, "D");
                for (Transaction.DequeueRecord record : dequeues) {
                    stmt.setString(5, record.getEnqueueRecord().getQueueId().toString());
                    stmt.setLong(6, record.getEnqueueRecord().getMessageNumber());
                    stmt.executeUpdate();
                }
            }
        }
        return Collections.emptyList();
    } catch (SQLException e) {
        getLogger().error("Failed to record xid", e);
        throw new StoreException("Error writing xid ", e);
    }
}
Also used : Transaction(org.apache.qpid.server.store.Transaction) StoredMessage(org.apache.qpid.server.store.StoredMessage) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StoreException(org.apache.qpid.server.store.StoreException)

Example 44 with StoreException

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

the class AbstractJDBCMessageStore method getAllContent.

QpidByteBuffer getAllContent(long messageId) throws StoreException {
    getLogger().debug("Message Id: {} Getting content body", messageId);
    try (Connection conn = newAutoCommitConnection();
        PreparedStatement stmt = conn.prepareStatement("SELECT content FROM " + getMessageContentTableName() + " WHERE message_id = ?")) {
        stmt.setLong(1, messageId);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            try (InputStream blobAsInputStream = getBlobAsInputStream(rs, 1)) {
                return QpidByteBuffer.asQpidByteBuffer(blobAsInputStream);
            }
        } else {
            throw new StoreException("Unable to find message with id " + messageId);
        }
    } catch (SQLException | IOException e) {
        throw new StoreException("Error retrieving content for message " + messageId + ": " + e.getMessage(), e);
    }
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) StoreException(org.apache.qpid.server.store.StoreException)

Example 45 with StoreException

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

the class AbstractJDBCMessageStore method abortTran.

private void abortTran(ConnectionWrapper connWrapper) throws StoreException {
    if (connWrapper == null) {
        throw new StoreException("Fatal internal error: transactional context is empty at abortTran");
    }
    getLogger().debug("abort tran called: {}", connWrapper.getConnection());
    try {
        Connection conn = connWrapper.getConnection();
        conn.rollback();
        conn.close();
    } catch (SQLException e) {
        throw new StoreException("Error aborting transaction: " + e.getMessage(), e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) 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