0

I want to fetch customers from remote db in background job. But during this time I want to keep using postgresql for other requests. I want to know if I establish_connection in worker, during this process does it cause any problem? For example during this process my app responses successfully from postgresql?

I've tried establish_connection in background job. But I need to be sure if it cause any problem.

  • 2
    The connection limit depends entirely on your setup. But its typically >1. https://guides.rubyonrails.org/configuring.html#database-pooling – max Nov 24 '22 at 20:55
  • Thank you for answer. My limit is 5. I'm changing connection with connnect method in a class like: def connect(settings) @old_connection = ActiveRecord::Base.remove_connection ActiveRecord::Base.establish_connection(settings) end during this for example oracle connection I'm fetching customers every night with background job. During this job running I'm wondering if my app can keep working with Postgre sql? – özgür pir Dec 04 '22 at 16:12
  • This is a borderline non-sensical question as its just so broad and vague. You can connect to your database as long as you have additional connections available. There is no obvious reason why your web application wouldn't be able to keep running while you perform the job asyncronously provided you give it the needed resources. – max Dec 05 '22 at 12:15
  • Also unless you're using an ancient version of Rails use the built support for multiple databases instead of reinventing the wheel (badly). https://guides.rubyonrails.org/active_record_multiple_databases.html – max Dec 05 '22 at 12:18

1 Answers1

0

ActiveRecord uses database connection pool so your background job processes and app server processes get separate DB connections. So yes your app and background job can keep running in parallel as long as you have enough connections in the pool.

Not sure why you need establish_connection. See if the newly added load_async is of any help.

tejasbubane
  • 914
  • 1
  • 8
  • 11