Skip to content

Commit

Permalink
Refactor NewTx and NewCachedTx - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed Aug 28, 2024
1 parent 1f44abe commit 3957352
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cmd/soroban-rpc/internal/db/ledgerentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,6 @@ func (r ledgerEntryReader) GetLatestLedgerSequence(ctx context.Context) (uint32,

func (r ledgerEntryReader) newBaseTx(ctx context.Context) (db.SessionInterface, error) {
txSession := r.db.Clone()
r.db.cache.RLock()
defer r.db.cache.RUnlock()
if err := txSession.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}); err != nil {
return nil, err
}
Expand All @@ -362,6 +360,8 @@ func (r ledgerEntryReader) NewCachedTx(ctx context.Context) (LedgerEntryReadTx,
// We need to make the parent cache access atomic with the read transaction creation.
// Otherwise, the cache can be made inconsistent if a write transaction finishes
// in between, updating the cache.
r.db.cache.RLock()
defer r.db.cache.RUnlock()
txSession, err := r.newBaseTx(ctx)
if err != nil {
return nil, err
Expand All @@ -379,6 +379,8 @@ func (r ledgerEntryReader) NewCachedTx(ctx context.Context) (LedgerEntryReadTx,
}

func (r ledgerEntryReader) NewTx(ctx context.Context) (LedgerEntryReadTx, error) {
r.db.cache.RLock()
defer r.db.cache.RUnlock()
txSession, err := r.newBaseTx(ctx)
if err != nil {
return nil, err
Expand Down

0 comments on commit 3957352

Please sign in to comment.