diff --git a/dao/src/main/java/com/iluwatar/dao/CustomException.java b/dao/src/main/java/com/iluwatar/dao/CustomException.java new file mode 100644 index 000000000..fd291b605 --- /dev/null +++ b/dao/src/main/java/com/iluwatar/dao/CustomException.java @@ -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); + } +} diff --git a/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java b/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java index 6e93207cc..fa2e411da 100644 --- a/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java +++ b/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java @@ -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. + * 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; /** @@ -65,8 +69,8 @@ public class DbCustomerDao implements CustomerDao { Connection connection; try { connection = getConnection(); - PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); //NOSONAR - ResultSet resultSet = statement.executeQuery(); //NOSONAR + PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); // NOSONAR + ResultSet resultSet = statement.executeQuery(); // NOSONAR return StreamSupport.stream(new Spliterators.AbstractSpliterator(Long.MAX_VALUE, Spliterator.ORDERED) { @@ -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 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); } } }