#optimistic locking / transactional spring

1 messages · Page 1 of 1 (latest)

tame violet
#

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@Entity
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String brand;
private double width;
private String typeFuel;
private double price;
@ManyToOne
@JoinColumn(name = "garage_id" , nullable = false)
private Garage garage;
@Version
private Long version;
}

@Transactional
public CarDto save(CreateCarCommand command) {
Garage garage = garageRepository.findWithLockingById(command.getGarageId())
.orElseThrow(() -> new EntityNotFoundException(MessageFormat
.format("Garage with id ={0} , not exist", command.getGarageId())));
if (garage.getNumberOfSpaces() <= garage.getCars().size()) {
throw new IllegalStateException("slot not available");
}
Car car = command.toEntity();
car.setGarage(garage);
return CarDto.fromEntity(carRepository.save(car));
}

REST API spring oobt , I have two entity Garage and Car , garage has numberOfSpaces , I need do locking beacuse if in the same time I send two request , created overbooking - numberOfSpaces 2 and size list 3 , how to impleement optimistic / pessimistic locking / transactional ? somone can help me on priv ?

flint hollowBOT
#

<@&1004656351647117403> please have a look, thanks.

flint hollowBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

private Long id;

@Version
private int version;

private String make;
private String model;
private int year;

}

@Transactional annotation is used to indicate that a method or class should be executed within a transaction. This ensures that all database operations performed within the annotated method or class are atomic and consistent.

@AllArgsConstructor annotation is used to generate a constructor with all arguments. This constructor can be used to initialize all fields of the Car class.

@NoArgsConstructor annotation is used to generate a constructor with no arguments. This constructor can be used to create an instance of the Car class with default values for all fields.

@Getter and @Setter annotations are used to generate getter and setter methods for all fields of the Car class.

@Builder annotation is used to generate a builder pattern for creating instances of the Car class. This allows for more flexible and readable object creation code.