【问题标题】:Set lock timeout programmatically Spring Boot JPA以编程方式设置锁定超时 Spring Boot JPA
【发布时间】:2019-04-13 08:28:39
【问题描述】:

我有

@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,这似乎不起作用)。

那么我该怎么做呢?

【问题讨论】:

  • 您可以将 TransactionDefinition 超时修改为 X 秒。例如,像这样的东西。 examples.javacodegeeks.com/enterprise-java/spring/jdbc/… 然后你可以使用 @ConfigurationProperties 或者一个 setter 或者一个 bean 和你自己的服务。
  • @Compass 谢谢。这让我找到了解决方案。

标签: java spring spring-boot spring-data-jpa


【解决方案1】:

我最终添加了

@Autowired
private Environment env;

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

到我的@SpringBootApplication-annotated 主类,这似乎完成了这项工作。不确定这是最好的方法。 (“transaction.timeout”是我编的一个属性名)。

【讨论】:

    猜你喜欢
    • 2015-09-24
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多