保存大量记录时的java.lang.OutOfMemoryError

bal*_*kyi 3 coldfusion exception cfwheels

我在使用CFWheels将大量记录保存到数据库时遇到问题.这是一个例子:

<cfloop from="1" to="10000" index="i">
 <cfset var newUser = model("user").new()>
 <cfset newUser.name = "Test"&i>
 <cfset newUser.save()>
</cfloop>
Run Code Online (Sandbox Code Playgroud)

这会导致java.lang.OutOfMemoryError

请帮我解决这个问题.

ora*_*ips 5

循环多个数据库调用导致OOM是一个已知的ColdFusion错误.幸运的是,有一个解决方法,使用<cfthread/>.你应该能够改变你的代码:

<cfloop from="1" to="10000" index="i">
 <cfset threadName = "thread" & createUuid()>
 <cfthread name="#threadName#">
  <cfset var newUser = model("user").new()>
  <cfset newUser.name = "Test"&i>
  <cfset newUser.save()>
 </cfthread>
 <cfthread action="join" name="#threadName#">
</cfloop>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您仅使用该线程的副作用,在不同的上下文中运行,以便它不会保留在堆上.因此,在声明线程后立即加入,所以它实际上并没有并行运行.