Simulate JBoss Connection Timeout

A simple Java web application that demonstrates how to simulate a database connection timeout in a JBoss/WildFly environment.

Overview

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.

Purpose

The main purposes of this project are:

  1. To demonstrate how connection timeouts manifest in a JBoss/WildFly environment
  2. To provide a test bed for configuring and testing different timeout settings
  3. To help developers understand and handle database connection timeouts properly

Prerequisites

  • Java 21
  • Maven 3.x
  • JBoss EAP or WildFly application server
  • PostgreSQL database

Dependencies

  • Jakarta EE Web API 10.0.0

Setup and Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/simulate-jboss-connection-timeout.git
    cd simulate-jboss-connection-timeout
    
  2. Build the project:

    mvn clean package
    
  3. 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>
  4. Deploy the WAR file to your JBoss/WildFly server:

    cp target/simulate-jboss-connection-timeout.war $JBOSS_HOME/standalone/deployments/
    

Usage

  1. Start your JBoss/WildFly server
  2. Access the application at: http://localhost:8080/simulate-jboss-connection-timeout/
  3. The application will attempt to execute a query that sleeps for 10 seconds
  4. If your datasource is configured with a query timeout less than 10 seconds, you should see a timeout exception in the server logs

How It Works

The application consists of two main components:

  1. IndexController: A servlet that handles HTTP requests and executes a long-running query
  2. DatabaseHelper: A helper class that provides database connections from a JNDI datasource

When a request is made to the application, it:

  1. Obtains a database connection from the configured datasource
  2. Executes a SELECT pg_sleep(10) query, which forces PostgreSQL to wait for 10 seconds
  3. If the datasource is configured with a timeout less than 10 seconds, a timeout exception will be thrown
  4. The exception is caught and printed to the server logs
  5. The application returns a simple JSON response: {"hello":"world"}

Configuration Options

You can modify the timeout behavior by:

  1. Changing the sleep duration in IndexController.java
  2. Adjusting the timeout settings in your JBoss/WildFly datasource configuration

Author

Muhammad Edwin < edwin at redhat dot com >

License

This project is open source and available under the MIT License.