为什么使用 Spring Boot 取消部署 Tomcat 7 上的数据源会关闭 [英] Why does Datasources close on Tomcat 7 undeploy with Spring Boot

查看:56
本文介绍了为什么使用 Spring Boot 取消部署 Tomcat 7 上的数据源会关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数据源的应用程序.每次我从管理器 GUI 取消部署应用程序时,数据源都会被关闭.当我尝试重新部署时,数据源保持关闭并抛出以下异常:

I have an application that has a datasource. Everytime I undeploy the application from the manager GUI the datasources are being closed. When I try to redeploy, the datasource stays closed and throws the following exception:

{
  "status" : "DOWN",
  "error" : "org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Data source is closed"
}

Caused by: java.sql.SQLException: Data source is closed
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1362) ~[tomcat-dbcp.jar:7.0.53]
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044) ~[tomcat-dbcp.jar:7.0.53]
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    ... 81 common frames omitted

重新启动服务器可以解决此问题,但这对于生产应用程序来说不是可接受的解决方案.

Restarting the server solves this issue but that is not an acceptable solution for a production application.

我有一个具有不同数据源的不同应用程序,但存在相同问题.

I have a different application with a different datasource with the same issue.

两个应用程序都使用 Spring Boot 1.1.4 版和 Tomcat 7.其中一个应用程序已转换为 Spring Boot,并且在转换前没有数据源问题.

Both application are using Spring Boot version 1.1.4 with Tomcat 7. One of the applications was converted to Spring Boot and didn't have the datasource issues before conversion.

以下是我当前在 Spring Boot Application.java 文件中创建数据源的方法.

Below is how I create the Datasource currently in my Spring Boot Application.java file.

@Bean()
    public DataSource dataSource() {
        return new JndiDataSourceLookup().getDataSource("com.datasource.CONSUMER");
    }   

如何阻止这种情况发生?

How do I stop this from happening?

推荐答案

这不是特定于 Spring Boot,而是标准的 Spring 行为.

This isn't specific to Spring Boot, it's standard Spring behaviour.

默认情况下,Spring 会推断 bean 的 destroy 方法.来自 javadoc对于 @Bean:

By default, Spring will infer a bean's destroy method. From the javadoc for @Bean:

为了方便用户,容器将尝试针对从 @Bean 方法返回的对象推断一个 destroy 方法.例如,给定一个返回 Apache Commons DBCP BasicDataSource@Bean 方法,容器将注意到该对象上可用的 close() 方法和自动将其注册为 destroyMethod.这种销毁方法推断"目前仅限于检测名为close"或shutdown"的公共无参数方法.

As a convenience to the user, the container will attempt to infer a destroy method against an object returned from the @Bean method. For example, given an @Bean method returning an Apache Commons DBCP BasicDataSource, the container will notice the close() method available on that object and automatically register it as the destroyMethod. This 'destroy method inference' is currently limited to detecting only public, no-arg methods named 'close' or 'shutdown'.

javadoc 继续描述如何禁用此行为:

The javadoc goes on to describe how to disable this behaviour:

要禁用特定 @Bean 的销毁方法推理,请指定一个空字符串作为值,例如@Bean(destroyMethod="")

To disable destroy method inference for a particular @Bean, specify an empty string as the value, e.g. @Bean(destroyMethod="")

您需要更新您的 dataSource() 方法:

You need to update your dataSource() method:

@Bean(destroyMethod="")
public DataSource dataSource() {
    return new JndiDataSourceLookup().getDataSource("com.datasource.CONSUMER");
}  

这篇关于为什么使用 Spring Boot 取消部署 Tomcat 7 上的数据源会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆