0

Given that there is just one simple SQL statement (example: select id from foo) that I need to execute on my Postgres 15 database, I am curious to know the performance impact of executing it using Play API's db.withTransaction vs using db.withConnection?

I suppose the former would be costlier since the database would need to start a transaction but I was hoping to get more details on the performance cost of using the former vs the latter.

3
  • 2
    Have you tried to do some benchmark? If you are using sbt, you can use sbt-jmh plugin Commented Jul 3 at 14:44
  • withConnection and withTransaction comes from play API for databases that gives you a jdbc connection. Then you can use the connection directly or you can use different libs such as anorm or slick. Without an specific case, is hard to say which approach has better performance Commented Jul 3 at 14:52
  • 1
    I doubt you'll see a significant difference if the only statement issued is a simple select. This will likely depend on the default transaction level that depends on the DB itself. Anyway, I would rather choose one or the other depending the consistency level you need rather than the performance.
    – Gaël J
    Commented Jul 3 at 15:28

1 Answer 1

0

According to the postgresql docs:

PostgreSQL actually treats every SQL statement as being executed within a transaction. If you do not issue a BEGIN command, then each individual statement has an implicit BEGIN and (if successful) COMMIT wrapped around it. A group of statements surrounded by BEGIN and COMMIT is sometimes called a transaction block.

So, even if you don't use a transaction, it will still use a transaction. Having said that, I think the withTransaction version issues more statements, and given that it's just a simple select, I think you can use the withConnection version and never look back. But... you didn't tell us about this select statement. Are you just checking the state of the connection pool? Or do you need to be worried about the isolation level?

Not the answer you're looking for? Browse other questions tagged or ask your own question.