Skip to content

Unclear how to use transactions #7

Description

@amacdougall

(If it is in fact impossible to use transactions, feel free to update the issue title, but I didn't want to assume too much.)

Description

The JDBC functions clojure.java.jdbc/with-db-transaction and clojure.java.jdbc/db-set-rollback-only! expect to operate on a "database connection" object compatible with the get-connection function. It appears that most people pass in the db spec they used to set up their normal connection, whether it's a DB URI, a hash with connection parameters, or whatever.

However, Foundation expects to use a javax.sql.DataSource-compatible object for every query. Since one of the allowed get-connection arguments is a {:datasource <ds>, ...} object, I tried doing this:

(clojure.java.jdbc/with-db-transaction [t-conn {:datasource db/conn}]
  (jdbc/db-set-rollback-only! t-conn)
  (pg/qry-> (:datasource t-conn) (db/alter-some-stuff! params)))

Where db/conn is the result of a def-datasource call. Although this syntax does alter the database, those changes persist.

This is not surprising: with-db-transaction is supposed to bind t-conn to a connection object. In this case, it binds a hash:

{:datasource
 #object[com.impossibl.postgres.jdbc.PGDataSource 0x5f9231ae "com.impossibl.postgres.jdbc.PGDataSource@5f9231ae"],
 :connection
 #object[com.impossibl.postgres.jdbc.PGConnectionImpl 0x129eb816 "com.impossibl.postgres.jdbc.PGConnectionImpl@129eb816"],
 :level 1,
 :rollback #<Atom@79e53c70: true>}

My theory is that the :connection part of that hash is the transaction connection, and if I were to operate on that, the changes would be rolled back. But Foundation expects to work on a PGDataSource, and it gets a new connection from it, one which does not have the transaction or rollback properties.

Am I right? And if so, is there a workaround for this situation, or just a better way of achieving my goal? I think you might have some unwelcome tension between Foundation's way of doing things and the bare-bones JDBC functions people may wish to apply.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions