Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ApplicationErrorJdbcException with UncheckedSQLException #397

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

import org.kiwiproject.dropwizard.error.dao.ApplicationErrorDao;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc.ApplicationErrorJdbcException;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorStatus;
import org.kiwiproject.dropwizard.error.model.ApplicationError;
import org.kiwiproject.jdbc.UncheckedSQLException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -27,6 +26,8 @@
import java.util.List;
import java.util.Optional;

import javax.sql.DataSource;

/**
* Implementation of {@link ApplicationErrorDao} that uses plain JDBC, and therefore does not require
* any additional dependencies outside the JDK. It might be useful when an application is using a
Expand Down Expand Up @@ -58,7 +59,7 @@ public Optional<ApplicationError> getById(long id) {
return Optional.empty();
}
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -82,7 +83,7 @@ private long countUsingQuery(String sql) {
nextOrThrow(rs);
return rs.getLong(1);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -98,7 +99,7 @@ public long countUnresolvedErrorsSince(ZonedDateTime since) {
return rs.getLong(1);
}
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -117,7 +118,7 @@ public long countUnresolvedErrorsOnHostSince(ZonedDateTime since, String hostNam
return rs.getLong(1);
}
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -130,7 +131,7 @@ public List<ApplicationError> getAllErrors(int pageNumber, int pageSize) {
try (var conn = connection(); var stmt = conn.createStatement(); var rs = stmt.executeQuery(sql)) {
return collectErrors(rs, pageSize);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -154,7 +155,7 @@ private List<ApplicationError> getErrors(boolean resolved, int pageNumber, int p
ps.setBoolean(1, resolved);
return collectErrors(ps, pageSize);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -171,7 +172,7 @@ public List<ApplicationError> getUnresolvedErrorsByDescription(String descriptio
ps.setString(1, description);
return collectErrors(ps, DEFAULT_PAGE_SIZE);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -185,7 +186,7 @@ public List<ApplicationError> getUnresolvedErrorsByDescriptionAndHost(String des
ps.setString(2, hostName);
return collectErrors(ps, DEFAULT_PAGE_SIZE);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand Down Expand Up @@ -232,7 +233,7 @@ public long insertError(ApplicationError newError) {
return generatedKeys.getLong(1);
}
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -248,7 +249,7 @@ public void incrementCount(long id) {
var count = ps.executeUpdate();
checkState(count == 1, "Unable to increment count. No ApplicationError found with id %s", id);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand Down Expand Up @@ -277,7 +278,7 @@ public ApplicationError resolve(long id) {
var count = ps.executeUpdate();
checkState(count == 1, "Unable to resolve. No ApplicationError found with id %s", id);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}

return getById(id).orElseThrow();
Expand All @@ -290,7 +291,7 @@ public int resolveAllUnresolvedErrors() {
try (var conn = connection(); var stmt = conn.createStatement()) {
return stmt.executeUpdate(sql);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand All @@ -311,7 +312,7 @@ private int deleteResolvedErrorsBeforeUsingQuery(String sql, ZonedDateTime expir
ps.setTimestamp(1, timestampFromZonedDateTime(expirationDate));
return ps.executeUpdate();
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
import lombok.extern.slf4j.Slf4j;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc.ApplicationErrorJdbcException;
import org.kiwiproject.jdbc.UncheckedSQLException;
import org.kiwiproject.test.jdbc.SimpleSingleConnectionDataSource;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

@UtilityClass
@Slf4j
public class TestHelpers {
Expand Down Expand Up @@ -58,7 +60,7 @@ public static void migrateDatabase(JdbcDatabaseContainer<?> container, String mi
try (var conn = DriverManager.getConnection(jdbcUrl, container.getUsername(), container.getPassword())) {
ApplicationErrorJdbc.migrateDatabase(conn, migrationsFilename);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}

LOG.info("Completed migrating {} container database using file {}", dockerImageName, migrationsFilename);
Expand All @@ -84,7 +86,7 @@ public static void migrateDatabase(SimpleSingleConnectionDataSource dataSource,
try {
ApplicationErrorJdbc.migrateDatabase(dataSource.getConnection(), migrationsFilename);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
throw new UncheckedSQLException(e);
}
LOG.info("Completed migrating database using file {}", migrationsFilename);
}
Expand Down