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

What's the recommend way to use transactions? #541

Open
ifyouseewendy opened this issue Jul 10, 2019 · 3 comments
Open

What's the recommend way to use transactions? #541

ifyouseewendy opened this issue Jul 10, 2019 · 3 comments
Labels

Comments

@ifyouseewendy
Copy link

👋 I've searched around the code base. It seems the transaction support is removed from hanami model. Hanami::Model.container.gateways[:default].connection seems to be a Sequel::Mysql2::Database which I could call transaction on it. May I ask if there is a recommended way to do it in hanami model? Thanks!

@marfoldi
Copy link

👋

class ArticleRepository < Hanami::Repository
  def method
    transaction do
      # ...
    end
  end
end

@monicao
Copy link

monicao commented Dec 30, 2021

Thanks @marfoldi . How about if you need to write records to two or more separate tables in a transaction?

For example, you have some User data and some UserAddress data that needs to be persisted in an atomic way.

@marfoldi
Copy link

marfoldi commented Jan 2, 2022

For example, you have some User data and some UserAddress data that needs to be persisted in an atomic way.

Maybe something like:

def update_user(user_repository: UserRepository.new, user_address_repository: UserAddressRepository.new)
  user_repository.transaction do
    user_repository.update(...)
    user_address_repository.update(...)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants