Skip to content

Commit

Permalink
work on exceptions (iluwatar#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergejsvisockis committed Apr 15, 2024
1 parent 99d5a5a commit ad80931
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@
@RequiredArgsConstructor
public abstract class RecordBase<T extends RecordBase<?>> {

private static final String EXCEPTION_MESSAGE = "Couldn't execute database query for the following domain model :";

@Setter
private static DataSource dataSource;

@SuppressWarnings({"unchecked"})
private final Class<T> clazz = (Class<T>) getClass();

protected Connection getConnection() throws SQLException {
return dataSource.getConnection();
protected Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("Unable to acquire database connection", e);
}
}

/**
Expand Down Expand Up @@ -58,9 +64,7 @@ public List<T> findAll() {
return recordList;
}
} catch (SQLException e) {
throw new RuntimeException(
"Unable to find all the records for the following domain model : " + clazz.getName()
+ " due to the data persistence error", e);
throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName(), e);
}
}

Expand All @@ -83,10 +87,7 @@ public T findById(Long id) {
return getDeclaredClassInstance();
}
} catch (SQLException e) {
throw new RuntimeException(
"Unable to find a record for the following domain model : " + clazz.getName() + " by id="
+ id
+ " due to the data persistence error", e);
throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName() + " with id=" + id, e);
}
}

Expand All @@ -103,9 +104,7 @@ public void save() {
pstmt.executeUpdate();

} catch (SQLException e) {
throw new RuntimeException(
"Unable to save the record for the following domain model : " + clazz.getName()
+ " due to the data persistence error", e);
throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName(), e);
}
}

Expand Down

0 comments on commit ad80931

Please sign in to comment.