Search…
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.
QueryBuilder
1
query.from( "users" )
2
.where( "id", 1 )
3
.sharedLock();
Copied!
MySQL
SQL Server
Postgres
Oracle
1
SELECT *
2
FROM `users`
3
WHERE `id` = ?
4
LOCK IN SHARE MODE
Copied!
1
SELECT *
2
FROM [users] WITH (ROWLOCK,HOLDLOCK)
3
WHERE [id] = ?
Copied!
1
SELECT *
2
FROM "users"
3
WHERE "id" = ?
4
FOR SHARE
Copied!
1
LOCK TABLE "USERS"
2
IN SHARE MODE NOWAIT;
3
4
SELECT *
5
FROM "USERS"
6
WHERE "ID" = ?
Copied!

lockForUpdate

Name
Type
Required
Default
Description
No arguments
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.
QueryBuilder
1
query.from( "users" )
2
.where( "id", 1 )
3
.lockForUpdate();
Copied!
MySQL
SQL Server
Postgres
Oracle
1
SELECT *
2
FROM `users`
3
WHERE `id` = ?
4
FOR UPDATE
Copied!
1
SELECT *
2
FROM [users] WITH (ROWLOCK,UPDLOCK,HOLDLOCK)
3
WHERE [id] = ?
Copied!
1
SELECT *
2
FROM "users"
3
WHERE "id" = ?
4
FOR UPDATE
Copied!
1
LOCK TABLE "USERS"
2
IN ROW EXCLUSIVE MODE NOWAIT;
3
4
SELECT *
5
FROM "USERS"
6
WHERE "ID" = ?
Copied!

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.
QueryBuilder
1
query.from( "users" )
2
.where( "id", 1 )
3
.noLock();
Copied!
SQL Server
1
SELECT *
2
FROM [users] WITH (NOLOCK)
3
WHERE [id] = ?
Copied!

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.
Last modified 7mo ago
Copy link
Edit on GitHub