Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Logging Table Lock Help

$
0
0

I need some helping formulating a strategy to fix an issue with the same log ID being assigned to multiple user imports.  In my web application users can upload files.  Every import is logged in a table with a single ID and entry.  Each import has a log and set or validations.  I currently have a log table and a validation table.  Each import is associated to the log and validation ID.  A single import can somtime be broken into multiple import entries but we still want each entry to be assigned to the same log since they were part of the same file.

Things to keep in mind:

1. An import file can create multiple entries in the imports database table

2. Each import entry create from a file should be assigned the same log ID

3. I cannot use an identity field because I need to be able to link a single import entry to multiple log entries.

The problem I am having is how to assign the log ID exclusively.  Currently I am using the following:

DECLARE @LogID int
SELECT @LogID = ISNULL(MAX(LOG_ID), 0) + 1
FROM [LOG] WITH(TABLOCKX)
INSERT INTO [LOG] VALUES (@LogID, 'Starting Message')
RETURN @LogID

This procedure works good and can be called from each of the import stored procedures.  The problem is that some log entries are getting crossed between imports.  While I may have the table lock on the select statement if another user is waiting for the lock it will process the select statement next and have the same log ID before the insert statement can be executed.

Requirements:

1. Be able to select the next available log entry and insert the first value into the table in order to reserve the ID before other users can potentially get the same log ID

2. Be able to return the log ID value back from the stored procedure

Suggestions?


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>