MaybeT and transactions in runDb

In my previous question about chain failures, Michael Sneiman suggested that I use MaybeTto run them, so if any of them fail, he just closes on Nothing.

I'm impressed with runDbeverything in a transaction. So, should a failure at some point in the code automatically cancel the transaction?

mauth <- runDb $ runMaybeT $ do
            valid    <- MaybeT $ return $ listToMaybe errs 
            uid      <- MaybeT $ insertUnique u 
            vid     <- MaybeT $ getBy $ UniqueField v -- this step fails but previous insert does not roll back
            auth     <- liftIO $ createAuthToken uid
            return auth

When I run the above code, it getByfails, but the user is still inserted. I don’t understand what runDbwill roll back on the Nothinginside MaybeT? Do I need to use some other Monad for this?

Evaluate your thoughts on how best to roll back on a crash.

Update : This is what I did at the suggestion of Michael.

mauth <- runDb $ do
          ma <- runMaybeT $ do
                   valid <- ... 
          case ma of
            Just _ -> return ma
            Nothing -> liftIO $ throwIO MyException

, .

!

+4
1

Nothing - , . ( - throwIO) Persistent, .

+3

All Articles