!! Unexpected error while processing request: fail

2019-03-07 01:27发布

问题:

Please help me in solving this error.

I am getting this error while loading records from text files in to database using ruby scripts.

It just works fine if I use small number of records to load in to the database.But fails if there are large number of records.

CSV.foreach(fileName) do |line|
    completePath = line[0]                                                
    num_of_bps = line[1]

    completePath = cluster_path+ '/' + completePath
    inode = FileOrFolder.find_by_fullpath(completePath, :select=>"id") 

    metric_instance = MetricInstance.find(:first, :conditions=>["file_or_folder_id = ? AND dataset_id = ?", inode.id, dataset_id])
    add_entry(metric_instance.id, num_of_bps, num_of_bp_tests) 
end

def self.add_entry(metaid, num_of_bps, num_of_bp_tests)
    entry = Bp.new
    entry.metric_instance_id = metaid
    entry.num_of_bps = num_of_bps
    entry.num_of_bp_tests = num_of_bp_tests
    entry.save
    return entry
end  

回答1:

Try something like this:

File.open(fileName) do |csv|
  csv.each_line do |line|
    CSV.parse(line) do |values|
      # Here you can do your manipulation
    end
  end
end

This way is slower, but it should ensure you don't get out of memory.