如何配置锁定等待时间

时间:2018-01-29 06:32:18

标签: hibernate spring-data spring-data-jpa

在我的Web应用程序中,我使用Spring 4.2.9.RELEASE,Hibernate 4.3.8.Final,Spring Data 1.8.2.RELEASE和MS SQLSERVER 2014 Enterprise。

我想配置锁等待时间而不是使用默认值(-1,这意味着永远等待)。根据我的研究,我需要为

定义一个值
javax.persistence.lock.timeout

但是,我无法找到如何定义它的示例。这是我的配置:

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean> 

    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="poolName" value="derek6HikariCP" />
        <property name="connectionTestQuery" value="${jdbc.connectionTestQuery}" />
        <property name="dataSourceClassName" value="${jdbc.dataSourceClassName}" />
        <property name="maximumPoolSize" value="${jdbc.maximumPoolSize}" />
        <property name="minimumIdle" value="${jdbc.minimumIdle}" />
        <property name="idleTimeout" value="${jdbc.idleTimeout}" />
        <property name="connectionTimeout" value="${jdbc.connectionTimeout}" />
        <property name="dataSourceProperties">
            <props>
                <prop key="url">${jdbc.url}</prop>
                <prop key="user">${jdbc.username}</prop>
                <prop key="password">${jdbc.password}</prop>
            </props>
        </property>     
    </bean>

    <bean  id="entityManagerFactory" name="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="packagesToScan" value="myproject.domain" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>                                 
                <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
                <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
            </props>
        </property>
    </bean>     

  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>   

    <context:annotation-config />  

    <jpa:repositories base-package="myproject.repository"
        entity-manager-factory-ref="emf" transaction-manager-ref="transactionManager" /> 

1 个答案:

答案 0 :(得分:0)

我不确定这是否是唯一的解决方案,但是您无需在配置中定义它。尽管不是完美的解决方案,您也可以将其作为属性直接传递给EntityManager查询。例如:

Map<String, Object> properties = new HashMap<>();
properties.put("javax.persistence.lock.timeout", 0L);

EntityManager entityManager = getEntityManager();
try {
    entityManager.find(Entity.class, id, LockModeType, properties); 
    entityManager.getTransaction().commit();                        
} finally {
    entityManager.close();
}

您可以在此处找到更多文档:https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html