0

I am working on a monitoring tool developed in Spring Boot using Hibernate as ORM.

I need to compare each row (already persisted rows of sent messages) in my table and see if a MailId (unique) has received a feedback (status: OPENED, BOUNCED, DELIVERED...) Yes or Not.

I get the feedbacks by reading csv files from a network folder. The CSV parsing and reading of files goes very fast, but the update of my database is very slow. My algorithm is not very efficient because I loop trough a list that can have hundred thousands of objects and look in my table.

This is the method that make the update in my table by updating the "target" Object (row in table database)

@Override
public void updateTargetObjectFoo() throws CSVProcessingException, FileNotFoundException {             
    // Here I make a call to performProcessing method which reads files on a folder and parse them to JavaObjects and I map them in a feedBackList of type Foo
    List<Foo> feedBackList = performProcessing(env.getProperty("foo_in"), EXPECTED_HEADER_FIELDS_STATUS, Foo.class, ".LETTERS.STATUS.");
    for (Foo foo: feedBackList) {                        
        //findByKey does a simple Select in mySql where MailId = foo.getMailId()
        Foo persistedFoo = fooDao.findByKey(foo.getMailId());
        if (persistedFoo != null) {
            persistedFoo.setStatus(foo.getStatus());
            persistedFoo.setDnsCode(foo.getDnsCode());             
            persistedFoo.setReturnDate(foo.getReturnDate());
            persistedFoo.setReturnTime(foo.getReturnTime());   
            //The save account here does an MySql UPDATE on the table
            fooDao.saveAccount(foo);
        }
    }
}

What if I achieve this selection/comparison and update action in Java side? Then re-update the whole list in database?

Will it be faster?

Thanks to all for your help.

1
  • The “fast way”, I have seen in real life projects so far, is to bypass the ORM and send SQL. In fact, I’ve seen projects where every database operation was actually done bypassing the ORM… Commented May 2, 2017 at 10:48

1 Answer 1

2

Hibernate is not particularly well-suited for batch processing. You may be better off using Spring's JdbcTemplate to do jdbc batch processing.

However, if you must do this via Hibernate, this may help: https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/chapters/batch/Batching.html

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, Thanks a lot for your reply and guidance. I am not restricted to use Hibernate in this project. I have to find a solution to make the operations way faster than it is now. I searched about Spring JdbcTemplate. Have you an idea on how could I implement this wisely? By knowing that I must perform a main operation(10-100 thousands times): Update each row of my database table based on a list of java Object
There's​ lots of documentation and examples on the web. Here's one: google.com/url?sa=t&source=web&rct=j&url=https://…
Hi again, Thanks for your answer. I am redeveloping a bit my DAO to implement JDCB template. I will keep you up to date about any change of performance. Have a nice day
Thanks for your guidance. Using Spring's JDBC Template has fasten the operations times 4!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.