A simple Java web application that demonstrates how to simulate a database connection timeout in a JBoss/WildFly environment.
This project provides a practical example of what happens when a database query takes longer than the configured connection timeout in a JBoss/WildFly application server. It uses a PostgreSQL database and deliberately executes a long-running query (pg_sleep(10)) to trigger a timeout exception.
The main purposes of this project are:
- To demonstrate how connection timeouts manifest in a JBoss/WildFly environment
- To provide a test bed for configuring and testing different timeout settings
- To help developers understand and handle database connection timeouts properly
- Java 21
- Maven 3.x
- JBoss EAP or WildFly application server
- PostgreSQL database
- Jakarta EE Web API 10.0.0
-
Clone the repository:
git clone https://github.com/yourusername/simulate-jboss-connection-timeout.git cd simulate-jboss-connection-timeout -
Build the project:
mvn clean package -
Configure a PostgreSQL datasource in your JBoss/WildFly server with the JNDI name
java:/pgsql-ds. Example configuration in standalone.xml:<datasource jndi-name="java:/pgsql-ds" pool-name="PostgreSQLPool" enabled="true" use-java-context="true"> <connection-url>jdbc:postgresql://localhost:5432/yourdb</connection-url> <driver>postgresql</driver> <security> <user-name>postgres</user-name> <password>postgres</password> </security> <timeout> <query-timeout>5</query-timeout> </timeout> </datasource>
-
Deploy the WAR file to your JBoss/WildFly server:
cp target/simulate-jboss-connection-timeout.war $JBOSS_HOME/standalone/deployments/
- Start your JBoss/WildFly server
- Access the application at: http://localhost:8080/simulate-jboss-connection-timeout/
- The application will attempt to execute a query that sleeps for 10 seconds
- If your datasource is configured with a query timeout less than 10 seconds, you should see a timeout exception in the server logs
The application consists of two main components:
- IndexController: A servlet that handles HTTP requests and executes a long-running query
- DatabaseHelper: A helper class that provides database connections from a JNDI datasource
When a request is made to the application, it:
- Obtains a database connection from the configured datasource
- Executes a
SELECT pg_sleep(10)query, which forces PostgreSQL to wait for 10 seconds - If the datasource is configured with a timeout less than 10 seconds, a timeout exception will be thrown
- The exception is caught and printed to the server logs
- The application returns a simple JSON response:
{"hello":"world"}
You can modify the timeout behavior by:
- Changing the sleep duration in
IndexController.java - Adjusting the timeout settings in your JBoss/WildFly datasource configuration
Muhammad Edwin < edwin at redhat dot com >
This project is open source and available under the MIT License.