[Backgroundrb-devel] When to use Mutex::synchronize?

Ben Johnson bjohnson at contuitive.com
Wed Jul 12 01:37:03 EDT 2006


I have a simple question when to the synchronize method in the Mutex  
class.

Now that backgroundrb has allow_concurrency = true there is no need  
to synchronize database calls in threads.

The question I have is lets say I have a simple method in my worker  
as follows:

def some_method
	SomeModel.find_all each do |obj|
		obj.some_count += 1
		obj.save!
	end
end

It accesses the database, but is not in a thread. Since this is a  
single process running in the background, if that method gets called  
simultaneously will the database lose connection? Will I get  
unexpected results? For instance, if 500 people access  
www.somedomain.com/controller/method which calls that method in the  
backgroundrb process will it screw everything up?

Basically do I need to wrap that in a synchronize method? Like this:

def some_method
	@mutex_obj.snchronize do
		SomeModel.find_all each do |obj|
			obj.some_count += 1
			obj.save!
		end
	end
end

Thanks a lot for your help.

Thank You,
Ben Johnson
E: bjohnson at contuitive.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/backgroundrb-devel/attachments/20060712/7097d3ed/attachment-0001.html 


More information about the Backgroundrb-devel mailing list