Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unnamed CFLOCKs

Status
Not open for further replies.

treadman

Programmer
May 15, 2007
3
US
Hi. I was recently hired to be the lead developer at a small web hosting company. Our server is Apache/1.3.34 (Unix) JRun/4.0. Coldfusion is CFMX7. Database is MySQL 4.1.20-standard.

I have verified that CFTRANSACTION is not supported by MySQL 4. Perhaps my predecessor realized this because these are wrapped in CFLOCKS.

The strange thing is the locks are neither named nor scoped. The typical insert code looks like this:

<cflock timeout="10">
<cftransaction>
<cfquery name="AddTo" datasource="#ds#">
INSERT INTO users ...
</cfquery>
<cfquery name="getID" datasource="#ds#">
SELECT Max(UserID) As MaxID
FROM users
</cfquery>
</cftransaction>
</cflock>

I verified that CFTRANSACTION doesn't work by altering the second query to fail. The INSERT happens even when the SELECT fails.

So what about that CFLOCK? Does the server just ignore it?
 
ColdFusion does allow unnamed locks, so it won't be ignored. But unnamed locks are discouraged in the documentation.

I don't use MySql. But at a guess I'd say the code is to trying to ensure the select statement returns the UserID inserted by the current request (and not some other request that happened to occur at the same time.) A brief google returned a number of results on the topic:

PS
I thought MySql 4+ did support transactions?
 
Thanks for the info on getting the latest auto-incremented ID. There are several good ways to do this. The thing I'd like to know is what does an unnamed lock actually do. My guess is that all unnamed locks in the app are locked. In other words, the lock is "named" [NULL STRING].

I think MySQL began supporting transactions for version 5. I tested my version by simply garbaging the tablename in the second query:

<cflock timeout="10">
<cftransaction>
<cfquery name="AddTo" datasource="#ds#">
INSERT INTO users ...
</cfquery>
<cfquery name="getID" datasource="#ds#">
SELECT Max(UserID) As MaxID
FROM usalksdfjers
</cfquery>
</cftransaction>
</cflock>

I got an error saying that the table "usalksdfjers" does not exist. But the INSERT did happen.
 
The thing I'd like to know is what does an unnamed lock actually do. My guess is that all unnamed locks in the app are locked. In other words, the lock is "named" [NULL STRING].

No I don't think so. Based on the documenation, I think each unnamed lock is a unique lock.

If you omit the identifier, the lock is anonymous and you cannot synchronize the code in the cflock tag block with any other code.

If that weren't the case, I think you'd notice ... because it would essentially single thread your application if it contains a lot of unnamed locks.

Apparently the INNODB model supports transactions
 
Livedocs says "... the lock is anonymous and you cannot synchronize the code in the cflock block with any other code."

So the server will not run "any other code" (for all users and apps on that server) while the the anonymous lock is in place? I'm confused about the meaning of "synchronize" in this context. Would it be the same to say "code in the cflock block will not run concurrently with any other code"?
 
Synchronize is more like "only one thread will be able to run the code (inside the cflock tags) at one time".

So what they're saying is "if you want to synchronize code, you must use a named locked. You cannot do it using an unnamed lock".
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top