Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Datasources close on Tomcat 7 undeploy with Spring Boot

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.

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.

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?

like image 423
zachariahyoung Avatar asked Aug 05 '14 23:08

zachariahyoung


People also ask

Can Tomcat run spring boot application?

React Full Stack Web Development With Spring BootBy using Spring Boot application, we can create a war file to deploy into the web server. In this chapter, you are going to learn how to create a WAR file and deploy the Spring Boot application in Tomcat web server.

How does spring boot embedded Tomcat work?

Spring Boot is a popular Java-based framework to develop microservices. By default, the Spring Tool Suite (STS) IDE -- which is used to build a Spring Boot application -- will automatically create an embedded Tomcat server with the microservices developed each time a build or deployment occurs.

Can I deploy spring boot jar to Tomcat?

You can not deploy Jar to tomcat and expect it to load your web application.

Does spring boot support Tomcat 10?

* names). Spring Boot 2 and Spring 5 support only the previous Java EE 8 specification, you need to wait for Spring Boot 3 and Spring 6 for Tomcat 10 support. Alternatively you can pass Spring libraries through the Apache Tomcat Migration Tool, which just reached version 1.0 or downgrade to Tomcat 9.0.


1 Answers

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

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

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'.

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

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

You need to update your dataSource() method:

@Bean(destroyMethod="")
public DataSource dataSource() {
    return new JndiDataSourceLookup().getDataSource("com.datasource.CONSUMER");
}  
like image 141
Andy Wilkinson Avatar answered Oct 20 '22 06:10

Andy Wilkinson