Fork me on GitHub
#sql
<
2021-04-30
>
richiardiandrea16:04:30

Hi there, I have an interesting problem here, we have a doseq within a with-db-transaction and I was wondering how that behaves in terms of connections. Does it open one connection per item in the doseq or it sends all the SQL statements over one connection (therefore one transaction?)

richiardiandrea17:04:20

yeah it seems like I would have to "unroll the loop" if I wanted to send individual statements

seancorfield17:04:15

with-db-transaction is going to create/manage a connection for you. It would be used for everything inside the body.

seancorfield17:04:31

(depending on what you pass to with-db-transaction)

seancorfield17:04:57

Any SQL actions that are dynamically within that “scope” will reuse that connection, assuming you’re passing in the bound value from with-db-transaction and not the thing you are making the tx from 🙂

seancorfield17:04:33

You say “statements” — what type of SQL are you executing? DDL often doesn’t respect transactions anyway.

richiardiandrea17:04:14

yep I now read that here: https://www.mchange.com/projects/c3p0/#configuring_statement_pooling basically it is something like this

(jdbc/with-db-transaction [api-tx api-db-spec]
      (doseq [report open-reports]
        (update-precision-on-all-measurements api-tx report)))

richiardiandrea17:04:02

I see now it is not a connection issue though and thank you for answering

seancorfield17:04:58

Right, so it makes a Connection from the db spec and starts a TX on it, then the body is executed with that new TX-enabled Connection (`api-tx`), then the TX is committed and the Connection closed.

👍 2