I am currently working on a program that is using Spring and Hibernate to retrieve data every 31 seconds (Spring scheduled service) and save to mysql tables (Hibernate).
I currently have a HashMap that is checking the data retrieved. If the data retrieved is in the HashMap, use that instance. If the data is not in the HashMap, add it to the HashMap and save this new entry through Hibernate.
My only concern with this design is that since this is a Spring schedule service that runs every 31 seconds, the HashMap object also get recreated every 31 seconds In that case, any entries previously added to the HashMap will not be on there anymore, and duplicated entries will be saved to the table.
I have came up with two ways to solve the issue:
Query for all the current entries on the table and use them to populate the HashMap, before the HashMap is used to check the new data retrieved. This way, the HashMap even after being recreated every 31 seconds, will still have all the unique entries.
Query the new data that just came in against all the entries on the table. This way, a HashMap is not needed. Whenever a new data comes in, just check it against all the entries through query.
Which of the two ways is more optimized as the number of entries on the table gets larger and larger? Please also feel free to suggest any alternative ways of doing this. Thanks.
My code:
@Scheduled(fixedRate = 31000)
public void saveCompanyData() {
// Retrieve data from online into Data
...
...
// Hibernate
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Company.class)
.buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
// Create HashMap
HashMap<String, Company> companyMap = new HashMap<>();
// Save to table if key not mapped
String companyKey = Data.getCompanyName();
Company company;
if(companyMap.containsKey(companyKey)){
company = companyMap.get(companyKey);
} else {
company = new company(Data.getCompanyName());
companyMap.put(companyKey, company);
session.save(company);
}
// Commit
session.getTransaction().commit();