Search in sources :

Example 46 with StoreException

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

the class AbstractJDBCMessageStore method removeMessage.

private void removeMessage(long messageId) {
    try (Connection conn = newConnection()) {
        try {
            try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM " + getMetaDataTableName() + " WHERE message_id = ?")) {
                stmt.setLong(1, messageId);
                int results = stmt.executeUpdate();
                stmt.close();
                if (results == 0) {
                    getLogger().debug("Message id {} not found (attempt to remove failed - probably application initiated rollback)", messageId);
                }
                getLogger().debug("Deleted metadata for message {}", messageId);
            }
            try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM " + getMessageContentTableName() + " WHERE message_id = ?")) {
                stmt.setLong(1, messageId);
                int results = stmt.executeUpdate();
            }
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException t) {
            // ignore - we are re-throwing underlying exception
            }
            throw e;
        }
    } catch (SQLException e) {
        throw new StoreException("Error removing message with id " + messageId + " from database: " + e.getMessage(), e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StoreException(org.apache.qpid.server.store.StoreException)

Example 47 with StoreException

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

the class AbstractJDBCMessageStore method storeMetaData.

private void storeMetaData(Connection conn, long messageId, StorableMessageMetaData metaData) throws SQLException {
    getLogger().debug("Adding metadata for message {}", messageId);
    try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO " + getMetaDataTableName() + "( message_id , meta_data ) values (?, ?)")) {
        stmt.setLong(1, messageId);
        final int bodySize = 1 + metaData.getStorableSize();
        byte[] underlying = new byte[bodySize];
        underlying[0] = (byte) metaData.getType().ordinal();
        try (QpidByteBuffer buf = QpidByteBuffer.wrap(underlying)) {
            buf.position(1);
            try (QpidByteBuffer bufSlice = buf.slice()) {
                metaData.writeToBuffer(buf);
            }
        }
        try (ByteArrayInputStream bis = new ByteArrayInputStream(underlying)) {
            stmt.setBinaryStream(2, bis, underlying.length);
            int result = stmt.executeUpdate();
            if (result == 0) {
                throw new StoreException("Unable to add meta data for message " + messageId);
            }
        } catch (IOException e) {
            throw new SQLException("Failed to close ByteArrayInputStream", e);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) IOException(java.io.IOException) StoreException(org.apache.qpid.server.store.StoreException)

Example 48 with StoreException

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

the class AbstractJDBCMessageStore method createOrOpenMessageStoreDatabase.

protected void createOrOpenMessageStoreDatabase() throws StoreException {
    try (Connection conn = newAutoCommitConnection()) {
        createVersionTable(conn);
        createQueueEntryTable(conn);
        createMetaDataTable(conn);
        createMessageContentTable(conn);
        createXidTable(conn);
        createXidActionTable(conn);
    } catch (SQLException e) {
        throw new StoreException("Failed to create message store tables", e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) StoreException(org.apache.qpid.server.store.StoreException)

Example 49 with StoreException

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

the class AbstractJDBCMessageStore method dequeueMessage.

private void dequeueMessage(ConnectionWrapper connWrapper, final UUID queueId, Long messageId) throws StoreException {
    Connection conn = connWrapper.getConnection();
    try {
        try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM " + getQueueEntryTableName() + " WHERE queue_id = ? AND message_id =?")) {
            stmt.setString(1, queueId.toString());
            stmt.setLong(2, messageId);
            int results = stmt.executeUpdate();
            if (results != 1) {
                throw new StoreException("Unable to find message with id " + messageId + " on queue with id " + queueId);
            }
            getLogger().debug("Dequeuing message {} on queue with id {}", messageId, queueId);
        }
    } catch (SQLException e) {
        getLogger().error("Failed to dequeue message {}", messageId, e);
        throw new StoreException("Error deleting enqueued message with id " + messageId + " for queue with id " + queueId + " from database", e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StoreException(org.apache.qpid.server.store.StoreException)

Example 50 with StoreException

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

the class AbstractJDBCMessageStore method enqueueMessage.

private void enqueueMessage(ConnectionWrapper connWrapper, final TransactionLogResource queue, Long messageId) throws StoreException {
    Connection conn = connWrapper.getConnection();
    try {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Enqueuing message {} on queue {} with id {} [Connection {}]", messageId, queue.getName(), queue.getId(), conn);
        }
        try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO " + getQueueEntryTableName() + " (queue_id, message_id) values (?,?)")) {
            stmt.setString(1, queue.getId().toString());
            stmt.setLong(2, messageId);
            stmt.executeUpdate();
        }
    } catch (SQLException e) {
        getLogger().error("Failed to enqueue message {}", messageId, e);
        throw new StoreException("Error writing enqueued message with id " + messageId + " for queue " + queue.getName() + " with id " + queue.getId() + " to database", e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) 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