select Reference,
[Required] = substring(Reference, cs, ce - cs)
+ '-'
+ substring(Reference, ps, pe - ps)
from tbl
cross apply -- find start position
(
select cs = charindex('course=', Reference) + len('course=')
) cs
cross apply -- find end position
(
select ce = charindex('&', Reference, cs)
) ce
cross apply
(
select ps = charindex('plan=', Reference) + len('plan=')
) ps
cross apply
(
select pe = charindex('&', Reference, ps)
) pe
///在服务类中我有一个方法saveCity,正如您所看到的,我将@transactional放在上面,但是它不起作用,数据库不会在Exception上回滚。 // saveToDb是另一个服务(在下面)中的方法,该服务基于Jpa存储库
@Service
public class SingleCityService implements ICityService {
private static final Logger logger = LoggerFactory.getLogger(SingleCityService.class);
@Autowired
CityMmtRepo cityMmtRepo;
@Autowired
MmtCityService mmtCityService;
@Autowired
RestCityService restCityService;
@PostConstruct
public void init() {
CityServiceFactory.getInstance().registor(EnumCity.SINGLE_CITY, this);
}
/**
* Method to validate if action type is to Create city or Update city
*
* @param request
* @return CityServiceResponse
*/
@Override
public CityServiceResponse serveRequest(CityServiceRequest request) throws ResourceException, InvalidCityCreateException, SQLException {
ValidatorFactory.getInstance().getRequestValidator(Validator.SINGLE_CITY_VALIDATOR).validate(request);
if (CityActionType.CREATE.toString().equals(request.getEventType().getActionType())) {
return createCity(request);
}
return updateCity(request);
}
/**
* Method to create city and fetch matched cities from cache.
*
* @param request
* @return CityServiceResponse
* @throws ResourceException
*/
private CityServiceResponse createCity(CityServiceRequest request) throws ResourceException {
logger.info("Inside SingleCityService: Starting createCity for request: {}", request.toString());
/**
* forceUpdate signifies if user still want to create city after getting conflicts.
*/
if (ResourceConstants.TRUE.equalsIgnoreCase(request.getCityDetails().get(0).getForceUpdate())) {
return saveCity(request, ResourceConstants.CREATED);
}
CityCacheServiceResponse cityCacheServiceResponse = restCityService.getCityMatchingFromCityCache(request);
CityServiceResponse cityServiceResponse = new CityServiceResponse();
if (ResourceConstants.FAILURE.equalsIgnoreCase(cityCacheServiceResponse.getStatus())) {
cityServiceResponse.setStatus(ResourceConstants.FAILURE);
cityServiceResponse.setDescription(cityCacheServiceResponse.getDescription());
} else {
if (cityCacheServiceResponse.getCity().get(0).getMatchedCity().isEmpty()) {
return saveCity(request, ResourceConstants.SUCCESS);
} else {
cityServiceResponse.setStatus(ResourceConstants.CONFLICTING_CITIES_UI_DISPLAY_MSG);
cityServiceResponse.setCity(CityServiceMapperUtil.cacheResponse(cityCacheServiceResponse));
}
}
return cityServiceResponse;
}
/**
* To update city.
*
* @param request
* @return CityServiceResponse
* @throws ResourceException
*/
private CityServiceResponse updateCity(CityServiceRequest request) throws ResourceException, InvalidCityCreateException {
logger.info("Inside SingleCityService, Starting updateCity for request: {}", request.toString());
CityMmtEntity cityMmtEntity = mmtCityService.fetchCityToEdit(request.getCityDetails().get(0).getCityCd());
request = CityServiceMapperUtil.mapCityToBeUpdated(cityMmtEntity, request);
return saveCity(request, ResourceConstants.UPDATE);
}
/**
* To create or update city in DB and Push city in Cache.
*
* @param request
* @param status
* @return CityServiceResponse
* @throws ResourceException
*/
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public CityServiceResponse saveCity(CityServiceRequest request, String status) throws ResourceException {
CityMmtEntity cityMmtEntity = CityServiceMapperUtil.getCityEntity(request);
logger.info("Inside SingleCityService: saveCity: Saving city data in DB for cityCode: {}", cityMmtEntity.getChtCitycd());
cityMmtEntity = mmtCityService.SaveToDb(cityMmtEntity);
int i = 1/0;
int k = i;
logger.info("Inside SingleCityService: saveCity: Syncing city to ES API for cityCode: {}", cityMmtEntity.getChtCitycd());
request.getCityDetails().get(0).setCityCd(cityMmtEntity.getChtCitycd());
request.getCityDetails().get(0).setLastUpdatedOn(cityMmtEntity.getChtCityLastUpdDt());
SyncCityCacheRequest syncCityCacheRequest = CityServiceMapperUtil.mapCityCacheRequest(request);
SyncCityCacheResponse syncCityCacheResponse = restCityService.syncCityToCityCache(syncCityCacheRequest);
CityServiceResponse cityServiceResponse = new CityServiceResponse();
cityServiceResponse.setStatus(status);
cityServiceResponse.setDescription(syncCityCacheResponse.getSyncCityDetails().get(0).getDescription());
return cityServiceResponse;
}
}
///我将k = 1/0抛出运行时异常,仅次于将实体保存到db的步骤,只是检查@Transactional是否正常工作,但在异常情况下实体不会从数据库回滚。 //实际上,我从另一个服务(其方法是SyncCityToCache)抛出异常是:-
@Service
public class MmtCityServiceImpl implements MmtCityService {
private static final Logger logger = LoggerFactory.getLogger(MmtCityServiceImpl.class);
@Autowired
private CityMmtRepo cityMmtRepo;
/**
* To save city in DB
*
* @param cityMmtEntity
* @return {@link CityMmtEntity}
*/
public CityMmtEntity SaveToDb(CityMmtEntity cityMmtEntity) throws ResourceException {
try {
logger.info("Inside MmtCityServiceImpl: Starting SaveToDb to save city for CityMmtEntity: {}", cityMmtEntity.toString());
return cityMmtRepo.save(cityMmtEntity);
} catch (Exception e) {
logger.error("MmtCityServiceImpl : Exception while saving/updating city in database, Exception: {}", e);
throw new ResourceException(ResourceErrors.SAVE_CITY_ERROR.getErrorCode(), ResourceErrors.SAVE_CITY_ERROR.getErrorDescription());
}
}
答案 0 :(得分:1)
Spring使用AOP将事务性应用于用@Transactional
注释的方法。 Spring by default使用代理将行为添加到现有类中。通常这不是问题,但是代理的问题之一是,只有外部方法调用通过代理,对象内部的方法调用没有通过代理。 (另请参见understanding AOP proxies)。
现在,当您从同一对象内部调用@Transactional
方法时,它将不会通过代理,因此将忽略@Transactional
。要修复您的顶级方法,应使用@Transactional
(serveRequest
方法)。
该方法也是您要全部完成或全部失败的工作单元。因此,简单的解决方案是为您的serveRequest
方法添加注释。