Merge pull request #633 from mookkiah/issue_587_da0_2

#587 SonarQube reports bugs in dao module
This commit is contained in:
Ilkka Seppälä 2017-09-19 08:48:23 +03:00 committed by GitHub
commit 1b0f55c3eb
2 changed files with 65 additions and 11 deletions

View File

@ -0,0 +1,43 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.dao;
/**
*
* Custom exception
*
*/
public class CustomException extends Exception {
private static final long serialVersionUID = 1L;
public CustomException() {}
public CustomException(String message) {
super(message);
}
public CustomException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -36,12 +36,16 @@ import java.util.stream.StreamSupport;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
/**
* An implementation of {@link CustomerDao} that persists customers in RDBMS.
*
*/
public class DbCustomerDao implements CustomerDao {
private static final Logger LOGGER = Logger.getLogger(DbCustomerDao.class);
private final DataSource dataSource;
/**
@ -79,12 +83,12 @@ public class DbCustomerDao implements CustomerDao {
action.accept(createCustomer(resultSet));
return true;
} catch (SQLException e) {
throw new RuntimeException(e);
throw new RuntimeException(e); // NOSONAR
}
}
}, false).onClose(() -> mutedClose(connection, statement, resultSet));
} catch (SQLException e) {
throw new Exception(e.getMessage(), e);
throw new CustomException(e.getMessage(), e);
}
}
@ -98,7 +102,7 @@ public class DbCustomerDao implements CustomerDao {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
LOGGER.info("Exception thrown " + e.getMessage());
}
}
@ -113,19 +117,26 @@ public class DbCustomerDao implements CustomerDao {
*/
@Override
public Optional<Customer> getById(int id) throws Exception {
ResultSet resultSet = null;
try (Connection connection = getConnection();
PreparedStatement statement =
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
resultSet = statement.executeQuery();
if (resultSet.next()) {
return Optional.of(createCustomer(resultSet));
} else {
return Optional.empty();
}
} catch (SQLException ex) {
throw new Exception(ex.getMessage(), ex);
throw new CustomException(ex.getMessage(), ex);
} finally {
if (resultSet != null) {
resultSet.close();
}
}
}
@ -147,7 +158,7 @@ public class DbCustomerDao implements CustomerDao {
statement.execute();
return true;
} catch (SQLException ex) {
throw new Exception(ex.getMessage(), ex);
throw new CustomException(ex.getMessage(), ex);
}
}
@ -164,7 +175,7 @@ public class DbCustomerDao implements CustomerDao {
statement.setInt(3, customer.getId());
return statement.executeUpdate() > 0;
} catch (SQLException ex) {
throw new Exception(ex.getMessage(), ex);
throw new CustomException(ex.getMessage(), ex);
}
}
@ -179,7 +190,7 @@ public class DbCustomerDao implements CustomerDao {
statement.setInt(1, customer.getId());
return statement.executeUpdate() > 0;
} catch (SQLException ex) {
throw new Exception(ex.getMessage(), ex);
throw new CustomException(ex.getMessage(), ex);
}
}
}