弹簧数据悲观锁超时Postgres [英] Spring Data Pessimistic Lock timeout with Postgres
问题描述
我在Repository中试过这个:
@Lock(value = LockModeType.PESSIMISTIC_WRITE)
MyObject findByUuid(String uuid);
配置(片段)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef =docsEntityManagerFactory,
transactionManagerRef =docsTransactionManager,
@Bean(name =docsEntityManagerFactory)
public EntityManagerFactory docsEntityManagerFactory(DataSource docsDataSource){
props.setProperty(javax.persistence.lock.timeout,0);
.. 。
我甚至在服务中注入了EntityManager,并返回0:
logger.info(em.getProperties()。get(javax.persistence.lock.timeout));
但上面的内容只让我看到FOR UPDATE,NOWAIT部分没有设置,我是否设置了正确的东西(从另一篇文章看起来应该是这样) 。
这让我更接近,虽然我不想使用原始的sql
<$ $ b $($ value =LockModeType.PESSIMISTIC_WRITE)
@Query(value =SELECT * FROM the_table WHERE uuid =:uuid FOR UPDATE NOWAIT,nativeQuery = true)
MyObject findByUuid(@Param(uuid)String uuid);
这会给我一个Lock异常,但是然后Spring装饰服务(代理)会抛出一个Transaction /返回时出现回滚异常,这意味着我无法给出该方法的返回值,这是我需要的。
问题:
1)如何获取Spring数据以添加NOWAIT?
2)有没有办法返回值?我怀疑我需要自己处理事务,还是改变逻辑流程?不知道Spring是否处理NOWAIT,如果它行为不同。
我意识到有类似的问题,但是这有些不同。
我使用的是hibernate 4.2.15,spring-data-jpa 1.7.1,postgres sql 9.3。
使用PESSIMISTIC_FORCE_INCREMENT
I'm unsuccessful at getting Spring Data to send postgres a FOR UPDATE with NOWAIT.
I've tried this in the Repository:
@Lock(value = LockModeType.PESSIMISTIC_WRITE)
MyObject findByUuid(String uuid);
Configuration (snippet)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "docsEntityManagerFactory",
transactionManagerRef = "docsTransactionManager",
@Bean(name = "docsEntityManagerFactory")
public EntityManagerFactory docsEntityManagerFactory(DataSource docsDataSource) {
props.setProperty("javax.persistence.lock.timeout", "0");
...
I even inject the EntityManager in my service and this returns 0:
logger.info(em.getProperties().get("javax.persistence.lock.timeout"));
but the above only gets me the "FOR UPDATE", the NOWAIT part isn't being set. Am I setting the right thing (from another post it looks like this should work).
This gets me closer, although I'd prefer not to use raw sql
@Lock(value = LockModeType.PESSIMISTIC_WRITE)
@Query(value = "SELECT * FROM the_table WHERE uuid = :uuid FOR UPDATE NOWAIT", nativeQuery = true)
MyObject findByUuid(@Param("uuid") String uuid);
This does give me a Lock exception, but then the Spring decorated service (proxy) throws a Transaction/Rollback exception on the return, which means I can't give a return value on the method, that I need.
Questions: 1) How do I get Spring Data to add the NOWAIT? 2) Is there a way to return the value? I suspect I need to handle the transaction myself, or change the logic flow? Not sure if Spring handles the NOWAIT if it will behave differently.
I realize there is similar questions, but this is slightly different. I'm using hibernate 4.2.15, spring-data-jpa 1.7.1, postgres sql 9.3.
use PESSIMISTIC_FORCE_INCREMENT
这篇关于弹簧数据悲观锁超时Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!