ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old October 29th, 2003, 06:16 PM
dirtybbq dirtybbq is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 53 dirtybbq User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 17 m
Reputation Power: 6
cflock question

If I have a registration form where a new record is inserted into one table and then want to select that new record's id using a select max(id) query, can I surround my code with cflock to ensure that the max(id) selected is actually the id of the record just inserted? Will the cflock prevent other potential users from inserting a new record until the previous record is inserted and it's id is selected?

ex.

Code:
<cflock timeout="60" throwontimeout="yes" type="exclusive" scope="application">

<!--- insert new record --->

<!--- select max(id)... --->

</cflock>

Reply With Quote
  #2  
Old November 5th, 2003, 05:47 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,627 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 8 m 55 sec
Reputation Power: 53
I think if you combine the lock with a CFTRANSACTION block it may guarantee that the max ID selected by the second query is in fact the record inserted by the first one, but I'm not 100% sure. In any event, this is messy...I'd do it on the database side if possible via a stored procedure. By the way this is really a database contention issue and not a ColdFusion-specific issue.

Regards.

Reply With Quote
  #3  
Old November 19th, 2003, 07:20 PM
cliffyman cliffyman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Washington DC, USA
Posts: 156 cliffyman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 7 sec
Reputation Power: 8
Send a message via AIM to cliffyman
I would avoid putting a CFQUERY (or any other "time consuming process") inside of a CFLOCK tag. You'll likely grind your app server to a halt under any kind of load. The best way to handle it is as kiteless suggests with the CFTRANSACTION tag. What database are you using?

Reply With Quote
  #4  
Old November 27th, 2003, 11:01 AM
highwaysjammed4 highwaysjammed4 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 4 highwaysjammed4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
maybe I'm old fashioned but my preferred method to get the id of a record that was just inserted is to

1. when the data is first being inserted, insert a unique identifier along with the other data (could use the cf rand function)

then

2. select the row containing the unique identifier

selecting (max) could give you the worng record if the db is updated in that moment between transactions.

Reply With Quote
  #5  
Old November 28th, 2003, 10:04 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 21 m 17 sec
Reputation Power: 891
i'd like to second highwaysjammed4's approach

when you think about it, an auto_increment or identity field is a surrogate key, and the table will likely have some other column(s) which are the "real" primary (i.e. unique) key

simply retain those column values after the INSERT and then query the row back in a subsequent SELECT

locking is not required, so this approach works really well in a heavy load and/or multi-threaded app

it's usually not necessary to have a separate unique identifier as well

rudy
http://r937.com/

Reply With Quote
  #6  
Old December 4th, 2003, 10:06 PM
dirtybbq dirtybbq is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 53 dirtybbq User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 17 m
Reputation Power: 6
thanks

just wanted to say thanks for all your help and advice.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > cflock question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway