cancel
Showing results for 
Search instead for 
Did you mean: 

Transaction Management in Hybris with Example

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos
<!-- 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

Answers (2)

Answers (2)

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

0 Kudos

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