Thanks @Sve Kamenska and @M.Denium. However this does not work for me.
I tried the following.
@Transactional(transactionManager = "standardizedDataSourceTransactionManager", propagation = Propagation.REQUIRES_NEW)
public void callMe() {
try {
boolean retVal = flattenProcessingInfoService.processSave1(1001, 2001, "MyHost");
}
catch (Exception e) {
e.printStackTrace();
}
}
And called CallMe() method from another one multiple times. I am not reaching e.printStackTrace at all.
This is the case when I have repository.save()
But when I have native query..
@Modifying
@Transactional
@Query(value = "INSERT INTO pega_data.T_FLATTEN_PROCESSING_INFO (host_id, begin_version, end_version, processing_dt_tm) VALUES " +
"(:hostName, :beginVersion, :endVersion, :processingDateTime)", nativeQuery = true)
void insertBatch(String hostName, long beginVersion, long endVersion, LocalDateTime processingDateTime);
it throws Violation of PRIMARY KEY constraint 'PK__T_FLATTE__AFB35C3BAD604B4D'. Cannot insert duplicate key in object 'DBO.T_FLATTEN_PROCESSING_INFO'. The duplicate key value is (1001, 2001). This works for me in one sense. but my entire transaction is rolled back which I dont want. I need to capture exception, make some decision based on PK violation and proceed further.
org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only.
I am not sure where it is marked as Rollback-only