我怎樣才能做到這一點代碼相當於:如何在使用Spring Data JPA查找實體時啓用LockModeType.PESSIMISTIC_WRITE?
tx.begin();
Widget w = em.find(Widget.class, 1L, LockModeType.PESSIMISTIC_WRITE);
w.decrementBy(4);
em.flush();
tx.commit();
...但使用Spring和Spring數據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數據JPA註釋org.springframework.data.jpa.repository.Lock
,它允許您設置LockModeType
,但我不知道這是否是有效的把它放在updateWidgetStock
方法。這聽起來更像是WidgetRepository
註解,因爲Javadoc中說:
org.springframework.data.jpa.repository
@Target(值=法)
@Retention(值= RUNTIME)
@Documented
public @interface Lock
註釋用於指定執行查詢時要使用的LockModeType。當在查詢方法上使用查詢時,或者如果從方法名稱派生查詢時,它將被評估。
...這似乎沒有幫助。
如何讓我的updateWidgetStock()
方法執行LockModeType.PESSIMISTIC_WRITE
集?
無論這個問題的答案可能會有所幫助:http://stackoverflow.com/questions/11880924/how-to-add-custom-method -to-spring-data-jpa – 2013-04-23 03:42:27