0

I am developing a REST API application using Spring-Boot. It turns that when I start the server (using the embedded tomcat) and I start sending requests to my API, I get the expected responses. But, lets say I wait for 30 minutes before send another request, at that time I get an org.springframework.transaction.CannotCreateTransactionException with root cause java.net.SocketTimeoutException: Read timed out.

My application connects to a remote MySQL server data base.

My WebApplicationStarter class looks looks is the following:

@Configuration
@EnableAutoConfiguration
@ComponentScan("monitec")
public class WebApplicationStarter extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplicationStarter.class);
    }

    public static void main(String[] args) throws Exception {
        ApplicationContext context = SpringApplication.run(WebApplicationStarter.class, args);
    }

    @Bean
    public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
        return hemf.getSessionFactory();
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();

        factory.addConnectorCustomizers(connector ->
                ((AbstractProtocol) connector.getProtocolHandler()).setConnectionTimeout(10000));

        factory.setPort(7543);//TODO: Replace this hardcoded value by a system preference
        factory.setSessionTimeout(20000);

        // configure some more properties

        return factory;
    }
}

My application.properties is the following:

# Thymeleaf
spring.thymeleaf.cache: false
# Data Source
spring.datasource.url=jdbc:mysql://hostname:8888/schema_name
spring.datasource.username=xxxxxxxxx
spring.datasource.password=xxxxxxxxxxx
# Hibernate
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
#spring.jpa.generate-ddl=true
#spring.jpa.hibernate.ddl-auto=create

I have research several posts and haven't been able to solve my problem. I also set the sessionTimeout to "-1" to make it infinite but it didn't work. I don't know if perhaps it is the MySQL server the one that is closing the connection, and if that's the case I would like to know how to make my application open a new one when a new http request arrive to the server. For now I have not enable any kind of security, I mean I do not require authentication from any client calling my REST API, I'll do it in the future, but for now it is not necessary.

Thank you in advance, I am open to any suggestions and improvements you can give me. If you need my REST Controller code, let me know and I'll post it.

PD: I am using POST MAN REST CLIENT to test my application.

EDIT: I always get the read timed out exception and I can't send any more requests to the server unless I restart the server. This means that after the exception, every request that I send from any client, I keep receiving the exception and the only way to get the expected result is by restarting the application (the embedded tomcat)

Hassingard
  • 108
  • 9
  • Session has timeout. please see the [reference](http://stackoverflow.com/questions/24561915/spring-boot-spring-security-session-timeout) –  Feb 23 '16 at 05:07
  • Hi, I already saw that post, problem there is not being able to get the timeout, while my problem is that I do get the timeout and can't sent any more requests unless I restart the server. I think I forgot to mention that. I'll edit the post. – Hassingard Feb 23 '16 at 05:10

1 Answers1

0

I have narrowed the issue to be a problem with Spring-Boot autoconfig managing the connection pool. And I confirmed my diagnose after reading this post https://aodcoding.wordpress.com/2015/05/22/handling-connection-pool-issues-in-spring-boot/

So, I solve the problem by adding connection pool properties, I decided not to used the C3P0 ones described in the article that I mentioned, but instead I used spring boot ones as follows:

spring.datasource.max-active=50
spring.datasource.initial-size=5
spring.datasource.max-idle=10
spring.datasource.min-idle=5
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=SELECT 1 FROM DUAL
spring.datasource.time-between-eviction-runs-millis=5000
spring.datasource.min-evictable-idle-time-millis=60000

And as far as I can tell, the problem is solved. I have wait for long time and re send requests to my service and I am getting proper responses. Next step for me is start enabling spring security configuration to secure the REST services.

Hope this help to any one having same issue I had. Because if you see the exception, is not very clear that the problem is due to connection pool, you would try to hit the problem following the wrong direction.

Hassingard
  • 108
  • 9