CFM Variable Area for ColdFusion

Earlier I asked a question about cf scopes on the cfm pages (happy that I understand the areas of CFC and potential problems), but it is still unclear in which field of variables.

In the answers to my previous question, it was suggested that there are no problems with thread safety using cfm pages and you will not get a scenario where two different users access the same page and have race conditions or thread safety problems (even if I just leave my variables in the cfm variable scope by default and that the variable scope for each user will be isolated and independent (here is my last question Lightening Coldfusion scopes )

However, I read this blog post http://blog.alexkyprianou.com/2010/09/20/variables-scope-in-coldfusion/ regarding the use of functions on the cfm page and the use of the variable area and it seems to offer a script according to by which the scope of variables is distributed between several users (I understand this problem in the context of CFCs - they are more similar to Java classes, and the scope of variables is instance variables, so thread safety problems are related to CFC / application scope / singleton), but it looks like previous answers - if the variable is placed function in the variable area on the cfm page can be accessed by other users, then, of course, the variables placed in the variable area directly in the cfm page code are the same?

I was hoping for some clear documents and manuals, but I really could not find a final explanation of the various areas and where they are available.

Thanks!

+7
source share
3 answers

Dan is right, and the blog article referenced in the question is simply incorrect. Dan's code demonstrates this, and I wrote and tested it completely on my blog (it was too big to go here).

The bottom line is the variable area in CFM, which is safe for this kind of race condition, because the variable area for each request is a different memory. Thus, one variables.foo does not coincide with another variables.foo , so never intersect.

The same applies to objects in the variable domain: their internal variable region is a separate object, so any number of queries can create a CFC instance in the query variable region, and CFC instance variable regions can all discrete objects.

The only time that the scope of variables can participate in a race state is the scope of variables of an object stored in a common area. Since all references to this object with common areas will refer to the same object in memory, therefore the same areas of variable objects in memory.

+12
source

Functions outside the CFC that access the variable scope will not have thread safety issues when 2 requests run the code, but if you use cfthread or other parallel functions, you may still have problems with the variable variable scope, and this can cause conditions race. Often this error can occur with a variable that you use a lot, for example, in a for loop, the variable "i".

for (i = 1; i <10; i ++) {t = arr [I]; }

But then another function does this during the first run:

for (i = 1; i <20; i ++) {t = arr [I]; }

The variable "i" must become a local variable to make it thread safe. You do not want the first cycle to go above 10 by mistake, and it is difficult to debug many times. I had to fix a ton of "i" variables and others to make my functions thread safe everywhere when I started caching objects and using cfthread more widely.

You can also avoid the need for locking by never changing existing objects. Instead, you can work on copies. This makes the data "unchanged." CFML does not have official support for creating immutable objects more efficiently, but you can make copies easily. http://en.wikipedia.org/wiki/Immutable_object

A simple example of securely changing the flow for a variable application area:

 var temp=structnew(); // build complete object temp.myValue=true; // set complete object to application scope variable application.myObject=temp; 

Writing to any shared object is often dangerous, because variables can be undefined or partially constructed. I always create a complete object and set it to a shared variable at the end, as in the example above. This facilitates thread safety if it is not too expensive to recreate data. The scope of variables in CFC is similar to a member variable in other languages. If you modify data in shared objects, you could use CFLOCK if you cannot make copies.

Some confusion in the field of cold resistance is associated with common areas in coldfusion 5 and previously less reliable. They had serious thread safety issues that could lead to data corruption or crashes. Under certain conditions, two streams could write to the same memory at the same time, if you did not block them correctly. Modern CFML engines can write to structural keys without the possibility of corruption / failure. You simply cannot be sure which data will actually end up as value without some consideration of thread safety now, but it will usually not be corrupted unless you deal with object types other than cfml, such as CFX, Java, and others The thread's security error can still lead to an infinite loop that can hang up the request until it expires, but it should not crash if it does not run out of memory.

+4
source

I think the blog is misleading. However, if you want to see for yourself, write a page with your function. Do it like that.

 <cffunction name="test" returntype="void"> <cfscript> foo = now(); sleep(3 * 60 * 1000); // should be 3 minutes writedump(foo); </cfscript> <cffunction> <cfdump var="#now()#"> <cfset test()> 

Launch the page. Within 3 minutes, open another browser or tab and start it again. Go back to where you ran it and wait for the results. If there is no significant difference between the two outputs, your second page request will not affect your first.

Note that I have not tried it myself, but my bid would have been on the second request, not affecting the first.

+3
source

All Articles