Search in sources :

Example 1 with EdcPersistenceException

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)));
    }
}
Also used : Flyway(org.flywaydb.core.Flyway) MigrateResult(org.flywaydb.core.api.output.MigrateResult) Config(org.eclipse.dataspaceconnector.spi.system.configuration.Config) ConnectionFactoryDataSource(org.eclipse.dataspaceconnector.sql.datasource.ConnectionFactoryDataSource) Properties(java.util.Properties) EdcPersistenceException(org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException)

Example 2 with EdcPersistenceException

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();
}
Also used : SQLException(java.sql.SQLException) EdcPersistenceException(org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 3 with EdcPersistenceException

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();
}
Also used : EdcPersistenceException(org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 4 with EdcPersistenceException

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);
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) EdcPersistenceException(org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException)

Example 5 with EdcPersistenceException

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);
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) EdcPersistenceException(org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException)

Aggregations

EdcPersistenceException (org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException)30 SQLException (java.sql.SQLException)26 Test (org.junit.jupiter.api.Test)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 NotNull (org.jetbrains.annotations.NotNull)6 IOException (java.io.IOException)4 AbstractMap (java.util.AbstractMap)3 DataSource (javax.sql.DataSource)3 ComponentTest (org.eclipse.dataspaceconnector.common.annotations.ComponentTest)3 Nullable (org.jetbrains.annotations.Nullable)3 DisplayName (org.junit.jupiter.api.DisplayName)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Optional.ofNullable (java.util.Optional.ofNullable)2 AssetEntry (org.eclipse.dataspaceconnector.dataloading.AssetEntry)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 String.format (java.lang.String.format)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1