LogoLogo
9.5.0
9.5.0
  • Introduction
  • What's New?
  • Installation & Usage
  • Migration Guide
  • Contributing & Filing Issues
  • Query Builder
    • Getting a New Query
    • Building Queries
      • Selects
      • From
      • Joins
      • Wheres
      • Order By
      • Group By and Having
      • Limit, Offset, and Pagination
      • Locks
      • Unions
      • Common Table Expressions (i.e. CTEs)
      • Raw Expressions
      • When / Conditionals
      • Query Parameters and Bindings
    • Executing Queries
      • Retrieving Results
      • Aggregates
      • Inserts, Updates, and Deletes
    • Options and Utilities
      • Query Options and Utilities
      • Clone and Reset
      • Return Format
      • Column Formatter
      • Interception Points
    • Debugging
      • sqlCommenter
  • Schema Builder
    • Overview
    • Create
    • Columns
    • Column Modifiers
    • Column Constraints
    • Creating Table Constraints
    • Alter
    • Drop
    • Debugging
  • External Links
    • API Docs
    • Source Code
    • Issue Tracker
Powered by GitBook
On this page
  • sharedLock
  • lockForUpdate
  • noLock
  • lock
  • clearLock

Was this helpful?

Edit on GitHub
Export as PDF
  1. Query Builder
  2. Building Queries

Locks

qb includes a few methods to help you lock certain rows when executing select statements.

Note: For locks to work properly, they must be nested inside a transaction. qb does not handle any of the transaction lifecycle for you.

sharedLock

Name

Type

Required

Default

Description

No arguments

A shared lock prevents the selected rows from being modified until your transaction is committed.

query.from( "users" )
    .where( "id", 1 )
    .sharedLock();
SELECT *
FROM `users`
WHERE `id` = ?
LOCK IN SHARE MODE
SELECT *
FROM [users] WITH (ROWLOCK,HOLDLOCK)
WHERE [id] = ?
SELECT *
FROM "users"
WHERE "id" = ?
FOR SHARE
LOCK TABLE "USERS"
IN SHARE MODE NOWAIT;

SELECT *
FROM "USERS"
WHERE "ID" = ?

lockForUpdate

Name
Type
Required
Default
Description

skipLocked

Boolean

false

false

A lock for update lock prevents the selected rows from being modified or selected with another shared lock until your transaction is committed.

The main difference between a sharedLock and lockForUpdate is that a lockForUpdate prevents other reads or selects as well as updates.

query.from( "users" )
    .where( "id", 1 )
    .lockForUpdate();
SELECT *
FROM `users`
WHERE `id` = ?
FOR UPDATE
SELECT *
FROM [users] WITH (ROWLOCK,UPDLOCK,HOLDLOCK)
WHERE [id] = ?
SELECT *
FROM "users"
WHERE "id" = ?
FOR UPDATE
SELECT *
FROM "USERS"
WHERE "ID" = ?
FOR UPDATE

When using the skipLocked flag, the query will skip over locked records and only return and lock available records.

query.from( "users" )
    .where( "id", 1 )
    .lockForUpdate( skipLocked = true )
    .orderBy( "id" )
    .limit( 5 );
SELECT *
FROM `users`
WHERE `id` = ?
ORDER BY `id`
LIMIT 5
FOR UPDATE SKIP LOCKED
SELECT TOP 5 *
FROM [users] WITH (ROWLOCK,UPDLOCK,HOLDLOCK,READPAST)
WHERE [id] = ?
ORDER BY [id]
SELECT *
FROM "users"
WHERE "id" = ?
ORDER BY "id"
LIMIT 1
FOR UPDATE SKIP LOCKED

noLock

Name

Type

Required

Default

Description

No arguments

noLock will instruct your grammar to ignore any shared locks when executing the query.

Currently this only makes a difference in SQL Server grammars.

query.from( "users" )
    .where( "id", 1 )
    .noLock();
SELECT *
FROM [users] WITH (NOLOCK)
WHERE [id] = ?

lock

Name

Type

Required

Default

Description

value

string

true

The custom lock directive to add to the query.

The lock method will allow you to add a custom lock directive to your query. Think of it as the raw method for lock directives.

These lock directives vary from grammar to grammar.

clearLock

Name

Type

Required

Default

Description

No arguments

Clears any lock directive on the query.

PreviousLimit, Offset, and PaginationNextUnions

Was this helpful?