cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Save to the db asynchronously using CompletableFuture

Former Member
0 Likes
1,395

I want to make saving to the db to be asynchronous using the example method below.

 void save(final List<CustomerModel> customers,
       final Map<String, List<CustomerProductsModel>> customerProducts) {
     CompletableFuture.runAsync(new Runnable() {
       @Override
       public void run() {
         // Save Customer + Products Aysnc
         for(final CustomerModel customer : customers) {
           for(final CustomerProductsModel product : customerProducts.get(customer.getUUID())) {
             product.setEndDate(changeDateFormat(products.getEndDate()));
             product.setStartDate(changeDateFormat(products.getStartDate()));
             modelService.save(product);
           }
           modelService.save(customer);
         }
       }
     });
   }



However, nothing is saving at all.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Likes

Never mind I figured it out. Need specify an executor, pass it into the CompletableFuture and set the modelService from the bean inside the run() method

Former Member
0 Likes

Forgot this...

final ModelService modelService = (ModelService) Registry.getApplicationContext().getBean("defaultModelService");

Marko_salonen
Contributor
0 Likes

Could it be that you are not running it in a transaction? See if this could be of help . https://help.hybris.com/6.7.0/hcd/8c7387f186691014922080f2e053216a.html