I have this spring serviceImp method public DemographicResponse getDemographics(IDVBO…
- User
I have this spring serviceImp method public DemographicResponse getDemographics(IDVBO idvBo) throws Exception { Boolean isSuccess = false; logger.info("Inside IDVServiceImpl getDemographics"); IDVTransaction transaction = fetchTransactionDetails(idvBo, String.valueOf(idvBo.getTransactionBO().getAckReferenceNo()));
String sourceSountry = idvBo.getTransactionBO().getSourceCountry(); DemographicResponse main_res = new DemographicResponse(); IDVExternalPluginService service = plugInService(sourceSountry); if (config.isDemographyEnabled(sourceSountry)) { if (isCustomerDataAlreadyNotRetrived(transaction)) { try { idvBo = service.getDemographics(idvBo); isSuccess = true; } finally { persistDemographicDetails(idvBo, isSuccess); } } else { idvBo = buildIDVBOFromDB(idvBo, transaction); } IDVBOToDemographicResponse converter = new IDVBOToDemographicResponse(); main_res = converter.convert(idvBo); } logger.info("Exiting IDVServiceImpl getDemographics"); return main_res; }For reference private void persistDemographicDetails(IDVBO idvBo, Boolean isSuccess) throws DatabaseConnectionErrorException { try { EkycGoldenSourcePayload idvEkycGoldenSourcePayload = ekycGoldenSourcePayloadRepo .saveAndFlush(entityConverter.IDVBOToGoldenSourcePayload(idvBo)); idvBo.getTransactionBO().setIdvEkycGoldenSourcePayloadId(idvEkycGoldenSourcePayload.getId()); logger.info("Persisted IDVEkycGoldenSourcePayload entity"); IDVEkycGoldenSource idvEkycGoldenSourceExtracted = idvEkycGoldenSourceRepo .saveAndFlush(entityConverter.IDVBOToGoldenSourceExtracted(idvBo)); idvBo.getTransactionBO().setIdvEkycGoldenSourceId(idvEkycGoldenSourceExtracted.getId()); logger.info("Persisted IDVEkycGoldenSource entity"); CustomerInformation customerInformation = customerInformationRepo .saveAndFlush(entityConverter.IDVBOToCustomerInformation(idvBo)); idvBo.getTransactionBO().setIdvCustomerInformationId(customerInformation.getId()); logger.info("Persisted CustomerInformation entity"); if (isSuccess) { updateState(idvBo, CUSTOMER_DATA_RETRIVED.name()); } repository.save(entityConverter.IDVBOTransaction(idvBo)); logger.info("Persisted Transaction entity"); } catch (CannotCreateTransactionException e) { logger.error("Database connection error. : " + e); throw new DatabaseConnectionErrorException(idvBo.getTransactionBO().getTransactionReference(), ""); } }
When calling the getDemographics API (for a failed scenario where the service.getDemographics failes due to a vendor call - example {"error_details":[{"id":"TXNREF10011735799818","code":"IDV200_TP_INVALID_ARGUMENT","detail":"Downstream System Error","source":"Zoloz : Bad Response. The invalid field attributes [ISSUE_DATE]","action":"ReSubmit"}]})
I have added an idempotency feature in the getDemographics method where when recalling the same API with same request body I should always get the same response even if it is a failure response.
But for this condition I get this as a response when calling the API for the second time after a failure with the same request body {"error_details":[{"id":"","code":"IDV999","detail":"Unexpected error occurred","source":"could not execute statement [ERROR: duplicate key value violates unique constraint "idv_ekyc_golden_source_payload_ack_index"\n Detail: Key (ack_reference_no)=(IDV-HK-01-20250102120659-551b067d-f54d-4cef-b) already exists.] [insert into idv_ekyc_golden_source_payload (ack_reference_no,channel_reference,created_date,fingerprint_match_golden_source_data,image_match_golden_souce_data,poa_golden_source_data,poi_golden_source_data,txn_reference_no,idv_ekyc_golden_source_payload_id) values (?,?,?,?,?,?,?,?,?)]; SQL [insert into idv_ekyc_golden_source_payload (ack_reference_no,channel_reference,created_date,fingerprint_match_golden_source_data,image_match_golden_souce_data,poa_golden_source_data,poi_golden_source_data,txn_reference_no,idv_ekyc_golden_source_payload_id) values (?,?,?,?,?,?,?,?,?)]; constraint [idv_ekyc_golden_source_payload_ack_index]","action":"ReSubmit"}]}
How to fix it?
- Assistant
The error you're encountering arises from a failure to ensure idempotency in your
getDemographicsmethod. Specifically, the system attempts to re-insert records into the database, causing a unique constraint violation due to existing records associated with the sameack_reference_no. Thus, when the same request is made again, the system should return a consistent response without performing the database insertion again.To implement idempotency, here are some changes and strategies you can apply:
1. Check for Existing Records Before Insertion
Before inserting any new records into the database, you should check if the records already exist based on the unique fields you have (for example,
ack_reference_no). If an existing record is found, return the information associated with that record instead of attempting to insert a new record.Here’s an outline of how you can achieve this:
public DemographicResponse getDemographics(IDVBO idvBo) throws Exception { logger.info("Inside IDVServiceImpl getDemographics"); IDVTransaction transaction = fetchTransactionDetails(idvBo, String.valueOf(idvBo.getTransactionBO().getAckReferenceNo())); String sourceCountry = idvBo.getTransactionBO().getSourceCountry(); DemographicResponse main_res = new DemographicResponse(); IDVExternalPluginService service = plugInService(sourceCountry); if (config.isDemographyEnabled(sourceCountry)) { // Check if we already have demographic data in the DB IDVBO existingData = findExistingDemographicData(idvBo); if (existingData != null) { // If data exists, convert it to response and return IDVBOToDemographicResponse converter = new IDVBOToDemographicResponse(); main_res = converter.convert(existingData); return main_res; } if (isCustomerDataAlreadyNotRetrived(transaction)) { try { idvBo = service.getDemographics(idvBo); persistDemographicDetails(idvBo, true); } catch (Exception e) { // In case of an error during the call, handle accordingly logger.error("Error retrieving demographics: ", e); // Return a response indicating the retrieval failed, without modifying the DB return createErrorResponse(e, idvBo.getTransactionBO().getTransactionReference()); } } else { idvBo = buildIDVBOFromDB(idvBo, transaction); } IDVBOToDemographicResponse converter = new IDVBOToDemographicResponse(); main_res = converter.convert(idvBo); } logger.info("Exiting IDVServiceImpl getDemographics"); return main_res; } // Method to find existing demographic data private IDVBO findExistingDemographicData(IDVBO idvBo) { // Replace with your actual logic to fetch existing data return repository.findByAckReferenceNo(idvBo.getTransactionBO().getAckReferenceNo()); } // Function to create an error response private DemographicResponse createErrorResponse(Exception e, String transactionReference) { // Create and return a structured error response return new DemographicResponse(/* Populate fields with error info */); }2. Persistent Record Handling
If the retrieval fails at the vendor service level and you want to avoid re-inserting records, ensure that the
persistDemographicDetailsmethod is only called when the operation is truly successful. If a failure occurs, the method should not be called, and instead, the already existing data should be used wherever possible.3. Idempotency Key
Consider using an idempotency key (like a unique transaction ID) with your requests. When a client makes a request with the same key, the server checks if that key has already been processed. If it has, the server returns the cached response associated with that key instead of processing the request anew.
4. Log and Handle Different Scenarios
In case the service failure results in a non-retriable state, carefully log the errors and provide appropriate responses to the client. Consistent error response structures help clients handle errors gracefully.
Following these steps will help ensure your
getDemographicsmethod respects idempotency and prevents data integrity issues in your database.