5 Replies Latest reply on May 4, 2016 7:26 AM by tomjenkinson

    Connection leak with Postgres




      I'm using Narayana's JDBC TransactionalDriver with a Postgres XA datasource. My problem is that the connection are never closed.


      After some debug, I've found that if I'm adding a modifier, the connections are closed :


      ModifierFactory.putModifier ("postgresql native driver", -1, -1, PsqlConnectionModifier.class.getName());


      Is that the expected behavior ? And am I right with my workaround ?

        • 1. Re: Connection leak with Postgres

          From my reading of the code (narayana/ConnectionImple.java at master · jbosstm/narayana · GitHub) we should be closing the connection if there is no modifier. Would it be possible to either include a small test case or some logging.

          • 2. Re: Connection leak with Postgres

            With a modifier, the close method is called on the XAConnection object, with no modifier it's called on the Connection object.


            With the postgresql driver, you have to close the XAConnection.


            Here's my example, showing the leak :


            public class ConnectionLeakLauncher {
                public static void main(String[] args) throws Exception {
                    for (int i = 0; i < 100; i++) {
                        Connection connection = DriverManager.getConnection("jdbc:arjuna:java:comp/env/datasources/XaPgDS", "postgres", "azer");
                        try (PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM pg_stat_activity")) {
                            ResultSet resultSet = statement.executeQuery();
                            if (resultSet.next()) {
                        } finally {
                private static void setupDS() throws NamingException, ClassNotFoundException {
                    PGXADataSource xaPgDS = new PGXADataSource();
                    InitialContext ctx = new InitialContext();
                    ctx.bind("datasources/XaPgDS", xaPgDS);
                    // ModifierFactory.putModifier("postgresql native driver", -1, -1, PsqlConnectionModifier.class.getName());
            1 of 1 people found this helpful
            • 3. Re: Connection leak with Postgres

              I think it looks like a good change to include Postgres in the list of databases that require this XAConnectionModifier.


              Your workaround is likely fine and please do consider raising a JBTM and a PR so we can see the content of PsqlConnectionModifier.

              For info this is the list that currently require a modifier (i.e. more or less all databases):



              Those databases also needed the isSameRM override so you wouldn't want to just add to that list but instead just add the line:

              ModifierFactory.putModifier("postgresql native driver", -1, -1, PsqlConnectionModifier.class.getName());


              And a new modifier that does not do the IsSameRm override.


              Thanks for the report!

              • 4. Re: Connection leak with Postgres

                To be honest, I've no idea of the goal of the modifiers. I've just seen that it could solve my problem.


                At my first attempt, I've used the IsSameRMModifier class, but having a internal package in my imports hurted me. So I duplicated the class with the name of PsqlConnectionModifier.


                I've created the issue : [JBTM-2667] JDBC Connection leak with Postgres - JBoss Issue Tracker

                And I can prepare a PR if it can help.

                1 of 1 people found this helpful
                • 5. Re: Connection leak with Postgres

                  Looking more at the bug I think it is just a straight bug. I have a PR and its linked into the issue you created - thanks again for the report!