December 28, 2012

PostgreSQL JDBC pool exhaustion under Play Framework 2.1-RC1

After experimenting with Scala and Play Framework version 2.1-RC1 and the default H2 Database, I decided to switch over to use PostgreSQL for persistence.

After installing PostgreSQL 9.2.2 and creating my test user and database, I reconfigured my Play application.

Update project/Build.scala to include the PostgreSQL driver as a dependency. val appDependencies = Seq( jdbc, anorm, "postgresql" % "postgresql" % "9.1-901-1.jdbc4" )

Update conf/application.conf to use the PostgreSQL driver for the database named "default". db.default.driver=org.postgresql.Driver db.default.url="postgres://testuser:testpass@localhost:5432/testdb" #db.default.user=sa #db.default.password=""

When applying database evolutions, the database connection pool would be exhausted:
org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections


The only solution was to restart the Play application entirely as I wasn't inclined to increase the connection limit without knowing why the existing limit was being hit. Re-applying the evolution would trigger the problem again.

Per Play's JDBC settings documentation, I could adjust connection pools, but as far as I could tell, the pool size wasn't the actual issue, since PostgreSQL was configured to support more connections than Play should have been using.

In my case, the problem was due to how Play handles semicolons included in a quoted string within a DML statement (initially, I had attributed this to how JDBC itself handled semicolons). You can view the current approach to statement splitting in framework/src/play-jdbc/src/main/scala/play/api/db/evolutions/Evolutions.scala line 233. Questions about this approach can be viewed on Play's Google Group and on Play 2.0 pull request #134.

This issue causes the query to fail execution and the Evolutions code retries enough times to consume the server's available connections. As I'm inserting seed data, I removed the semicolon from the quoted string and corrected the values after the evolution was applied.

No comments:

Post a Comment