use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class JDBCLinkStore method insert.
private void insert(final Connection connection, final String linkKey, final LinkDefinition<? extends BaseSource, ? extends BaseTarget> linkDefinition) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(String.format("INSERT INTO %s (link_key, remote_container_id, link_name, link_role, source, target) VALUES (?,?,?,?,?,?)", getLinksTableName()))) {
statement.setString(1, linkKey);
saveStringAsBlob(statement, 2, linkDefinition.getRemoteContainerId());
saveStringAsBlob(statement, 3, linkDefinition.getName());
statement.setInt(4, linkDefinition.getRole().getValue() ? 1 : 0);
saveObjectAsBlob(statement, 5, linkDefinition.getSource());
saveObjectAsBlob(statement, 6, linkDefinition.getTarget());
if (statement.executeUpdate() != 1) {
throw new StoreException(String.format("Cannot save link %s", new LinkKey(linkDefinition)));
}
}
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class AbstractDerbyMessageStore method reduceSizeOnDisk.
private void reduceSizeOnDisk(Connection conn) {
CallableStatement cs = null;
PreparedStatement stmt = null;
try {
String tableQuery = "SELECT S.SCHEMANAME, T.TABLENAME FROM SYS.SYSSCHEMAS S, SYS.SYSTABLES T WHERE S.SCHEMAID = T.SCHEMAID AND T.TABLETYPE='T'";
stmt = conn.prepareStatement(tableQuery);
ResultSet rs = null;
List<String> schemas = new ArrayList<String>();
List<String> tables = new ArrayList<String>();
try {
rs = stmt.executeQuery();
while (rs.next()) {
schemas.add(rs.getString(1));
tables.add(rs.getString(2));
}
} finally {
if (rs != null) {
rs.close();
}
}
cs = conn.prepareCall("CALL SYSCS_UTIL.SYSCS_COMPRESS_TABLE(?, ?, ?)");
for (int i = 0; i < schemas.size(); i++) {
cs.setString(1, schemas.get(i));
cs.setString(2, tables.get(i));
cs.setShort(3, (short) 0);
cs.execute();
}
} catch (SQLException e) {
throw new StoreException("Error reducing on disk size", e);
} finally {
JdbcUtils.closePreparedStatement(stmt, getLogger());
JdbcUtils.closePreparedStatement(cs, getLogger());
}
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class AbstractDerbyMessageStore method getSizeOnDisk.
private long getSizeOnDisk(Connection conn) {
PreparedStatement stmt = null;
try {
String sizeQuery = "SELECT SUM(T2.NUMALLOCATEDPAGES * T2.PAGESIZE) TOTALSIZE" + " FROM " + " SYS.SYSTABLES systabs," + " TABLE (SYSCS_DIAG.SPACE_TABLE(systabs.tablename)) AS T2" + " WHERE systabs.tabletype = 'T'";
stmt = conn.prepareStatement(sizeQuery);
ResultSet rs = null;
long size = 0l;
try {
rs = stmt.executeQuery();
while (rs.next()) {
size = rs.getLong(1);
}
} finally {
if (rs != null) {
rs.close();
}
}
return size;
} catch (SQLException e) {
throw new StoreException("Error establishing on disk size", e);
} finally {
JdbcUtils.closePreparedStatement(stmt, getLogger());
}
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class AbstractJDBCConfigurationStore method createOrOpenConfigurationStoreDatabase.
protected void createOrOpenConfigurationStoreDatabase() throws StoreException {
Connection conn = null;
try {
conn = newAutoCommitConnection();
createConfiguredObjectsTable(conn);
createConfiguredObjectHierarchyTable(conn);
} catch (SQLException e) {
throw new StoreException("Unable to open configuration tables", e);
} finally {
JdbcUtils.closeConnection(conn, getLogger());
}
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class AbstractJDBCConfigurationStore method upgradeFromV7.
private void upgradeFromV7(ConfiguredObject<?> parent) throws SQLException {
@SuppressWarnings("serial") Map<String, String> defaultExchanges = new HashMap<String, String>() {
{
put("amq.direct", "direct");
put("amq.topic", "topic");
put("amq.fanout", "fanout");
put("amq.match", "headers");
}
};
Connection connection = newConnection();
try {
String virtualHostName = parent.getName();
UUID virtualHostId = UUIDGenerator.generateVhostUUID(virtualHostName);
String stringifiedConfigVersion = "0." + DEFAULT_CONFIG_VERSION;
boolean tableExists = tableExists(getConfigurationVersionTableName(), connection);
if (tableExists) {
int configVersion = getConfigVersion(connection);
getLogger().debug("Upgrader read existing config version {}", configVersion);
stringifiedConfigVersion = "0." + configVersion;
}
Map<String, Object> virtualHostAttributes = new HashMap<String, Object>();
virtualHostAttributes.put("modelVersion", stringifiedConfigVersion);
virtualHostAttributes.put("name", virtualHostName);
ConfiguredObjectRecord virtualHostRecord = new ConfiguredObjectRecordImpl(virtualHostId, "VirtualHost", virtualHostAttributes);
insertConfiguredObject(virtualHostRecord, connection);
getLogger().debug("Upgrader created VirtualHost configuration entry with config version {}", stringifiedConfigVersion);
Map<UUID, Map<String, Object>> bindingsToUpdate = new HashMap<UUID, Map<String, Object>>();
List<UUID> others = new ArrayList<UUID>();
final ObjectMapper objectMapper = ConfiguredObjectJacksonModule.newObjectMapper(true);
PreparedStatement stmt = connection.prepareStatement("SELECT id, object_type, attributes FROM " + getConfiguredObjectsTableName());
try {
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
UUID id = UUID.fromString(rs.getString(1));
String objectType = rs.getString(2);
if ("VirtualHost".equals(objectType)) {
continue;
}
Map<String, Object> attributes = objectMapper.readValue(getBlobAsString(rs, 3), Map.class);
if (objectType.endsWith("Binding")) {
bindingsToUpdate.put(id, attributes);
} else {
if (objectType.equals("Exchange")) {
defaultExchanges.remove((String) attributes.get("name"));
}
others.add(id);
}
}
} catch (IOException e) {
throw new StoreException("Error recovering persistent state: " + e.getMessage(), e);
}
} finally {
stmt.close();
}
stmt = connection.prepareStatement("INSERT INTO " + getConfiguredObjectHierarchyTableName() + " ( child_id, parent_type, parent_id) VALUES (?,?,?)");
try {
for (UUID id : others) {
stmt.setString(1, id.toString());
stmt.setString(2, "VirtualHost");
stmt.setString(3, virtualHostId.toString());
stmt.execute();
}
for (Map.Entry<UUID, Map<String, Object>> bindingEntry : bindingsToUpdate.entrySet()) {
stmt.setString(1, bindingEntry.getKey().toString());
stmt.setString(2, "Queue");
stmt.setString(3, bindingEntry.getValue().remove("queue").toString());
stmt.execute();
stmt.setString(1, bindingEntry.getKey().toString());
stmt.setString(2, "Exchange");
stmt.setString(3, bindingEntry.getValue().remove("exchange").toString());
stmt.execute();
}
} finally {
stmt.close();
}
for (Map.Entry<String, String> defaultExchangeEntry : defaultExchanges.entrySet()) {
UUID id = UUIDGenerator.generateExchangeUUID(defaultExchangeEntry.getKey(), virtualHostName);
Map<String, Object> exchangeAttributes = new HashMap<String, Object>();
exchangeAttributes.put("name", defaultExchangeEntry.getKey());
exchangeAttributes.put("type", defaultExchangeEntry.getValue());
exchangeAttributes.put("lifetimePolicy", "PERMANENT");
Map<String, UUID> parents = Collections.singletonMap("VirtualHost", virtualHostRecord.getId());
ConfiguredObjectRecord exchangeRecord = new org.apache.qpid.server.store.ConfiguredObjectRecordImpl(id, "Exchange", exchangeAttributes, parents);
insertConfiguredObject(exchangeRecord, connection);
}
stmt = connection.prepareStatement("UPDATE " + getConfiguredObjectsTableName() + " set object_type =?, attributes = ? where id = ?");
try {
for (Map.Entry<UUID, Map<String, Object>> bindingEntry : bindingsToUpdate.entrySet()) {
stmt.setString(1, "Binding");
byte[] attributesAsBytes = objectMapper.writeValueAsBytes(bindingEntry.getValue());
ByteArrayInputStream bis = new ByteArrayInputStream(attributesAsBytes);
stmt.setBinaryStream(2, bis, attributesAsBytes.length);
stmt.setString(3, bindingEntry.getKey().toString());
stmt.execute();
}
} catch (IOException e) {
throw new StoreException("Error recovering persistent state: " + e.getMessage(), e);
} finally {
stmt.close();
}
if (tableExists) {
dropConfigVersionTable(connection);
}
connection.commit();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException re) {
}
throw e;
} finally {
connection.close();
}
}
Aggregations