Я добавил @transactional (readonly = true) аннотацию в метод запроса только для чтения, как ниже. Тем не менее, соединение не возвращается и создается непрерывно в состоянии сна. < /P>
Код: Выделить всё
@Transactional(readOnly = true)
public Page getAllActiveBarcodes(Pageable pageable, Boolean isAction, String keyword, String searchType, String type) throws IllegalArgumentException {
return barCodeRepository.searchBarcodes(pageable, isAction, keyword, searchType, type);
}
@Transactional(readOnly = true)
public List barCodeSearch(String keyword) throws IllegalArgumentException {
return barCodeRepository.searchKeyword(keyword);
}
@Transactional(readOnly = true)
public List barCodeList(){
return barCodeRepository.findAll();
}< /code>
< /div>
< /div>
`< /p>
"@Override
public List searchKeyword(String keyword) {
BooleanBuilder builder = new BooleanBuilder();
builder.and(barCode.isActive.isTrue());
builder.and(barCode.deleteDate.isNull());
if (keyword != null && !keyword.trim().isEmpty()) {
BooleanBuilder keywordBuilder = new BooleanBuilder();
keywordBuilder.or(barCode.barcode.containsIgnoreCase(keyword));
keywordBuilder.or(barCode.type.containsIgnoreCase(keyword));
keywordBuilder.or(barCode.message.containsIgnoreCase(keyword));
keywordBuilder.or(barCode.descText.containsIgnoreCase(keyword));
keywordBuilder.or(barCode.createUser.containsIgnoreCase(keyword));
builder.and(keywordBuilder);
}
return queryFactory
.selectFrom(barCode)
.where(builder)
.orderBy(barCode.createDate.desc())
.fetch();
} @Override
public Page searchBarcodes(Pageable pageable, Boolean isAction, String keyword, String searchType, String type) {
BooleanBuilder builder = new BooleanBuilder();
if (isAction != null) {
builder.and(barCode.isActive.eq(isAction));
}
if(type != null && !type.trim().isEmpty()) {
builder.and(barCode.type.eq(type));
}
if (keyword != null && !keyword.trim().isEmpty()) {
switch (searchType.toLowerCase()) {
case "barcode" -> builder.and(barCode.barcode.containsIgnoreCase(keyword));
case "message" -> builder.and(barCode.message.containsIgnoreCase(keyword));
case "desc_text" -> builder.and(barCode.descText.containsIgnoreCase(keyword));
case "create_user" -> builder.and(barCode.createUser.containsIgnoreCase(keyword));
default -> throw new IllegalArgumentException("잘못된 검색 유형입니다.");
}
}
long total = Optional.ofNullable(
queryFactory
.select(barCode.count())
.from(barCode)
.where(builder)
.fetchOne()).orElse(0L);
if (total == 0) {
return Page.empty(pageable);
}
List result = queryFactory
.selectFrom(barCode)
.where(builder)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(barCode.createDate.desc())
.fetch();
return new PageImpl(result, pageable, total);
}< /code>
< /div>
< /div>
< /p>
@GetMapping("/barcodes")
public ResponseEntity getAllBarcodes(@PageableDefault(page = 0, size = 32) Pageable pageable,
@RequestParam(value = "isAction", required = false) Boolean isAction,
@RequestParam("type") String type,
@RequestParam("searchType") String searchType,
@RequestParam("keyword") String keyword) {
try {
Page barcodeList = barcodeService.getAllActiveBarcodes(pageable, isAction, keyword, searchType, type);
if(barcodeList.isEmpty()){
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(barcodeList);
} catch (IllegalStateException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@GetMapping("/barcodes/all")
public ResponseEntity getBarcode() throws IllegalArgumentException {
try {
List barCodeList = barcodeService.barCodeList();
return ResponseEntity.ok(barCodeList);
}catch (IllegalStateException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}catch (Exception e){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@GetMapping("/barcodes/active")
public ResponseEntity getActiveBarcode(@RequestParam(required = false) String keyword) throws IllegalArgumentException {
try{
List search = barcodeService.barCodeSearch(keyword);
return ResponseEntity.ok(search);
}catch (IllegalStateException e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}catch (Exception e){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}Подробнее здесь: https://stackoverflow.com/questions/795 ... th-aws-rds