AuditDBHelper { public boolean update(Set<Tuple> tupleSet) { LOG.info("Connecting to DB ..."); Connection connection = getConnection( config.getString(AuditDBConstants.JDBC_DRIVER_CLASS_NAME), config.getString(AuditDBConstants.DB_URL), config.getString(AuditDBConstants.DB_USERNAME), config.getString(AuditDBConstants.DB_PASSWORD)); if (connection == null) { LOG.error("Connection not initialized returning ..."); return false; } LOG.info("Connected to DB"); boolean isUpdate = false, isInsert = false; ResultSet rs = null; String selectstatement = getSelectStmtForUpdation(); String insertStatement = getInsertStmtForUpdation(); String updateStatement = getUpdateStmtForUpdation(); PreparedStatement selectPreparedStatement = null, insertPreparedStatement = null, updatePreparedStatement = null; try { selectPreparedStatement = connection.prepareStatement(selectstatement); insertPreparedStatement = connection.prepareStatement(insertStatement); updatePreparedStatement = connection.prepareStatement(updateStatement); for (Tuple tuple : tupleSet) { rs = executeSelectStmtUpdation(selectPreparedStatement, tuple); if (rs.next()) { if (!addToUpdateStatementBatch(updatePreparedStatement, tuple, rs)) return false; isUpdate = true; } else { if (!addToInsertStatementBatch(insertPreparedStatement, tuple)) return false; isInsert = true; } } if (isUpdate) updatePreparedStatement.executeBatch(); if (isInsert) insertPreparedStatement.executeBatch(); connection.commit(); } catch (SQLException e) { logNextException("SQLException while updating daily table", e); return false; } finally { try { if (rs != null) { rs.close(); } selectPreparedStatement.close(); insertPreparedStatement.close(); updatePreparedStatement.close(); connection.close(); } catch (SQLException e) { logNextException("Exception while closing ", e); } } return true; } AuditDBHelper(ClientConfig config); String getTableName(); static Connection getConnection(ClientConfig config); static Connection getConnection(String driverName, String url,
      String username, String password); boolean update(Set<Tuple> tupleSet); String getSelectStmtForUpdation(); Set<Tuple> retrieve(Date toDate, Date fromDate, Filter filter,
      GroupBy groupBy); String getSelectStmtForRetrieve(Filter filter, GroupBy groupBy); static void logNextException(String message, SQLException e); static Date addDaysToCurrentDate(Integer dayIncrement); static Date addDaysToGivenDate(Date date, int increment); static Long getFirstMilliOfDay(Date date); static final SimpleDateFormat DAY_CHK_FORMATTER; static final SimpleDateFormat TABLE_DATE_FORMATTER; }
@Test(priority = 1) public void testUpdate() { ClientConfig config = ClientConfig.loadFromClasspath(AuditDBConstants.FEEDER_CONF_FILE); AuditDBHelper helper = new AuditDBHelper(config); String selectStmt = helper.getSelectStmtForUpdation(); PreparedStatement selectStatement = null; ResultSet rs = null; try { selectStatement = connection.prepareStatement(selectStmt); rs = getResultSetOfQuery(selectStatement, tuple1); Assert.assertNotNull(rs); Assert.assertFalse(rs.next()); Assert.assertTrue(helper.update(tupleSet1)); rs = getResultSetOfQuery(selectStatement, tuple1); Assert.assertNotNull(rs); Assert.assertTrue(rs.next()); Assert.assertEquals(tuple1.getSent(), rs.getLong(AuditDBConstants.SENT)); for (LatencyColumns latencyColumns : LatencyColumns.values()) { Long val = tuple1.getLatencyCountMap().get(latencyColumns); if (val == null) val = 0l; Assert.assertEquals(val, (Long) rs.getLong(latencyColumns.toString())); } Assert.assertEquals(tuple1.getLostCount(), (Long) rs.getLong(LatencyColumns.C600.toString())); Assert.assertTrue(helper.update(tupleSet2)); rs = getResultSetOfQuery(selectStatement, tuple1); Assert.assertNotNull(rs); Assert.assertTrue(rs.next()); Assert.assertEquals(tuple1.getSent() + tuple2.getSent(), rs.getLong(AuditDBConstants.SENT)); for (LatencyColumns latencyColumns : LatencyColumns.values()) { Long val1 = tuple1.getLatencyCountMap().get(latencyColumns); if (val1 == null) val1 = 0l; Long val2 = tuple2.getLatencyCountMap().get(latencyColumns); if (val2 == null) val2 = 0l; Assert.assertEquals(val1 + val2, rs.getLong(latencyColumns .toString())); } Assert.assertEquals(tuple1.getLostCount() + tuple2.getLostCount(), rs.getLong(LatencyColumns.C600.toString())); Assert.assertTrue(helper.update(tupleSet3)); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (selectStatement != null) { selectStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } } }