While moving a workload from server to AWS Batch (Python), we hit a strange issue:
- Some SQL jobs started taking 2x longer
- Parallel jobs were getting blocked / waiting
- No code changes in SQL
After debugging, the root cause turned out to be:
π autocommit = false in pymssql
π§© The Setup
From Python, we were executing a group of SQL statements in one go (sample):
DECLARE @jobId INT;
EXEC @jobId = dbo.ProcessJob @input = 123;
IF (@jobId > 0)
PRINT 'Job processed';
ELSE
PRINT 'Job failed';
Inside the stored procedure (sample):
CREATE PROCEDURE dbo.ProcessJob
@input INT
AS
BEGIN
BEGIN TRAN;
INSERT INTO Jobs (input) VALUES (@input);
UPDATE JobStatus
SET status = 'PROCESSING'
WHERE job_id = @input;
SELECT * FROM Jobs WHERE input = @input;
COMMIT TRAN;
RETURN @input;
END
So overall:
- Python β executes batch SQL
- Stored Procedure β has its own transaction
β οΈ The Problem
Even though the stored procedure had COMMIT, we saw:
- Other queries getting blocked
- Jobs stuck in waiting state
- Execution time increasing
π What Was Actually Happening
Python connection was:
conn = pymssql.connect(..., autocommit=False)
#This created a parent transaction.
π₯ Locking Flow
- Python opens connection β starts transaction
- Stored procedure runs:
- BEGIN TRAN β does INSERT/UPDATE
- COMMIT β completes inner transaction
- But outer transaction (Python) is still open
Result:
- SQL Server does NOT release locks
- Rows/tables remain locked until Python commits
- Other queries trying to access same tables β WAIT
π Example Lock Scenario
Job 1 (Running)
UPDATE JobStatus SET status = 'PROCESSING' WHERE job_id = 123;
β‘οΈ Lock acquired on JobStatus table
Job 2 (Parallel)
UPDATE JobStatus SET status = 'DONE' WHERE job_id = 123;
β Blocked because:
Job 1 still holds lock (due to outer transaction)
π Even though SP committed, lock is held by parent transaction
π οΈ The Fix
Changed to:
conn = pymssql.connect(..., autocommit=True)
What changed?
- No parent transaction from Python
- Stored procedure controls its own transactions
- Locks released immediately after COMMIT
β Result
- Execution time back to normal
- No blocking between jobs
- Smooth parallel execution
π§ Key Takeaway
-Even if your stored procedure commits,
locks wonβt release if thereβs an outer transaction still open.
π― When This Can Happen
- Running parallel database operations
- Using pymssql / drivers with autocommit off
- Working with transaction-heavy stored procedures
π Final Thought
This issue wasnβt about query complexity β it was about transaction boundaries.
A small config like autocommit can silently:
- Increase execution time
- Cause blocking
- Impact system performance
β Lokesh Vangari
Top comments (0)