Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;“连接已关闭”;从暂停返回后在Spring应用程序中_Java_Xml_Spring_Spring Mvc_Jdbc - Fatal编程技术网

Java &引用;“连接已关闭”;从暂停返回后在Spring应用程序中

Java &引用;“连接已关闭”;从暂停返回后在Spring应用程序中,java,xml,spring,spring-mvc,jdbc,Java,Xml,Spring,Spring Mvc,Jdbc,我使用的是SpringSecurity,它在一些BeanXML配置中使用jdbcTemplate。当我将我的开发笔记本电脑置于睡眠状态,稍后再开始工作时,当我尝试恢复测试应用程序时,会得到“此连接已关闭” 我知道如何恢复的唯一方法是通过繁琐地重新启动本地服务器,该服务器通过internet连接到DB(postgreSQL) 是否有某种方法可以让应用程序在从挂起(或长期不在?)返回后重新建立jdbc连接,而不是反复显示连接错误,从不重新建立连接 我的security-context.xml如下所示

我使用的是SpringSecurity,它在一些BeanXML配置中使用jdbcTemplate。当我将我的开发笔记本电脑置于睡眠状态,稍后再开始工作时,当我尝试恢复测试应用程序时,会得到“此连接已关闭”

我知道如何恢复的唯一方法是通过繁琐地重新启动本地服务器,该服务器通过internet连接到DB(postgreSQL)

是否有某种方法可以让应用程序在从挂起(或长期不在?)返回后重新建立jdbc连接,而不是反复显示连接错误,从不重新建立连接

我的security-context.xml如下所示:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.2.xsd">
    <http use-expressions="true">
        <intercept-url pattern="/login*" access="isAnonymous()" />
        <intercept-url pattern="/**" access="isAuthenticated()" />
        <intercept-url pattern="/upload" access="hasRole('ROLE_ADMIN')" />
        <form-login login-page="/login" default-target-url="/" always-use-default-target="false" authentication-failure-url="/login-error"/>
        <remember-me/>
        <logout />
    </http>
<global-method-security secured-annotations="enabled" />
    <authentication-manager>
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"/>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

还有我的数据库-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" 
        destroy-method="close">
        <property name="driverClassName" value="${dataSource.driverClassName}" />
        <property name="url" value="${dataSource.url}" />
        <property name="username" value="${dataSource.username}" />
        <property name="password" value="${dataSource.password}" />

</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="dataSource"></property>  
</bean>
</beans>

我不喜欢这些XML配置,因为我更希望对应用程序进行编程控制,但我不是选择和设计应用程序体系结构的人,所以我正在使用它。如果我们不使用基于XML的配置,我将能够以编程方式检查错误连接并轻松地重新建立它


使用基于XML的配置从挂起(或长时间不在?)返回后,如何让应用程序重新建立连接?

使用autoReconnect=true配置连接字符串。这是URL连接字符串的一个属性,它在驱动程序级别工作。您需要更改数据源配置中的连接字符串。下面的URL用于mysql。博士后一定有办法做到这一点

url="jdbc:mysql://localhost:3306/yourDB?autoReconnect=true"

或者您可以在postgres配置文件中增加超时时间。

谢谢您的建议!我试过做
url=“jdbc:postgresql://localhost:5432/yourDB?autoReconnect=true“
但它似乎不起作用。