Skip to content

Commit

Permalink
ChennuruAkhil#8 Refactored code with Query… (#1148)
Browse files Browse the repository at this point in the history
* ChennuruAkhil#8 Refactored code with Query Hints

* ChennuruAkhil#8 Fixed Sonar issues

* ChennuruAkhil#8 Refactored code as per the comments
  • Loading branch information
ChennuruAkhil authored Mar 18, 2024
1 parent 3427916 commit dddf633
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
package com.example.locks.repositories;

import com.example.locks.entities.Actor;
import org.springframework.data.jpa.repository.JpaRepository;
import jakarta.persistence.LockModeType;
import jakarta.persistence.QueryHint;
import java.util.Optional;
import org.springframework.data.jpa.repository.*;
import org.springframework.data.repository.query.Param;

public interface ActorRepository extends JpaRepository<Actor, Long>, CustomizedActorRepository {}
public interface ActorRepository extends JpaRepository<Actor, Long> {

@Query(value = "SELECT actor FROM Actor actor WHERE actor.actorId = :actorId")
@QueryHints(
value = {
@QueryHint(name = "jakarta.persistence.lock.timeout", value = "5000"),
@QueryHint(name = "jakarta.persistence.query.timeout", value = "3000"),
})
@Lock(LockModeType.PESSIMISTIC_WRITE)
Optional<Actor> findByIdWithPessimisticWriteLock(@Param("actorId") Long actorId);

@Query(value = "SELECT actor FROM Actor actor WHERE actor.actorId = :actorId")
@QueryHints(
value = {
@QueryHint(name = "jakarta.persistence.lock.timeout", value = "5000"),
@QueryHint(name = "jakarta.persistence.query.timeout", value = "3000"),
})
@Lock(LockModeType.PESSIMISTIC_READ)
Optional<Actor> findByIdWithPessimisticReadLock(@Param("actorId") Long actorId);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.example.locks.model.response.ActorResponse;
import com.example.locks.model.response.PagedResult;
import com.example.locks.repositories.ActorRepository;
import jakarta.persistence.LockModeType;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -94,21 +93,25 @@ public Actor updateActorWithLock(Long id, String name) {
}

public Actor obtainPessimisticLockAndUpdate(Long id, String name) {
Actor actor = actorRepository.getActorAndObtainPessimisticLockingOnItById(id, LockModeType.PESSIMISTIC_WRITE);
actor.setActorName(name);
return actorRepository.save(actor);
Optional<Actor> actor = actorRepository.findByIdWithPessimisticWriteLock(id);
actor.ifPresent(obj -> {
sleepForAWhile();
obj.setActorName(name);
actorRepository.save(obj);
});
return actor.orElseThrow(() -> new ActorNotFoundException(id));
}

private void sleepForAWhile() {
try {
TimeUnit.MILLISECONDS.sleep(2000);
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}

@Transactional
public Actor getActorWithPessimisticReadLock(Long id) {
return actorRepository.getActorAndObtainPessimisticLockingOnItById(id, LockModeType.PESSIMISTIC_READ);
return actorRepository.findByIdWithPessimisticReadLock(id).orElseThrow(() -> new ActorNotFoundException(id));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.locks.utils;

import lombok.experimental.UtilityClass;

@UtilityClass
public final class AppConstants {
public static final String PROFILE_PROD = "prod";
public static final String PROFILE_NOT_PROD = "!" + PROFILE_PROD;
Expand Down

0 comments on commit dddf633

Please sign in to comment.