メインコンテンツへスキップする

トランザクション

トランザクションの開始

// GenTxは、1つのトランザクションでエンティティのグループを生成します。
func GenTx(ctx context.Context, client *ent.Client) error {
tx, err := client.Tx(ctx)
if err != nil {
return fmt.Errorf("starting a transaction: %w", err)
}
hub, err := tx.Group.
Create().
SetName("Github").
Save(ctx)
if err != nil {
return rollback(tx, fmt.Errorf("failed creating the group: %w", err))
}
// グループの管理者を作成
dan, err := tx.User.
Create().
SetAge(29).
SetName("Dan").
AddManage(hub).
Save(ctx)
if err != nil {
return rollback(tx, err)
}
// "Ariel"というユーザーを作成
a8m, err := tx.User.
Create().
SetAge(30).
SetName("Ariel").
AddGroups(hub).
AddFriends(dan).
Save(ctx)
if err != nil {
return rollback(tx, err)
}
fmt.Println(a8m)
// Output:
// User(id=2, age=30, name=Ariel)

// トランザクションをコミット
return tx.Commit()
}

// rollbackはtx.Rollbackを呼び出し、
// エラーが発生した場合はそれをロールバックエラーでラップします
func rollback(tx *ent.Tx, err error) error {
if rerr := tx.Rollback(); rerr != nil {
err = fmt.Errorf("%w: %v", err, rerr)
}
return err
}

You must call Unwrap() if you are querying edges off of a created entity after a successful transaction (example: a8m.QueryGroups()). Unwrap restores the state of the underlying client embedded within the entity to a non-transactable version.

Note Calling Unwrap() on a non-transactional entity (i.e., after a transaction has been committed or rolled back) will cause a panic. :::

The full example exists in GitHub.

トランザクションクライアント

Sometimes, you have an existing code that already works with *ent.Client, and you want to change it (or wrap it) to interact with transactions. For these use cases, you have a transactional client. An *ent.Client that you can get from an existing transaction.

// WrapGen はトランザクション内の既存の "Gen" 関数をラップします。
func WrapGen(ctx context.Context, client *ent.Client) error {
tx, err := client.Tx(ctx)
if err != nil {
return err
}
txClient := tx.Client()
// 以下の"Gen"を使用し、トランザクションクライアントを与えます。"Gen"のコード変更は必要ありません
if err := Gen(ctx, txClient); err != nil {
return rollback(tx, err)
}
return tx.Commit()
}

// Gen は、エンティティのグループを生成します。
func Gen(ctx context.Context, client *ent.Client) error {
// ...
return nil
}

The full example exists in GitHub.

ベストプラクティス

Reusable function that runs callbacks in a transaction:

func WithTx(ctx context.Context, client *ent.Client, fn func(tx *ent.Tx) error) error {
tx, err := client.Tx(ctx)
if err != nil {
return err
}
defer func() {
if v := recover(); v != nil {
tx.Rollback()
panic(v)
}
}()
if err := fn(tx); err != nil {
if rerr := tx.Rollback(); rerr != nil {
err = fmt.Errorf("%w: rolling back transaction: %v", err, rerr)
}
return err
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("committing transaction: %w", err)
}
return nil
}

Its usage:

func Do(ctx context.Context, client *ent.Client) {
// WithTx helper.
if err := WithTx(ctx, client, func(tx *ent.Tx) error {
return Gen(ctx, tx.Client())
}); err != nil {
log.Fatal(err)
}
}

フック

Same as schema hooks and runtime hooks, hooks can be registered on active transactions, and will be executed on Tx.Commit or Tx.Rollback:

func Do(ctx context.Context, client *ent.Client) error {
tx, err := client.Tx(ctx)
if err != nil {
return err
}
// Tx.Commitにフックを追加します
tx.OnCommit(func(next ent.Committer) ent.Committer {
return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error {
// 実際にコミットする前のコード
err := next.Commit(ctx, tx)
// トランザクションがコミットされた後のコード
return err
})
})
// Tx.Rollbackにフックを追加します
tx.OnRollback(func(next ent.Rollbacker) ent.Rollbacker {
return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error {
// 実際にロールバックする前のコード
err := next.Rollback(ctx, tx)
// トランザクションがロールバックされた後のコード
return err
})
})
//
// <Code goes here>
//
return err
}

Isolation Levels

Some drivers support tweaking a transaction's isolation level. For example, with the sql driver, you can do so with the BeginTx method.

tx, err := client.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead})