Java.lang.OutOfMemoryError while saving a huge amount of records

I have a problem storing a huge number of records in a database using CFWheels. Here is an example:

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

This raises java.lang.OutOfMemoryError

Please help me solve this problem.

+3
source share
3 answers

There are a couple of pretty inefficient things happening here. Firstly, it generates 1000 userobjects, which is actually not a good idea to do in one request in ColdFusion. Secondly, it launches 1000 database queries, which is actually not a good idea in any programming language.

, . ORM Wheels, , , , .

, SQL Server 2008, user, cfquery:

<cffunction name="batchCreate">
    <cfquery datasource="#get('dataSourceName')#">
        INSERT INTO
            #this.tableName()# (#this.columnNameForProperty("name")#)
        VALUES
            <cfloop from="1" to="10000" index="i">
                (<cfqueryparam cfsqltype="cf_sql_varchar" value="Test#i#">)
                <cfif i lt 10000>,</cfif>
            </cfloop>
    </cfquery>
</cffunction>

, -, MySQL .

0

, 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>

, , . , , .

+5

All Articles