on 2022 Oct 18 1:54 PM
How can I manage Transaction in Hybris to update millions of records in database?
Request clarification before answering.
<!-- This will copy the value of mediaCode (column) and paste it in newMediaCode (column) in database -->
<!-- This api is using the Transaction API to get best performance to migrate the millions of records from one column to another column through groovy script-->
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()) // For LOG
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.")
println("Script execution started at : " + LocalDateTime.now()) // For HAC LOG
LOGGER.info("Script execution finished at : " + LocalDateTime.now()) // For LOG
return totalRecords + " total media records saved."
Best | Ashish Vyas
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
We can use the Transaction API of Hybris to perform the Transaction Management in SAP Commerce/Hybris. Below is for quick reference.
SAP Commerce Transaction API
Best | Ashish Vyas
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
19 | |
17 | |
3 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.