19
19
@ RequiredArgsConstructor
20
20
public abstract class RecordBase <T extends RecordBase <?>> {
21
21
22
+ private static final String EXCEPTION_MESSAGE = "Couldn't execute database query for the following domain model :" ;
23
+
22
24
@ Setter
23
25
private static DataSource dataSource ;
24
26
25
27
@ SuppressWarnings ({"unchecked" })
26
28
private final Class <T > clazz = (Class <T >) getClass ();
27
29
28
- protected Connection getConnection () throws SQLException {
29
- return dataSource .getConnection ();
30
+ protected Connection getConnection () {
31
+ try {
32
+ return dataSource .getConnection ();
33
+ } catch (SQLException e ) {
34
+ throw new RuntimeException ("Unable to acquire database connection" , e );
35
+ }
30
36
}
31
37
32
38
/**
@@ -58,9 +64,7 @@ public List<T> findAll() {
58
64
return recordList ;
59
65
}
60
66
} catch (SQLException e ) {
61
- throw new RuntimeException (
62
- "Unable to find all the records for the following domain model : " + clazz .getName ()
63
- + " due to the data persistence error" , e );
67
+ throw new RuntimeException (EXCEPTION_MESSAGE + clazz .getName (), e );
64
68
}
65
69
}
66
70
@@ -83,10 +87,7 @@ public T findById(Long id) {
83
87
return getDeclaredClassInstance ();
84
88
}
85
89
} catch (SQLException e ) {
86
- throw new RuntimeException (
87
- "Unable to find a record for the following domain model : " + clazz .getName () + " by id="
88
- + id
89
- + " due to the data persistence error" , e );
90
+ throw new RuntimeException (EXCEPTION_MESSAGE + clazz .getName () + " with id=" + id , e );
90
91
}
91
92
}
92
93
@@ -103,9 +104,7 @@ public void save() {
103
104
pstmt .executeUpdate ();
104
105
105
106
} catch (SQLException e ) {
106
- throw new RuntimeException (
107
- "Unable to save the record for the following domain model : " + clazz .getName ()
108
- + " due to the data persistence error" , e );
107
+ throw new RuntimeException (EXCEPTION_MESSAGE + clazz .getName (), e );
109
108
}
110
109
}
111
110
0 commit comments