cancel
Showing results for 
Search instead for 
Did you mean: 

Transaction Management in Hybris with Example

How can I manage Transaction in Hybris to update millions of records in database?

View Entire Topic
ashish_bosch_com
Discoverer
0 Kudos

import de.hybris.platform.core.model.media.MediaModel
import de.hybris.platform.servicelayer.model.ModelService
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery
import de.hybris.platform.servicelayer.search.FlexibleSearchService
import de.hybris.platform.servicelayer.search.SearchResult
import de.hybris.platform.tx.Transaction
import org.apache.log4j.Logger
import java.sql.Connection
import java.time.LocalDateTime
int start = 0;
final int range = 1000;
int total = 0;
int initialRetry = 0
final int maxRetry = 5;
int totalRecords = 0
final FlexibleSearchService flexibleSearchService = spring.getBean("flexibleSearchService")
final ModelService modelService = spring.getBean("modelService")
final Logger LOGGER = Logger.getLogger(this.class)
final String query = "SELECT {PK} FROM {Media} where {newMediaCode} IS NULL"
final FlexibleSearchQuery fQuery = new FlexibleSearchQuery(query)
fQuery.setCount(range);
fQuery.setNeedTotal(true);
println("Script execution started at : " + LocalDateTime.now()) // For HAC LOG
LOGGER.info("Script execution finished at : " + LocalDateTime.now())

Transaction tx = Transaction.current()
tx.setTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED);
do {
fQuery.setStart(start);
final SearchResult<MediaModel> searchResult = flexibleSearchService.search(fQuery)
total = searchResult.getTotalCount();
// It may be returned millions of records
start += range;
boolean success = false
try {
for (MediaModel model : searchResult.getResult())
{
String code = model.getCode()
model.setNewMediaCode(code)
modelService.save(model)
modelService.refresh(model)
totalRecords++
}
success = true
}
catch(Exception e)
{
LOGGER.warn(e.getCause())
}
finally {
if (success) {
tx.commit()
tx.begin()
LOGGER.info(totalRecords + " total media records have been updated and committed in database: " + LocalDateTime.now())
}
else
{
LOGGER.warn("Error occurred while saving the media model and retrying to run the script again.. : " + LocalDateTime.now())
initialRetry++
tx.rollback()
tx.begin()
}
}
}while ((start < total) && (initialRetry <= maxRetry))
LOGGER.info(totalRecords + " total media records have been updated and committed in database.")

// For HAC LOG
println("Script execution started at : " + LocalDateTime.now())

// For LOG
LOGGER.info("Script execution finished at : " + LocalDateTime.now())

return totalRecords + " total media records saved."

// Best | Ashish Vyas