How to Prevent Spring Applications from Losing Connection to MySQL After 8 Hours?

Question

What configurations are needed to ensure that a Spring application maintains a persistent connection to a MySQL database without dropping after 8 hours?

<property name="spring.datasource.validation-query" value="SELECT 1"/>

Answer

Maintaining a persistent connection between a Spring application and a MySQL database is crucial for applications that rely on continuous database interactions. A common issue is the loss of connection after a certain idle period, often due to server side timeouts. This guide explains how to configure your Spring application to avoid this issue effectively.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
    <property name="username" value="user"/>
    <property name="password" value="password"/>
    <property name="validationQuery" value="SELECT 1"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
</bean>

Causes

  • MySQL server timeout settings
  • Spring datasource configurations
  • Network instability
  • Connection pooling timeout settings

Solutions

  • Configure the connection pool to test connections before use, using a validation query.
  • Set the `validation-query` property in your Spring configuration to a simple query (e.g., SELECT 1).
  • Adjust MySQL server `wait_timeout` and `interactive_timeout` settings to prevent premature disconnections.
  • Implement a connection keep-alive mechanism to regularly ping the database.

Common Mistakes

Mistake: Not setting a validation query for the connection pool.

Solution: Ensure you set `validation-query` to a lightweight SQL command, such as `SELECT 1`.

Mistake: Neglecting to adjust server timeout settings.

Solution: Modify the MySQL `wait_timeout` and `interactive_timeout` parameters to higher values, such as 28800 seconds (8 hours).

Helpers

  • Spring application connection
  • MySQL database connection
  • Spring config MySQL
  • persistent database connection
  • prevent connection loss

Related Questions

⦿How to Resolve 'lib/modules Locked' Issue in Software Development?

Understanding the causes and solutions for the libmodules locked issue in software development. Expert tips and code snippets included.

⦿How to Dynamically Add a JAR File to the Classpath at Runtime in Java 9

Learn how to dynamically add a JAR file to the classpath in Java 9 at runtime with stepbystep instructions and code examples.

⦿How to Render Devanagari Ligatures in Java Swing JComponent on Mac OS X?

Learn how to render Devanagari ligatures in Java Swing JComponent on Mac OS X with detailed steps and code examples.

⦿How to Resolve Connection Issues with Samsung Remote Test Lab

Learn how to troubleshoot connection problems while using Samsung Remote Test Lab with expert tips and solutions.

⦿How to Install a JAR in a Local Gradle Repository Like Maven?

Learn how to install a JAR file in a local Gradle repository similar to Mavens installinstallfile command.

⦿Is Java Vulnerable to Regular Expression Denial of Service (ReDoS)?

Learn about the vulnerability of Java to Regular Expression Denial of Service ReDoS attacks and how to mitigate it effectively.

⦿Is the Monster Builder an Effective Implementation of the Builder/Factory Pattern for Managing Complex Object Construction?

Explore whether the Monster Builder design pattern effectively abstracts complex constructors and setters in software design.

⦿How to Send an Email to a Non-ASCII Email Address in Java?

Learn how to send emails to NonASCII email addresses using Java. Stepbystep guide with code examples and common mistakes.

⦿How to Troubleshoot Tomcat Random Shutdowns with AbstractProtocol Pause After Mild Usage

Discover how to troubleshoot Tomcat random shutdowns caused by AbstractProtocol pauses after mild usage. Key solutions and insights provided.

⦿How to Compile JAXB Schema Independently from WSDL in JAX-WS

Learn how to independently compile JAXB schemas from WSDL in JAXWS with expert insights and code examples.

© Copyright 2025 - CodingTechRoom.com