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