use of org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException in project product-edc by catenax-ng.
the class AbstractPostgresqlMigrationExtension method initialize.
@Override
public void initialize(final ServiceExtensionContext context) {
final String subSystemName = Objects.requireNonNull(getSubsystemName());
final String dataSourceName = context.getConfig().getString(getDataSourceNameConfigurationKey(), null);
if (dataSourceName == null) {
return;
}
boolean enabled = context.getConfig().getBoolean(String.format("net.catenax.edc.postgresql.migration.%s.enabled", subSystemName), true);
if (!enabled) {
return;
}
Config datasourceConfiguration = context.getConfig(String.join(".", EDC_DATASOURCE_PREFIX, dataSourceName));
final String jdbcUrl = Objects.requireNonNull(datasourceConfiguration.getString("url"));
final Properties jdbcProperties = new Properties();
jdbcProperties.putAll(datasourceConfiguration.getRelativeEntries());
final DriverManagerConnectionFactory driverManagerConnectionFactory = new DriverManagerConnectionFactory(jdbcUrl, jdbcProperties);
final ConnectionFactoryDataSource dataSource = new ConnectionFactoryDataSource(driverManagerConnectionFactory);
final String schemaHistoryTableName = getSchemaHistoryTableName(subSystemName);
final String migrationsLocation = getMigrationsLocation();
final Flyway flyway = Flyway.configure().baselineVersion(MigrationVersion.fromVersion("0.0.0")).failOnMissingLocations(true).dataSource(dataSource).table(schemaHistoryTableName).locations(migrationsLocation).load();
flyway.baseline();
final MigrateResult migrateResult = flyway.migrate();
if (!migrateResult.success) {
throw new EdcPersistenceException(String.format("Migrating DataSource %s for subsystem %s failed: %s", dataSourceName, subSystemName, String.join(", ", migrateResult.warnings)));
}
}
use of org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException in project DataSpaceConnector by eclipse-dataspaceconnector.
the class CommonsConnectionPoolTest method getConnectionSqlExceptionThrownThrowsSame.
@Test
void getConnectionSqlExceptionThrownThrowsSame() throws SQLException {
DataSource dataSource = Mockito.mock(DataSource.class);
CommonsConnectionPoolConfig commonsConnectionPoolConfig = CommonsConnectionPoolConfig.Builder.newInstance().build();
CommonsConnectionPool connectionPool = new CommonsConnectionPool(dataSource, commonsConnectionPoolConfig);
SQLException causingSqlException = new SQLException("intended to be thrown");
Mockito.when(dataSource.getConnection()).thenThrow(causingSqlException);
EdcPersistenceException sqlException = Assertions.assertThrows(EdcPersistenceException.class, connectionPool::getConnection);
Assertions.assertNotNull(sqlException.getCause());
Assertions.assertEquals(causingSqlException, sqlException.getCause());
Mockito.verify(dataSource, Mockito.atLeastOnce()).getConnection();
}
use of org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException in project DataSpaceConnector by eclipse-dataspaceconnector.
the class CommonsConnectionPoolTest method getConnectionAnyExceptionThrownThrowsSqlException.
@Test
void getConnectionAnyExceptionThrownThrowsSqlException() throws SQLException {
DataSource dataSource = Mockito.mock(DataSource.class);
CommonsConnectionPoolConfig commonsConnectionPoolConfig = CommonsConnectionPoolConfig.Builder.newInstance().build();
CommonsConnectionPool connectionPool = new CommonsConnectionPool(dataSource, commonsConnectionPoolConfig);
RuntimeException causingRuntimeException = new RuntimeException("intended to be thrown");
Mockito.when(dataSource.getConnection()).thenThrow(causingRuntimeException);
EdcPersistenceException exceptionWrappingRuntimeException = Assertions.assertThrows(EdcPersistenceException.class, connectionPool::getConnection);
Assertions.assertNotNull(exceptionWrappingRuntimeException.getCause());
Assertions.assertEquals(causingRuntimeException, exceptionWrappingRuntimeException.getCause());
Mockito.verify(dataSource, Mockito.atLeastOnce()).getConnection();
}
use of org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException in project DataSpaceConnector by eclipse-dataspaceconnector.
the class SqlTransferProcessStore method update.
@Override
public void update(TransferProcess process) {
transactionContext.execute(() -> {
String id = process.getId();
var existing = find(id);
if (existing == null) {
insert(process);
} else {
try (var conn = getConnection()) {
leaseContext.by(leaseHolderName).withConnection(conn).breakLease(id);
update(conn, id, process);
} catch (SQLException e) {
throw new EdcPersistenceException(e);
}
}
});
}
use of org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException in project DataSpaceConnector by eclipse-dataspaceconnector.
the class SqlTransferProcessStore method delete.
@Override
public void delete(String processId) {
transactionContext.execute(() -> {
var existing = find(processId);
if (existing != null) {
try (var conn = getConnection()) {
// attempt to acquire lease - should fail if someone else holds the lease
leaseContext.by(leaseHolderName).withConnection(conn).acquireLease(processId);
var stmt = statements.getDeleteTransferProcessTemplate();
executeQuery(conn, stmt, processId);
// necessary to delete the row in edc_lease
leaseContext.by(leaseHolderName).withConnection(conn).breakLease(processId);
} catch (SQLException e) {
throw new EdcPersistenceException(e);
}
}
});
}
Aggregations