Skip to content

Commit

Permalink
use same txn for lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Hitenjain14 committed Aug 4, 2023
1 parent 3138346 commit 8176b42
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions code/go/0chain.net/blobbercore/allocation/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
func GetOrCreate(ctx context.Context, allocationId string) (*Allocation, error) {

db := datastore.GetStore().CreateTransaction(ctx)
tx := datastore.GetStore().GetTransaction(ctx)
// tx := datastore.GetStore().GetTransaction(ctx)

if len(allocationId) == 0 {
return nil, errors.Throw(constants.ErrInvalidParameter, "tx")
}

alloc, err := Repo.GetById(db, allocationId)
tx.Rollback()
// tx.Rollback()

if err == nil {
return alloc, nil
Expand Down
6 changes: 4 additions & 2 deletions code/go/0chain.net/blobbercore/handler/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func WithHandler(handler func(ctx *Context) (interface{}, error)) func(w http.Re
w.Header().Set("Content-Type", "application/json")

ctx, err := WithVerify(r)
txn := datastore.GetStore().GetTransaction(ctx)
statusCode := ctx.StatusCode

if err != nil {
Expand All @@ -155,6 +156,7 @@ func WithHandler(handler func(ctx *Context) (interface{}, error)) func(w http.Re
}

http.Error(w, err.Error(), statusCode)
txn.Rollback()
return
}

Expand All @@ -165,7 +167,7 @@ func WithHandler(handler func(ctx *Context) (interface{}, error)) func(w http.Re
if statusCode == 0 {
statusCode = http.StatusInternalServerError
}

txn.Rollback()
http.Error(w, err.Error(), statusCode)
return
}
Expand All @@ -174,7 +176,7 @@ func WithHandler(handler func(ctx *Context) (interface{}, error)) func(w http.Re
statusCode = http.StatusOK
}
w.WriteHeader(statusCode)

txn.Commit()
if result != nil {
json.NewEncoder(w).Encode(result) //nolint
}
Expand Down

0 comments on commit 8176b42

Please sign in to comment.