Skip to content

Instantly share code, notes, and snippets.

@souenzzo

souenzzo/sql.clj Secret

Created May 28, 2020 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save souenzzo/2783a30bc8d5b904a7b1e0ecf8cbe082 to your computer and use it in GitHub Desktop.
Save souenzzo/2783a30bc8d5b904a7b1e0ecf8cbe082 to your computer and use it in GitHub Desktop.
(let [jdbc-url (System/getenv "JDBC_DATABASE_URL")
ds (jdbc/get-datasource {:jdbcUrl jdbc-url})]
(with-open [test-conn (jdbc/get-connection ds {:auto-commit false})]
(jdbc/execute! test-conn
["DELETE FROM app_user"])
(jdbc/with-transaction [app-conn test-conn
#_{:rollback-only true}]
(let [sp (.setSavepoint app-conn)]
(try
;; here is my app-code; so it should use `jdbc/with-transact` when it needs
(jdbc/with-transaction [tx app-conn]
(jdbc/execute! tx ["INSERT INTO app_user (username) VALUES (?)"
(UUID/randomUUID)]))
(finally
;; throws: ERROR: ROLLBACK TO SAVEPOINT can only be used in transaction blocks
(.rollback app-conn sp)))))
;; should be empty again
(jdbc/execute! test-conn
["SELECT * FROM app_user"])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment