Spring boot @服务中的事务性
我已经创建了一个投票应用程序,我有一个改变投票数的方法。它实现了一个带有@Transactional注释的接口 谢谢 VotingService是一个接口。 实现类 在spring中,默认情况下VotingServiceImplic是单例类。它是 线程之间共享。 它不应该具有的实例变量 持有投票信息。Spring boot @服务中的事务性,spring-boot,service,transactional,Spring Boot,Service,Transactional,我已经创建了一个投票应用程序,我有一个改变投票数的方法。它实现了一个带有@Transactional注释的接口 谢谢 VotingService是一个接口。 实现类 在spring中,默认情况下VotingServiceImplic是单例类。它是 线程之间共享。 它不应该具有的实例变量 持有投票信息。 您可以通过使用postman或jmeter执行并行请求来验证服务的正确性。谢谢您的帮助。我试过jmeter。共测试了23个用户。它本该起作用的。尽管建议在实现中使用事务。 @Transaction
您可以通过使用postman或jmeter执行并行请求来验证服务的正确性。谢谢您的帮助。我试过jmeter。共测试了23个用户。它本该起作用的。尽管建议在实现中使用事务。
@Transactional(readOnly = true)
public interface VotingService {
Vote getByRestaurantId(int restaurantId);
Vote get(int id);
List<Vote> getWithRestaurantsByDate(LocalDateTime date);
List<Vote> getWithRestaurantsToday(HttpServletResponse response, int id);
@Transactional
Vote voteFor(int restaurantId, int userId);
}
@Service
public class VotingServiceImpl implements VotingService {
...
@Override
public Vote voteFor(int restaurantId, int userId) {
...
Vote vote = getByRestaurantId(restaurantId);
vote.setNumberOfVotes(vote.getNumberOfVotes() + 1)
...
return vote;
...
}
...
}
@Entity
@Table(name = "votes", uniqueConstraints = {@UniqueConstraint(columnNames = {"restaurant_id", "date", "votes"}, name = "votes_unique_restaurant_date_votes_idx")})
public class Vote extends AbstractEntity {
@Column(name = "votes")
private int numberOfVotes;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "restaurant_id", nullable = false)
@NotNull
private Restaurant restaurant;
public int getNumberOfVotes() {
return numberOfVotes;
}
public void setNumberOfVotes(int numberOfVotes) {
this.numberOfVotes = numberOfVotes;
}
public Vote() {
}
public Restaurant getRestaurant() {
return restaurant;
}
public void setRestaurant(Restaurant restaurant) {
this.restaurant = restaurant;
}
@Override
public String toString() {
return "Vote{" +
super.toString() +
"numberOfVotes=" + numberOfVotes +
", restaurant=" + restaurant +
'}';
}
}