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?
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.
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.
You can not deploy Jar to tomcat and expect it to load your web application.
* 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.
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 DBCPBasicDataSource
, the container will notice theclose()
method available on that object and automatically register it as thedestroyMethod
. 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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With