Search in sources :

Example 11 with JdbcRepositoryException

use of uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException in project microservice_framework by CJSCommonPlatform.

the class EventJdbcRepository method clear.

public void clear(final UUID streamId) {
    final long eventCount = getStreamSize(streamId);
    try (final PreparedStatementWrapper ps = jdbcRepositoryHelper.preparedStatementWrapperOf(dataSource, SQL_DELETE_STREAM)) {
        ps.setObject(1, streamId);
        final int deletedRows = ps.executeUpdate();
        if (deletedRows != eventCount) {
            // Rollback, something went wrong
            throw new JdbcRepositoryException(format(DELETING_STREAM_EXCEPTION_DETAILS, streamId, eventCount, deletedRows));
        }
    } catch (final SQLException e) {
        throw new JdbcRepositoryException(format(DELETING_STREAM_EXCEPTION, streamId), e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatementWrapper(uk.gov.justice.services.jdbc.persistence.PreparedStatementWrapper) JdbcRepositoryException(uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException)

Example 12 with JdbcRepositoryException

use of uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException in project microservice_framework by CJSCommonPlatform.

the class EventJdbcRepository method getStreamSize.

/**
 * Returns the current position for the given stream streamId.
 *
 * @param streamId streamId of the stream.
 * @return current position streamId for the stream.  Returns 0 if stream doesn't exist.
 */
public long getStreamSize(final UUID streamId) {
    try (final PreparedStatementWrapper ps = jdbcRepositoryHelper.preparedStatementWrapperOf(dataSource, SQL_FIND_LATEST_POSITION)) {
        ps.setObject(1, streamId);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet.next()) {
            return resultSet.getLong(1);
        }
    } catch (final SQLException e) {
        logger.warn(FAILED_TO_READ_STREAM, streamId, e);
        throw new JdbcRepositoryException(format(READING_STREAM_EXCEPTION, streamId), e);
    }
    return NO_EXISTING_VERSION;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatementWrapper(uk.gov.justice.services.jdbc.persistence.PreparedStatementWrapper) JdbcRepositoryException(uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException)

Example 13 with JdbcRepositoryException

use of uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException in project microservice_framework by CJSCommonPlatform.

the class EventJdbcRepository method findByStreamIdOrderByPositionAsc.

/**
 * Returns a Stream of {@link Event} for the given stream streamId.
 *
 * @param streamId streamId of the stream.
 * @return a stream of {@link Event}. Never returns null.
 */
public Stream<Event> findByStreamIdOrderByPositionAsc(final UUID streamId) {
    try {
        final PreparedStatementWrapper ps = jdbcRepositoryHelper.preparedStatementWrapperOf(dataSource, SQL_FIND_BY_STREAM_ID);
        ps.setObject(1, streamId);
        return jdbcRepositoryHelper.streamOf(ps, entityFromFunction());
    } catch (final SQLException e) {
        logger.warn(FAILED_TO_READ_STREAM, streamId, e);
        throw new JdbcRepositoryException(format(READING_STREAM_EXCEPTION, streamId), e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatementWrapper(uk.gov.justice.services.jdbc.persistence.PreparedStatementWrapper) JdbcRepositoryException(uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException)

Example 14 with JdbcRepositoryException

use of uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException in project microservice_framework by CJSCommonPlatform.

the class EventStreamJdbcRepository method delete.

public void delete(final UUID streamId) {
    try (final PreparedStatementWrapper ps = eventStreamJdbcRepositoryHelper.preparedStatementWrapperOf(dataSource, SQL_DELETE_EVENT_STREAM)) {
        ps.setObject(1, streamId);
        ps.executeUpdate();
    } catch (final SQLException e) {
        throw new JdbcRepositoryException(format(EVENT_STREAM_EXCEPTION_MESSAGE, streamId), e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatementWrapper(uk.gov.justice.services.jdbc.persistence.PreparedStatementWrapper) JdbcRepositoryException(uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException)

Example 15 with JdbcRepositoryException

use of uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException in project microservice_framework by CJSCommonPlatform.

the class EventStreamJdbcRepository method getPosition.

public long getPosition(final UUID streamId) {
    try (final PreparedStatementWrapper psquery = eventStreamJdbcRepositoryHelper.preparedStatementWrapperOf(dataSource, SQL_FIND_POSITION_BY_STREAM)) {
        psquery.setObject(1, streamId);
        ResultSet resultSet = psquery.executeQuery();
        if (resultSet.next()) {
            return resultSet.getLong(1);
        }
        throw new InvalidStreamIdException("Invalid Stream Id: " + streamId.toString());
    } catch (final SQLException e) {
        throw new JdbcRepositoryException(format(EVENT_STREAM_EXCEPTION_MESSAGE, streamId), e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatementWrapper(uk.gov.justice.services.jdbc.persistence.PreparedStatementWrapper) JdbcRepositoryException(uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException) InvalidStreamIdException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.InvalidStreamIdException)

Aggregations

SQLException (java.sql.SQLException)17 JdbcRepositoryException (uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException)17 PreparedStatementWrapper (uk.gov.justice.services.jdbc.persistence.PreparedStatementWrapper)16 ResultSet (java.sql.ResultSet)3 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 InvalidStreamIdException (uk.gov.justice.services.eventsourcing.repository.jdbc.exception.InvalidStreamIdException)1