在Spring Data JPA中,当我将@NamedQueries
与lockMode = LockModeType.WRITE
一起使用时,我可以在日志中看到结果(假设jpa.show-sql
为true
):
... SELECT ... FOR UPDATE
但是,当我在方法上使用@Lock(LockModeType.WRITE)
注释时,在日志中什么也看不到。我要确保锁定机制按预期工作。
如何查看
@Lock(LockModeType.WRITE)
正常工作?应该启用什么日志级别?
编辑:这是我的代码的摘录:
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Transactional
public User checkCredentials(String email, String password, String verificationCode)
throws BadCredentialsException {
User user = userRepository.findByEmail(email);
if(user == null)
throw new BadCredentialsException("Invalid username");
...
user.setLastVerificationCode(verificationCode);
userRepository.save(user);
return user;
}
}
答案 0 :(得分:2)
您使用了错误的CREATE FUNCTION GetPath(startingID INT)
@outputVal VARCHAR(20) OUTPUT
BEGIN
DECLARE x INT;
SELECT category_id INTO x FROM table2 Where product_id = startingID;
SET outputVal = x;
sloop:LOOP
SELECT category_id INTO x FROM table1 WHERE parent_id = x;
IF x IS NOT NULL THEN
SET outputVal = outputVal + "," + x;
ITERATE sloop;
ELSE
LEAVE sloop;
END IF;
END LOOP;
RETURN outputVal;
END $$
:LockModeType
是LockModeType.WRITE
的同义词
LockModeType.OPTIMISTIC_FORCE_IMCREMENT
并且您可以从 service 方法中删除@Lock(LockModeType.PESSIMISTIC_WRITE)
,因为@Lock
仅适用于存储库方法。将@Lock
添加到相应的 service 方法也很重要。
在此处详细了解@Transactional
https://docs.oracle.com/javaee/7/api/javax/persistence/LockModeType.html