Skip to content

Commit 1b0f55c

Browse files
authored
Merge pull request iluwatar#633 from mookkiah/issue_587_da0_2
iluwatar#587 SonarQube reports bugs in dao module
2 parents 5836fdb + e957963 commit 1b0f55c

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.dao;
24+
25+
/**
26+
*
27+
* Custom exception
28+
*
29+
*/
30+
public class CustomException extends Exception {
31+
32+
private static final long serialVersionUID = 1L;
33+
34+
public CustomException() {}
35+
36+
public CustomException(String message) {
37+
super(message);
38+
}
39+
40+
public CustomException(String message, Throwable cause) {
41+
super(message, cause);
42+
}
43+
}

dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java

+22-11
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@
3636

3737
import javax.sql.DataSource;
3838

39+
import org.apache.log4j.Logger;
40+
3941
/**
40-
* An implementation of {@link CustomerDao} that persists customers in RDBMS.
42+
* An implementation of {@link CustomerDao} that persists customers in RDBMS.
4143
*
4244
*/
4345
public class DbCustomerDao implements CustomerDao {
4446

47+
private static final Logger LOGGER = Logger.getLogger(DbCustomerDao.class);
48+
4549
private final DataSource dataSource;
4650

4751
/**
@@ -65,8 +69,8 @@ public Stream<Customer> getAll() throws Exception {
6569
Connection connection;
6670
try {
6771
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
7074
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
7175
Spliterator.ORDERED) {
7276

@@ -79,12 +83,12 @@ public boolean tryAdvance(Consumer<? super Customer> action) {
7983
action.accept(createCustomer(resultSet));
8084
return true;
8185
} catch (SQLException e) {
82-
throw new RuntimeException(e);
86+
throw new RuntimeException(e); // NOSONAR
8387
}
8488
}
8589
}, false).onClose(() -> mutedClose(connection, statement, resultSet));
8690
} catch (SQLException e) {
87-
throw new Exception(e.getMessage(), e);
91+
throw new CustomException(e.getMessage(), e);
8892
}
8993
}
9094

@@ -98,7 +102,7 @@ private void mutedClose(Connection connection, PreparedStatement statement, Resu
98102
statement.close();
99103
connection.close();
100104
} catch (SQLException e) {
101-
e.printStackTrace();
105+
LOGGER.info("Exception thrown " + e.getMessage());
102106
}
103107
}
104108

@@ -113,19 +117,26 @@ private Customer createCustomer(ResultSet resultSet) throws SQLException {
113117
*/
114118
@Override
115119
public Optional<Customer> getById(int id) throws Exception {
120+
121+
ResultSet resultSet = null;
122+
116123
try (Connection connection = getConnection();
117124
PreparedStatement statement =
118125
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
119126

120127
statement.setInt(1, id);
121-
ResultSet resultSet = statement.executeQuery();
128+
resultSet = statement.executeQuery();
122129
if (resultSet.next()) {
123130
return Optional.of(createCustomer(resultSet));
124131
} else {
125132
return Optional.empty();
126133
}
127134
} 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+
}
129140
}
130141
}
131142

@@ -147,7 +158,7 @@ public boolean add(Customer customer) throws Exception {
147158
statement.execute();
148159
return true;
149160
} catch (SQLException ex) {
150-
throw new Exception(ex.getMessage(), ex);
161+
throw new CustomException(ex.getMessage(), ex);
151162
}
152163
}
153164

@@ -164,7 +175,7 @@ public boolean update(Customer customer) throws Exception {
164175
statement.setInt(3, customer.getId());
165176
return statement.executeUpdate() > 0;
166177
} catch (SQLException ex) {
167-
throw new Exception(ex.getMessage(), ex);
178+
throw new CustomException(ex.getMessage(), ex);
168179
}
169180
}
170181

@@ -179,7 +190,7 @@ public boolean delete(Customer customer) throws Exception {
179190
statement.setInt(1, customer.getId());
180191
return statement.executeUpdate() > 0;
181192
} catch (SQLException ex) {
182-
throw new Exception(ex.getMessage(), ex);
193+
throw new CustomException(ex.getMessage(), ex);
183194
}
184195
}
185196
}

0 commit comments

Comments
 (0)