大佬教程收集整理的这篇文章主要介绍了使用Spring Data JPA查找实体时如何启用LockModeType.PESSIMISTIC_WRITE?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@Lock
从Spring Data JPA 1.6版开始,CRUD方法就支持该功能(实际上,已经有了一个里程碑)。有关更多详细信息。
使用该版本,你只需声明以下内容:
interface WidgetRepository extends Repository<Widget, Long> {
@Lock(LockmodeType.@R_559_3270@_WRITE)
Widget findOne(Long ID);
}
这将导致后备存储库代理的CRUD实现部分将已配置的应用LockmodeType
到上的find(…)
调用Entitymanager
。
我该如何实现与以下代码等效的功能:
tx.begin();
Widget w = em.find(Widget.class,1L,LockmodeType.PESSIMISTIC_WRITE);
w.decrementBy(4);
em.flush();
tx.commit();
&Hellip;但是使用Spring和Spring-Data-JPA批注?
我现有代码的基础是:
@service
@transactional(readOnly = truE)
public class WidgetserviceImpl implements Widgetservice
{
/** The spring-data widget repository which extends CrudRepository<Widget,Long>. */
@Autowired
private WidgetRepository repo;
@transactional(readOnly = falsE)
public void updateWidgetStock(Long id,int count)
{
Widget w = this.repo.findOne(id);
w.decrementBy(4);
this.repo.save(w);
}
}
但是我不知道如何指定该updateWidgetStock
方法中的所有操作都应使用悲观锁集。
有一个Spring Data JPA批注org.springframework.data.jpa.repository.Lock
,允许你设置LockmodeType
,但我不知道将其放在updateWidgetStock
方法上是否有效。听起来更像是的注解WidgetRepository
,因为Javadoc说:
org.springframework.data.jpa.repository
@Target(value=METHOD)
@Retention(value=RUNTIME)
@Documented
public @interface Lock
Annotation used to specify the LockmodeType to be used when execuTing the query. It will be evaluated when using Query on a query method or if you derive the query from the method name.
&Hellip;因此似乎无济于事。
如何使我的updateWidgetStock()
方法通过LockmodeType.PESSIMISTIC_WRITEset
执行?
以上是大佬教程为你收集整理的使用Spring Data JPA查找实体时如何启用LockModeType.PESSIMISTIC_WRITE?全部内容,希望文章能够帮你解决使用Spring Data JPA查找实体时如何启用LockModeType.PESSIMISTIC_WRITE?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。