How to avoid “IJ000470: … trying to use a connection factory that has been shut down…”

Summary

The IJ000470 error occurs when trying to use a connection factory that has been shut down. This issue arises in Jakarta EE applications, particularly when using Wildfly as the application server. The error is often encountered during the shutdown process of the application, when the @PreDestroy method of a @Singleton EJB attempts to write data to the database.

Root Cause

The root cause of this issue is the shutdown of the connection factory before the @PreDestroy method is executed. This can be attributed to the following reasons:

  • The connection factory is shut down as part of the application server’s shutdown process.
  • The @PreDestroy method is called after the connection factory has been shut down.
  • The application does not properly synchronize the shutdown of the connection factory with the execution of the @PreDestroy method.

Why This Happens in Real Systems

This issue occurs in real systems due to the following factors:

  • Asynchronous shutdown processes can lead to a situation where the connection factory is shut down before the @PreDestroy method is executed.
  • Lack of synchronization between the shutdown of the connection factory and the execution of the @PreDestroy method can cause this issue.
  • Incorrect configuration of the application server or the connection factory can also contribute to this problem.

Real-World Impact

The real-world impact of this issue includes:

  • Data loss: The application may not be able to write data to the database during shutdown, resulting in data loss.
  • Application instability: The error can cause the application to become unstable or even crash during shutdown.
  • Difficulty in debugging: The issue can be challenging to debug due to its asynchronous nature and the complexity of the application server’s shutdown process.

Example or Code

@Singleton
public class DBStatistic {

    @Resource(lookup = "java:jboss/jdbc/wsStatisticDS")
    private DataSource dataSource;

    @PreDestroy
    public void shutdown() {
        // Attempt to write data to the database
        try (Connection connection = dataSource.getConnection()) {
            // Write data to the database
        } catch (SQLException e) {
            // Handle the exception
        }
    }
}

How Senior Engineers Fix It

Senior engineers can fix this issue by:

  • Synchronizing the shutdown of the connection factory with the execution of the @PreDestroy method.
  • Using a separate connection factory for the @PreDestroy method to ensure that it is not shut down before the method is executed.
  • Implementing a retry mechanism to handle cases where the connection factory is shut down during the execution of the @PreDestroy method.

Why Juniors Miss It

Junior engineers may miss this issue due to:

  • Lack of experience with Jakarta EE and Wildfly.
  • Insufficient understanding of the application server’s shutdown process and the @PreDestroy method.
  • Inadequate testing of the application’s shutdown process, which can lead to the issue being overlooked.