簡體   English   中英

以編程方式設置鎖定超時Spring Boot JPA

[英]Set lock timeout programmatically Spring Boot JPA

我有

@Transactional(timeout = 600)
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @Override
    public void actOnCustomer(Long customerId) {
    ...

隨着

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    Optional<Customer> findById(Long id);

客戶行似乎已被鎖定,並且超時值似乎具有適當的作用。

現在,我希望能夠以編程方式和/或使用application.properties文件來設置超時值 我已經看到了一些在傳遞給EntityManagerfind方法的屬性中設置javax.persistence.lock.timeout示例,但是我不確定如何最好地將EntityManager調用合並到Spring存儲庫中,似乎應該采取一種更加Spring-y的方式(例如,在application.properties中設置spring.jpa.properties.javax.persistence.lock.timeout=600 ,這似乎不起作用)。

那么我該怎么做呢?

我最終添加

@Autowired
private Environment env;

@PostConstruct
public void configureJpaTransactionManager() {
    ((JpaTransactionManager) this.platformTransactionManager).setDefaultTimeout(
                Integer.parseInt(env.getProperty("transaction.timeout", "3")));
}

到我的@SpringBootApplication主類,似乎可以完成工作。 不確定這是最好的方法。 (“ transaction.timeout”是我組成的屬性名稱)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM