Not sure about how to use transaction and rollback in MyXQL

Hi all

I’m not sure how to use transaction and rollback with MyXQL (and probably with the other sql drivers).

In order to have the rollback performed in case of error, should I write:

res = case MyXQL.transaction(cnx, fun, opts) do
  {:ok, res} -> res
  {_error, reason} -> MyXQL.rollback(cnx, reason)

or is there any other pattern that should be used ?


What’s wrong with using YourApp.Repo.transaction? That’s how it’s supposed to be used.

rollback is meant to be used within the fun' passed to transaction to explicitly abort the transaction. Once transaction returns a value it was either committed or rolled back already. No need for futher action.

@ dimitarvp: Sorry, I was’t clear enough: I’m not using ecto here

@ LostKobrakai: Ok, I understand now.

Thanks to both