Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transactions do not work with *db-connection* #42

Open
miikka opened this Issue Jan 9, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@miikka
Copy link
Contributor

miikka commented Jan 9, 2019

The way I'm using Toucan is that I never call set-default-db-connection! and instead I always set *db-connection*. I'm using Integrant and explicitly passing the database connection fits that model better than having global state.

I just noticed that transactions do not work with *db-connection* – if you use toucan.db/transaction, the code inside the macro will not be run inside a transaction. This is because toucan.db/connection always returns *db-connection* if it's set:

toucan/src/toucan/db.clj

Lines 124 to 127 in c5567c8

(or *db-connection*
*transaction-connection*
@default-db-connection
(throw (Exception. "DB is not set up. Make sure to call set-default-db-connection! or bind *db-connection*."))))

I'm not sure if this is a bug or an intentional feature, but I certainly expected transactions to work. As a workaround, I'm now binding toucan.db/*transaction-connection* (which is marked as private) instead of *db-connection* and that works fine.

@miikka

This comment has been minimized.

Copy link
Contributor Author

miikka commented Jan 10, 2019

I thought about it more and concluded that the current behavior is a feature: it's good to be always able to tell the database functions which connection they should use and *db-connection* allows you to do exactly that.

Binding *transaction-connection* supports my use case well even if the name is a bit misleading. Would it make sense to make this use case more "official" by marking *transaction-connection* as public?

@camsaul

@camsaul

This comment has been minimized.

Copy link
Member

camsaul commented Jan 16, 2019

Hey @miikka that's a use case I really haven't thought much about. If you want to go ahead and make *transaction-connection* public I think that's the best course of action here.

@camsaul camsaul added the help wanted label Jan 16, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.