#1514·quartz

MSSQL Frequent deadlocks on QRTZ_SCHEDULER_STATE due to datatype mismatch

Author: terinvargheseCreated Aug 25, 2026Updated Aug 25, 2026

Problem statement and effect

The INSTANCE_NAME column is defined as NVARCHAR(200) in the mssql database. While the calls from quartz scheduler library (jar file) passes the value of INSTANCE_NAME as VARCHAR(8000). As a result the primary key (PK_QRTZ_SCHEDULER_STATE) cannot be used by the update query since the data types do not match. This results in deadlocks when multiple instances of the application tries to use the table.

UPDATE .QRTZ_SCHEDULER_STATE
SET LAST_CHECKIN_TIME = @P0
WHERE SCHED_NAME = 'ENE_DataIncrementScheduler'
	AND INSTANCE_NAME = @P1 --column definition NVARCHAR(200)

Suggested solution

The query is defined as value for the variable UPDATE_SCHEDULER_STATE in StdJDBCConstants. The value for INSTANCE_NAME is set in StdJDBCDelegate using PreparedStatement.setString method which results in the value being translated as VARCHAR once it reaches the database query execution. Instead INSTANCE_NAME should be set in StdJDBCDelegate using PreparedStatement.setNString method which results in value being translated as NVARCHAR once it reaches the database query execution.

If the fix affects databases or than MSSQL then the StdJDBCDelegate.updateSchedulerState() method should be overridden in MSSQLDelegate class so that changes are specific for MSSQL query executions while other database query executions remain untouched.