Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ func runTransactionWithOptions(ctx context.Context, db *sql.DB, opts *sql.TxOpti
return err
}
for {
err = f(ctx, tx)
err = protected(ctx, tx, f)
errDuringCommit := false
if err == nil {
err = tx.Commit()
Expand Down Expand Up @@ -742,6 +742,15 @@ func runTransactionWithOptions(ctx context.Context, db *sql.DB, opts *sql.TxOpti
}
}
}

}
func protected(ctx context.Context, tx *sql.Tx, f func(ctx context.Context, tx *sql.Tx) error) (err error) {
defer func() {
if x := recover(); x != nil {
err = spanner.ToSpannerError(status.Errorf(codes.Unknown, "transaction function panic: %v", x))
}
}()
return f(ctx, tx)
}

func resetTransactionForRetry(ctx context.Context, conn *sql.Conn, errDuringCommit bool) error {
Expand Down
15 changes: 15 additions & 0 deletions driver_with_mockserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4227,6 +4227,21 @@ func TestRunTransactionCommitError(t *testing.T) {
}
}

func TestRunTransactionPanics(t *testing.T) {
t.Parallel()

ctx := context.Background()
db, _, teardown := setupTestDBConnection(t)
defer teardown()

err := RunTransaction(ctx, db, nil, func(ctx context.Context, tx *sql.Tx) error {
panic(nil)
})
if err == nil {
t.Fatal("missing error from transaction runner")
}
}

func TestTransactionWithLevelDisableRetryAborts(t *testing.T) {
t.Parallel()

Expand Down
Loading