Answer the question
In order to leave comments, you need to log in
How safe is it to keep the model in a separate thread?
Hello.
There is a model that can persist for quite a long time. About a second, but for a web application this is critical.
How justified is this approach?
Thread.new do
model_instance.save
end
Answer the question
In order to leave comments, you need to log in
It is likely that here you can already think about some tools for working with background tasks and give something "long-playing" there.
But, if it’s specific to the case, then you need to keep in mind that each thread will be opened with its own connection to the database, which then will need to be closed by hand inside the block after saving:
Thread.new do
model_instance.save
ActiveRecord::Base.connection.close
end
Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
model_instance.save
end
end
I would start with the db. Analyzed the query plan, optimized the query. Finding out the reason why the database takes a long time to execute a query is paramount.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question