#587 SonarQube reports bugs in dao module
This commit is contained in:
		
							
								
								
									
										43
									
								
								dao/src/main/java/com/iluwatar/dao/CustomException.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								dao/src/main/java/com/iluwatar/dao/CustomException.java
									
									
									
									
									
										Normal 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);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -36,12 +36,16 @@ import java.util.stream.StreamSupport;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import javax.sql.DataSource;
 | 
					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 {
 | 
					public class DbCustomerDao implements CustomerDao {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private static final Logger LOGGER = Logger.getLogger(DbCustomerDao.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private final DataSource dataSource;
 | 
					  private final DataSource dataSource;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
@@ -79,12 +83,12 @@ public class DbCustomerDao implements CustomerDao {
 | 
				
			|||||||
            action.accept(createCustomer(resultSet));
 | 
					            action.accept(createCustomer(resultSet));
 | 
				
			||||||
            return true;
 | 
					            return true;
 | 
				
			||||||
          } catch (SQLException e) {
 | 
					          } catch (SQLException e) {
 | 
				
			||||||
            throw new RuntimeException(e);
 | 
					            throw new RuntimeException(e); // NOSONAR
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }, false).onClose(() -> mutedClose(connection, statement, resultSet));
 | 
					      }, false).onClose(() -> mutedClose(connection, statement, resultSet));
 | 
				
			||||||
    } catch (SQLException e) {
 | 
					    } 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();
 | 
					      statement.close();
 | 
				
			||||||
      connection.close();
 | 
					      connection.close();
 | 
				
			||||||
    } catch (SQLException e) {
 | 
					    } catch (SQLException e) {
 | 
				
			||||||
      e.printStackTrace();
 | 
					      LOGGER.info("Exception thrown " + e.getMessage());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -113,19 +117,26 @@ public class DbCustomerDao implements CustomerDao {
 | 
				
			|||||||
   */
 | 
					   */
 | 
				
			||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
  public Optional<Customer> getById(int id) throws Exception {
 | 
					  public Optional<Customer> getById(int id) throws Exception {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ResultSet resultSet = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try (Connection connection = getConnection();
 | 
					    try (Connection connection = getConnection();
 | 
				
			||||||
        PreparedStatement statement = 
 | 
					        PreparedStatement statement = 
 | 
				
			||||||
            connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
 | 
					            connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      statement.setInt(1, id);
 | 
					      statement.setInt(1, id);
 | 
				
			||||||
      ResultSet resultSet = statement.executeQuery();
 | 
					      resultSet = statement.executeQuery();
 | 
				
			||||||
      if (resultSet.next()) {
 | 
					      if (resultSet.next()) {
 | 
				
			||||||
        return Optional.of(createCustomer(resultSet));
 | 
					        return Optional.of(createCustomer(resultSet));
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        return Optional.empty();
 | 
					        return Optional.empty();
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } catch (SQLException ex) {
 | 
					    } 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();
 | 
					      statement.execute();
 | 
				
			||||||
      return true;
 | 
					      return true;
 | 
				
			||||||
    } catch (SQLException ex) {
 | 
					    } 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());
 | 
					      statement.setInt(3, customer.getId());
 | 
				
			||||||
      return statement.executeUpdate() > 0;
 | 
					      return statement.executeUpdate() > 0;
 | 
				
			||||||
    } catch (SQLException ex) {
 | 
					    } 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());
 | 
					      statement.setInt(1, customer.getId());
 | 
				
			||||||
      return statement.executeUpdate() > 0;
 | 
					      return statement.executeUpdate() > 0;
 | 
				
			||||||
    } catch (SQLException ex) {
 | 
					    } catch (SQLException ex) {
 | 
				
			||||||
      throw new Exception(ex.getMessage(), ex);
 | 
					      throw new CustomException(ex.getMessage(), ex);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user