Creating a Query Timeout on JBoss EAP 8

Sometimes we want to limit the time needed for each queries, this is necessary to prevent our system from being slow and unresponsive due to slow queries.

For this sample, we are going to run a query on PostgreSQL that can simulate slowness

select pg_sleep(10);

And lets start with a pom file,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.edw</groupId>
    <artifactId>simulate-jboss-connection-timeout</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <dependency>
            <groupId>jakarta.platform</groupId>
            <artifactId>jakarta.jakartaee-web-api</artifactId>
            <version>10.0.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <warName>${project.name}</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

And some Java files,

package com.edw.helper;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DatabaseHelper {
    private DataSource ds = null;

    public DatabaseHelper() {
        try {
            Context initCxt = new InitialContext();
            ds = (DataSource) initCxt.lookup("java:/pgsql-ds");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}

package com.edw.controller;

import com.edw.helper.DatabaseHelper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet(name = "IndexController", urlPatterns = "/")
public class IndexController extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private DatabaseHelper dbHelper = new DatabaseHelper();

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException {

        // create a 10 second query
        String sql = "select pg_sleep(10)";

        try (Connection conn = dbHelper.getConnection();
             PreparedStatement preparedStmt = conn.prepareStatement(sql)) {

            // do some query
            ResultSet resultSet = preparedStmt.executeQuery();

        } catch (Exception e) {
            e.printStackTrace(); // we should see some timeout exception here
        }

        response.setContentType("application/json");

        PrintWriter out = response.getWriter();
        out.println("{\"hello\":\"world\"}");
    }

}

Above code is using a Connection Pool called “java:/pgsql-ds” and would simulate a slowness when being executed,

$ time curl -kv http://127.0.0.1:8080/simulate-jboss-connection-timeout/
*   Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080
> GET /simulate-jboss-connection-timeout/ HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: application/json;charset=ISO-8859-1
< Content-Length: 19
< Date: Mon, 16 Jun 2025 08:04:50 GMT
<
{"hello":"world"}
* Connection #0 to host 127.0.0.1 left intact
real    0m 10.17s
user    0m 0.01s
sys     0m 0.01s

Lets try giving a timeout to that Connection Pool,

And try to do another curl test

$ time curl -kv http://127.0.0.1:8080/simulate-jboss-connection-timeout/
*   Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080
> GET /simulate-jboss-connection-timeout/ HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: application/json;charset=ISO-8859-1
< Content-Length: 19
< Date: Mon, 16 Jun 2025 08:05:26 GMT
<
{"hello":"world"}
* Connection #0 to host 127.0.0.1 left intact
real    0m 5.22s
user    0m 0.00s
sys     0m 0.01s

And we can see that theres an exception logged at our JBoss console,

15:11:42,531 ERROR [stderr] (default task-1) org.postgresql.util.PSQLException: ERROR: canceling statement due to user request
15:11:42,532 ERROR [stderr] (default task-1) 	at deployment.simulate-jboss-connection-timeout.war//com.edw.controller.IndexController.doGet(IndexController.java:43)

Complete code can be viewed on below Github repository

https://github.com/edwin/simulate-jboss-connection-timeout

Leave a Comment

Your email address will not be published.