Skip to main content
December 18, 2015
Question

cfthread in cfloop, all variables passed in on the thread, intermittent java.util.concurrentmodificationexception. cfloop is a query loop and each thread calls cfquery. How do I find the error?

  • December 18, 2015
  • 2 replies
  • 1875 views

Code

Below is the script, the queries in the thread only use the variables on the cfthread call. The process works about 80% of the time and then I receive Java.util.concurrentmodificationexception on one of the threads, occasionally more than one.  Using CF 9,0,0,251028, but have tested with 9,0,2,282541 with the same issue.

Any hekp is appreciated

<cfquery name = "variables.Dts" datasource="#mydsn#">

select 

to_char(next_day(to_date('#selDate#','mm/dd/yyyy')-56,'sat')+(level-1)*7-2,'mm/dd/yyyy') as thu,

to_char(next_day(to_date('#selDate#','mm/dd/yyyy')-56,'sat')+(level-1)*7-1,'mm/dd/yyyy') as fri,

to_char(next_day(to_date('#selDate#','mm/dd/yyyy')-56,'sat')+(level-1)*7,'mm/dd/yyyy') as sat

from dual

connect by  level <  9

</cfquery>

<!--- test --->

<cfset qtr = 0>

<cfset tname = ''>

<cfloop query="variables.dts" >

<cfset qtr += 1>

<cfthread name="t#qtr#" action="run" sat2='#variables.dts.sat#' thu2='#variables.dts.thu#' selarea2='#selArea#' selDistrict2='#seldistrict#' qtr2='#qtr#' dsn="#mydsn#" >

<cfquery name="variables.m#qtr2#" datasource="#dsn#" >

...

</cfquery>

</cfthread>

<cfset tname &= 't#qtr#' & ','>

</cfloop>

<cfloop query="variables.dts" >

<cfset qtr += 1>

<cfthread name="t#qtr#" action="run" sat1='#variables.dts.sat#' fri1='#variables.dts.fri#' selarea1='#selArea#' selDistrict1='#seldistrict#' qtr1='#qtr#' dsn="#mydsn#">

<cfquery name="variables.m#qtr1#" datasource="#dsn#" >

...

</cfquery>

</cfthread>

<cfset tname &= 't#qtr#' & ','>

</cfloop>

<cfset tname = left(tname,len(tname)-1)>

<cfthread action="join" name="#tname#" />

This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
December 20, 2015

What is your motivation for using threads? I cannot see any, yet the threads add a lot of complexity to the code.

The database is an independent system. It should therefore be treated as a black-box which may have its own concurrency strategy, beyond Coldfusion's control. Before you know it, there are worms everywhere.

Aside from that, if you replace <cfset tname &= 't#qtr#' & ','> with <cfset tname = listAppend(tname, 't#qtr#')> you will save yourself one line of code.

December 20, 2015

Thank you for the reply

The threads are being used to speed up the retrieval of the data. It works well, I can shave minutes off the total query time. Each query returns the data of a single partition and is combined in a dbquery. It provides in a week analysis of selected facilities almost instantly. It is exactly what is needed, if I can resolve the concurrentmodificationexception. Thank you for the listAppend suggestion, much more readable.

BKBK
Community Expert
Community Expert
December 21, 2015

You have omitted some code (marked by ellipsis), so it is difficult to say.  In any case, the hint from the concurrentmodificationexception is that a thread th6, say, attempted to modify a variable variables.somevar while thread th7was still busy with it.

As your threads are asynchronous, execution may already move to the second loop while threads of the first loop are still busy. Is that consistent with your business logic?

Carl Von Stetten
Legend
December 18, 2015

I moved this to the Advanced Techniques subforum because many of the more experienced ColdFusion community members hang out there.  If you don't get an answer, you might try joining the CFML Slack channel, as you can get access to more real-time community assistance.  I'll even put a link there to this thread to see if I can draw some attention.

-Carl V. - Moderator