【问题标题】:JDBC SQL Server query timeoutJDBC SQL Server 查询超时
【发布时间】:2019-02-26 01:34:33
【问题描述】:

我正在开发一个使用 Maven 构建的 Java 应用程序。该应用程序使用来自本地 SQL Server 数据库的数据。我工作场所的 SQL Server 不可用(几次断电)是很常见的,所以我想为系统设置更短的超时时间以引发异常。

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection

我们的想法是在让我们知道服务器不可用之前让应用程序挂起约 30 秒。

我尝试在数据源的 URL 中包含 socketTimeout 和 queryTimeout 属性,如 these two 链接中所示,但无济于事。有没有办法为此设置全局超时?

【问题讨论】:

    标签: java sql-server maven timeout


    【解决方案1】:

    通过 jdbcTemplate:

    JdbcTemplate template = new JdbcTemplate(dataSource);
    template.setQueryTimeout(30);
    

    【讨论】:

    • 我确实通过 ApplicationContext.xml 文件更改了 jdbcTemplate 的 queryTimeout。 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> <property name="queryTimeout" value="1"></property> </bean> 但是,它似乎没有什么不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2018-08-03
    相关资源
    最近更新 更多