36
36
37
37
import javax .sql .DataSource ;
38
38
39
+ import org .apache .log4j .Logger ;
40
+
39
41
/**
40
- * An implementation of {@link CustomerDao} that persists customers in RDBMS.
42
+ * An implementation of {@link CustomerDao} that persists customers in RDBMS.
41
43
*
42
44
*/
43
45
public class DbCustomerDao implements CustomerDao {
44
46
47
+ private static final Logger LOGGER = Logger .getLogger (DbCustomerDao .class );
48
+
45
49
private final DataSource dataSource ;
46
50
47
51
/**
@@ -65,8 +69,8 @@ public Stream<Customer> getAll() throws Exception {
65
69
Connection connection ;
66
70
try {
67
71
connection = getConnection ();
68
- PreparedStatement statement = connection .prepareStatement ("SELECT * FROM CUSTOMERS" ); // NOSONAR
69
- ResultSet resultSet = statement .executeQuery (); //NOSONAR
72
+ PreparedStatement statement = connection .prepareStatement ("SELECT * FROM CUSTOMERS" ); // NOSONAR
73
+ ResultSet resultSet = statement .executeQuery (); // NOSONAR
70
74
return StreamSupport .stream (new Spliterators .AbstractSpliterator <Customer >(Long .MAX_VALUE ,
71
75
Spliterator .ORDERED ) {
72
76
@@ -79,12 +83,12 @@ public boolean tryAdvance(Consumer<? super Customer> action) {
79
83
action .accept (createCustomer (resultSet ));
80
84
return true ;
81
85
} catch (SQLException e ) {
82
- throw new RuntimeException (e );
86
+ throw new RuntimeException (e ); // NOSONAR
83
87
}
84
88
}
85
89
}, false ).onClose (() -> mutedClose (connection , statement , resultSet ));
86
90
} catch (SQLException e ) {
87
- throw new Exception (e .getMessage (), e );
91
+ throw new CustomException (e .getMessage (), e );
88
92
}
89
93
}
90
94
@@ -98,7 +102,7 @@ private void mutedClose(Connection connection, PreparedStatement statement, Resu
98
102
statement .close ();
99
103
connection .close ();
100
104
} catch (SQLException e ) {
101
- e . printStackTrace ( );
105
+ LOGGER . info ( "Exception thrown " + e . getMessage () );
102
106
}
103
107
}
104
108
@@ -113,19 +117,26 @@ private Customer createCustomer(ResultSet resultSet) throws SQLException {
113
117
*/
114
118
@ Override
115
119
public Optional <Customer > getById (int id ) throws Exception {
120
+
121
+ ResultSet resultSet = null ;
122
+
116
123
try (Connection connection = getConnection ();
117
124
PreparedStatement statement =
118
125
connection .prepareStatement ("SELECT * FROM CUSTOMERS WHERE ID = ?" )) {
119
126
120
127
statement .setInt (1 , id );
121
- ResultSet resultSet = statement .executeQuery ();
128
+ resultSet = statement .executeQuery ();
122
129
if (resultSet .next ()) {
123
130
return Optional .of (createCustomer (resultSet ));
124
131
} else {
125
132
return Optional .empty ();
126
133
}
127
134
} catch (SQLException ex ) {
128
- throw new Exception (ex .getMessage (), ex );
135
+ throw new CustomException (ex .getMessage (), ex );
136
+ } finally {
137
+ if (resultSet != null ) {
138
+ resultSet .close ();
139
+ }
129
140
}
130
141
}
131
142
@@ -147,7 +158,7 @@ public boolean add(Customer customer) throws Exception {
147
158
statement .execute ();
148
159
return true ;
149
160
} catch (SQLException ex ) {
150
- throw new Exception (ex .getMessage (), ex );
161
+ throw new CustomException (ex .getMessage (), ex );
151
162
}
152
163
}
153
164
@@ -164,7 +175,7 @@ public boolean update(Customer customer) throws Exception {
164
175
statement .setInt (3 , customer .getId ());
165
176
return statement .executeUpdate () > 0 ;
166
177
} catch (SQLException ex ) {
167
- throw new Exception (ex .getMessage (), ex );
178
+ throw new CustomException (ex .getMessage (), ex );
168
179
}
169
180
}
170
181
@@ -179,7 +190,7 @@ public boolean delete(Customer customer) throws Exception {
179
190
statement .setInt (1 , customer .getId ());
180
191
return statement .executeUpdate () > 0 ;
181
192
} catch (SQLException ex ) {
182
- throw new Exception (ex .getMessage (), ex );
193
+ throw new CustomException (ex .getMessage (), ex );
183
194
}
184
195
}
185
196
}
0 commit comments