代码之家  ›  专栏  ›  技术社区  ›  Lulex97

LockModeType。悲观\u FORCE\u INCREMENT throws“无法提取结果集”

  •  0
  • Lulex97  · 技术社区  · 2 年前

    在我的服务中,当我想通过方法从数据库中获取对象时:

    public ParkingEntity findParking(String idParking) {
        return parkingRepository.findById(idParking).orElseThrow((() ->
                new ParkingNotFoundException(idParking)));
    }
    

    存储库中的“findById”:

    public interface ParkingRepository extends JpaRepository<ParkingEntity, String> {
        @Lock(LockModeType.PESSIMISTIC_FORCE_INCREMENT)
        Optional<ParkingEntity> findById(String s);
    }
    

    我正在 org.hibernate.exception.GenericJDBCException: could not extract ResultSet Caused by: java.sql.SQLException: Cannot execute statement in a READ ONLY transaction.

    只有当我添加 @Lock(LockModeType.PESSIMISTIC_FORCE_INCREMENT) .无@锁定,无任何错误。我的parkingEntity车身:

    @Entity
    @Table(name = "parking")
    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    public class ParkingEntity {
        @Id
        @Column(name = "parking_id")
        private String idParking;
        private String name;
        private String address;
        private int numberOfParkingSlots;
        private int numberOfChargers;
        private boolean isLPGAllowed;
        private Double widthOfSlot;
        @OneToMany(mappedBy = "parking", cascade = CascadeType.ALL)
        private Set<CarEntity> cars = new HashSet<>();
        @Version
        private Long version;
    
    0 回复  |  直到 2 年前