Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

SQL Server error 1204: The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

A query fails with error 1204, and the full text reads: "The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time. Rerun your statement when there are fewer active users. Ask the database administrator to check the lock and memory configuration for this instance, or to check for long-running transactions." The message tells you to rerun the statement when there are fewer active users, but a rerun only helps if the cause is temporary.

This is a shortage of lock structures, not a blocking problem. Work through the read-only checks first. Nothing before the Fix section changes the server.

What the error means

Queries acquire and release locks as they run. Each lock uses a structure from a pool. Microsoft's page for error 1204 says that when "there are no more lock structures available in the pool, the error 1204 message is returned." The same page lists four reasons.

The Microsoft page for the locks option adds the background. By default, locks is 0, which lets the Database Engine allocate and deallocate lock structures dynamically. The lock manager starts with an initial pool of 2,500 lock structures and acquires more memory as the pool is used. That dynamic pool "doesn't acquire more than 60 percent of the memory allocated to the Database Engine". Once it reaches that point, or the computer has no more memory, further requests for locks generate an error.

Likely causes

Cause What it looks like How to confirm
SQL Server cannot get more memory Other processes hold the memory, or SQL Server has reached max server memory Memory clerk query and the configured limit, below
The lock manager has used 60 percent of the memory available to SQL Server One session or a few sessions hold a very large number of locks Lock count per session, below
locks set to a non-default, non-dynamic value The setting is not 0 Configured and running value of locks, below
Trace flag 1211, 1224 or both enabled Queries that need many locks fail while the flags are on Trace flag status, below

The error page also names one situation outside the instance: applications other than SQL Server using memory on the same server.

Prove the cause before changing anything

All of these only read.

  1. Find out whether another memory clerk holds a large share of SQL Server's memory. This is the query on Microsoft's error page:
SELECT pages_kb, type, name, virtual_memory_committed_kb, awe_allocated_kb
FROM sys.dm_os_memory_clerks
ORDER BY pages_kb DESC;
  1. Find the session that holds the most locks. This is also the query on Microsoft's error page:
SELECT request_session_id, COUNT (*) num_locks
FROM sys.dm_tran_locks
GROUP BY request_session_id
ORDER BY count (*) DESC;
  1. Check the memory limit. Microsoft's memory page gives this query, which "returns results regardless of whether the sp_configure option 'show advanced options' is enabled":
SELECT [name],
       [value],
       [value_in_use]
FROM sys.configurations
WHERE [name] = 'max server memory (MB)'
      OR [name] = 'min server memory (MB)';
  1. Check the locks option. This is the same query with the filter changed to the option name, so it is an adaptation of the page's query rather than a statement on it:
SELECT [name], [value], [value_in_use]
FROM sys.configurations
WHERE [name] = 'locks';

A value_in_use other than 0 means the option has been set. Microsoft's locks page says that when it is not 0, "the Database Engine can't allocate more locks than the value specified in locks".

  1. Check whether trace flags are active. Microsoft's trace flags page says: "Use the DBCC TRACESTATUS command to determine which trace flags are currently active."
DBCC TRACESTATUS (1211, 1224);

Microsoft's trace flag page says trace flag 1211 "Disables lock escalation based on memory pressure, or based on number of locks", and that using it "can generate excessive number of locks" and "cause error message 1204 (unable to allocate lock resource) because of insufficient memory". Trace flag 1224 disables escalation based on the number of locks, while memory pressure can still trigger it. The page says that if both are set, 1211 takes precedence, and to use 1224 instead of 1211.

Example (illustrative): a hosted server sql01 runs an accounting database and an import job. Error 1204 appears only while the nightly import runs. The second query shows one session with several million rows in sys.dm_tran_locks, and locks is 0. That matches the second cause on Microsoft's page: one transaction is holding most of the locks.

Fix

Match the fix to the cause you proved. Microsoft's page gives one action for each.

Cause: SQL Server cannot get more memory

If another memory clerk holds a large part of the configured memory, Microsoft says to reduce that clerk's consumption so lock memory can use more. If other applications are using memory, the page says to stop them or run them on a separate server.

If max server memory is set and is the limit, the page says to increase it. Before you change it, save the current values by running this query and writing them down.

SELECT [name],
       [value],
       [value_in_use]
FROM sys.configurations
WHERE [name] = 'max server memory (MB)'
      OR [name] = 'min server memory (MB)';

Risk: The next statement changes the maximum memory SQL Server may use. Microsoft's memory page says that a value set too high can make SQL Server compete with other instances on the same host, and that the setting takes effect immediately without a restart.

Set the new limit with the statements below. The figure 12288 is Microsoft's example value on its memory page, not a recommendation for your server. Work out your own figure from the page's guidance on leaving memory for the operating system.

EXECUTE sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXECUTE sp_configure 'max server memory', 12288;
GO
RECONFIGURE;
GO

To undo it: run the same statements with the original max server memory value you saved from the query in step 3.

Cause: one transaction holds most of the locks

Microsoft's page says to identify the transaction holding the most locks and terminate it. Take the session ID from step 2. Before you end it, confirm what that session is doing and who owns it, using the query on Microsoft's KILL page:

SELECT conn.session_id, host_name, program_name,
     nt_domain, login_name, connect_time, last_request_end_time
FROM sys.dm_exec_sessions AS sess
JOIN sys.dm_exec_connections AS conn
    ON sess.session_id = conn.session_id;

Save first. Write down the session ID, its login, host name and program name from the queries above, and its lock count, so that its owner knows what was ended and can repeat the lost work.

Risk: The next statement ends one user session. Microsoft's KILL page says that if the session "has much work to undo, the KILL statement can take some time to complete", particularly when it must roll back a long transaction. Work that was not committed is lost.

Replace the placeholder with the session ID you identified and that you have confirmed belongs to the runaway transaction; the statement fails as written until you do. Do not end a system process.

KILL <session_id>;

To undo it: a killed session cannot be brought back, and the rollback cannot be reversed. The application or user must reconnect and repeat the work that was rolled back. To watch the rollback, Microsoft's page gives KILL <session_id> WITH STATUSONLY;, which reports progress and does not end anything.

Cause: locks set to a fixed value

Microsoft's error page says to return locks to its default. Its locks page shows the full sequence and adds that "The server must be restarted before the setting can take effect." Save first. Note the current value by running this query:

SELECT [name], [value], [value_in_use]
FROM sys.configurations
WHERE [name] = 'locks';

Disruptive step: The next statements set locks back to 0 and then need a restart of the SQL Server service before they take effect. While the service restarts, nobody can use that instance, so choose a time when that is acceptable.

EXECUTE sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXECUTE sp_configure 'locks', 0;
GO
RECONFIGURE;
GO

Restart the SQL Server service in a maintenance window.

To undo it: run the same statements with the original locks value you saved, and restart the service again.

Cause: trace flags 1211 or 1224

Microsoft's page says to check their use and disable them while executing queries that need a large number of locks. The trace flags page says a global flag turned on with DBCC TRACEON (2528, -1) style syntax "is lost on server restart", and that a flag enabled by the -T startup option stays active after a restart. Which method was used decides how you turn it off. Microsoft's page says to turn off a global flag with DBCC TRACEOFF and the -1 argument.

Save first. Record which flags are active and how they are set by running this statement:

DBCC TRACESTATUS (1211, 1224);

Risk: The next statement turns off trace flag 1211 server-wide. Lock escalation will then occur again, which changes how locks are held and can change blocking behaviour for every workload on the instance. If the flag was set deliberately by a vendor, check with them first.

DBCC TRACEOFF (1211, -1);

To undo it: run DBCC TRACEON (1211, -1);. This lasts until a restart. If the flag came from a -T1211 startup option, removing it from the startup parameters is a separate change that Microsoft's page describes only as the -T option.

Confirm the fix

  • The statement that failed now completes.
  • Re-run the lock count query from step 2 during the busy period. No single session should hold an extreme share of the locks.
  • Re-run the queries from steps 3 to 5. They show the values you intended.

What to watch afterwards

  • The same error at the same time each day points to a scheduled job that holds many locks.
  • Whether memory use by other applications on the server grows. The error page lists other processes using memory as a cause.
  • Any later change to max server memory or to startup trace flags.

Risks and limits

These steps have not been run or lab-tested. Microsoft's error page gives no version range beyond "SQL Server". Figures such as the 60 percent limit and the 2,500-lock initial pool come from the locks page, which also says the option "will be removed in a future version of SQL Server". The example value 12288 is Microsoft's, not advice for your server. Microsoft's pages do not say how many locks a given transaction needs, so no threshold for "too many" is given.

Sources