Skip to main content
Known Participant
October 6, 2009
Question

DB Locking and ColdFusion

  • October 6, 2009
  • 2 replies
  • 1268 views

Hi

I need to read a 1 row control table to get the next account number to assign (MS SQL Server). I then will insert a row to a Customer table using the next available account number.

The problem is if 2 threads/sessions read the Control table at the same time. The 1st to do the insert will be OK but the next will fail.

I found 3 solutions (1 SQL Server and 2 CF) but I'm not sure which to use :

1) T-SQL Update Intent :

It seems you can read a row with update intent :
SELECT * from CONTROLTBL (UPDLOCK)
--
Insert your row
--
UPDATE CONTROLTBL
SET Counter = counter +1

2) There is a CFTRANSACTION tag that is used to group a series of updates and roll back if any one of them fails. But is has an option of :
<CFTRANSACTION isolation="serializable">
Read and update
</CFTRANSACTION>

3) The last is a CFLOCK tag that will prevent any thread from proceeding
<cflock scope = "Application" name = "newaccount" type = "exclusive "> <!--- CFML to be synchronized --->
</cflock>

Any suggestions?
Thanks in advance,

hefterr

This topic has been closed for replies.

2 replies

Inspiring
October 7, 2009

My suggestion is to drop the control table and use this subquery (select max(id) + 1 from customer), in your insert query.  Better yet, make the field an autoincrement.

Inspiring
October 6, 2009

I'd use the CFTRANSACTION tag to wrap your SQL statements.   Using CFLOCK only isolates CF code not other applications that might query your database. Take a look at the descriptions of the isolation levels for SQL server.  Based on your requirements you *may* be able to use read committed rather then serializable.

Info on isolation levels in SQL Server

http://msdn.microsoft.com/en-us/library/ms173763.aspx