Il giorno martedì 7 febbraio 2017 03:57:25 UTC+1, Steve Roth ha scritto: > > I'd like input on whether the following idea is good or bad, and why. > > Consider an abstract transaction, modeled as follows: > func Transaction(ctx context.Context, body func(ctx context.Context) > error) error >
Is Transaction a method of your database connection type? > func OnCommit(ctx context.Context, commitHook func(ctx context.Context)) > func OnRollback(ctx context.Context, rollbackHook func(ctx > context.Context)) > > The code that wishes to execute a transaction would call Transaction and > provide the body of the transaction as a function parameter. If that > function returns an error, the transaction is rolled back, by calling any > hooks registered during that transaction by calling OnRollback. If the > body function returns success, the transaction is committed by calling any > hooks registered during that transaction by calling OnCommit. > > The implication of this model is that the identity of what transaction > you're in is stored in the context. The ctx passed into body has that > value added to it, and the OnCommit and OnRollback calls associate the > hooks with the transaction they find in the context. > > Of course the same thing could be done by passing the transaction > explicitly, e.g. > func Transaction(ctx context.Context, body func(ctx context.Context, tx > Tx) error) error > There is one problem here. Suppose you have a function that needs to query the database. This function *should* work both when in auto commit mode or when inside a transaction. However sql.DB and sql.Tx are concrete types, so you can not pass both of them to the function. > [...] Manlio -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
