Mar 22nd, 2006, 07:44 PM
Database connections don't close until all tests are done
I've been trying to get XA transactions in Tomcat using Jencks. I'm doing this because I use both JDO and JCR (Apache's Jackrabbit) in my project and every item in the JCR repository has data about it stored in JDO (permissions for Acegi mainly). While I was successful in getting Jencks to provide transactions to JDO and JCR, I noticed during my testing that each time a new file with integration tests was run, two more database connections were opened (I'm using PostgreSQL 8.1, with JDBC connector 8.1-405... this should support XA according to the Postgres JDBC FAQ). By the end of my tests, there were more than 50 connections. Once maven finished, they all closed.
I'm tracking this by watching the processes running on my machine (MacOS X). I'm pretty sure each instance of "postgres" in the process list is a database connection. I've also noticed that its only ever the one or two most recently created "postgres" processes that do any work.
In trying to isolate the problem, I created two sets of tests, one that used Jencks only with JDO, and the other that used Jencks only with JCR. I found the same behavior in both test sets. Then I took Jencks out of the picture and moved back to Spring's JdoTransactionManager for the JDO set where I saw the same behavior again. So this appears to be independent of the transaction manager.
I'm not sure how to track this down further, but it needs to be fixed because no matter which transaction manager I use, I'm going to be adding to the project and writing more integration tests. As things stand, I can't build the maven website because the unit tests are executed twice (once normally and once for cobertura coverage) and a few tests into the second pass, everything locks up (presumably waiting for an available connection).
Does anyone know what might be causing this, or have suggestions as to what I can check next?
Mar 23rd, 2006, 04:58 AM
I may have been wrong when I assumed that each instance of a postgres process meant an open connection to the database. I don't know enough about how postgres spawns processes, or when, but I did find that if I set maven's JUnit plugin to fork a new JVM for each test suite, the postgres processes don't build up.
It turns out the lockups weren't necessarily caused by a lack of connections, but a lack of RAM. Each postgres process uses about 40 MB, and even though I have 1.5 GB in my machine, after enough tests have been run (it varies depending on what other apps are open) there's just not enough left for more. It may have to do with the amount of shared memory available, but I'm not sure.
Of course, forking off the tests causes failures for those tests I have using easymock's class extension, but that's a different (and hopefully easier) problem to deal with.