Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent tenants with threads using Postgres without schemas #180

Open
epotocko opened this issue Nov 20, 2014 · 1 comment
Open

Concurrent tenants with threads using Postgres without schemas #180

epotocko opened this issue Nov 20, 2014 · 1 comment

Comments

@epotocko
Copy link

I am experiencing issues with tenants not being switched in sidekiq for the entire duration of the job when there are multiple jobs running concurrently. I am using Postgres without schemas. I was able to put together this script to reproduce the problem (I can run it via the rails console or a rake task). I haven't tested this code with Postgres with schemas or Mysql so it could be broken there too.

def get_thread(tenant)
  Thread.new { 
    Apartment::Tenant.process(tenant) do
      (0..5).each do |i|
        sleep 1
        puts "#{tenant} = #{Apartment::Tenant.current_tenant}"
      end
    end
  }
end

Apartment::Tenant.switch('base')
t1 = get_thread('db1')
sleep(1)
t2 = get_thread('db2')

t1.join
t2.join

Output is:
db1 = db2
db2 = db2
db1 = db2
db2 = db2
db1 = db2
db2 = db2
db1 = db2
db2 = db2
db1 = db2
db2 = db2
db1 = db2
db2 = base

@bradrobertson
Copy link
Contributor

Apartment just uses rails connection handling if you're not using postgres schemas. I'm going to be looking at another threading issue shortly, I'll see if this aligns with your findings at all

mikecmpbll added a commit to mikecmpbll/apartment that referenced this issue Oct 8, 2015
The problem: with connection-based adapters (adapters that use
`establish_connection`) switching the tenant in one thread would
switch it for all other threads. This is because of how AR's
connection handler stores a global connection pool to class name
mapping.

The solution: AR has a strange way that you can override the
connection handler _for the local thread_, by calling the
`connection_handler=` setter. This will means that threads don't
share a connection pool, so there will be more connections
(threads*pool_size), but it means that we can use separate
connections per thread.
mikecmpbll added a commit to mikecmpbll/apartment that referenced this issue Oct 8, 2015
The problem: with connection-based adapters (adapters that use
`establish_connection`) switching the tenant in one thread would
switch it for all other threads. This is because of how AR's
connection handler stores a global connection pool to class name
mapping.

The solution: AR has a strange way that you can override the
connection handler _for the local thread_, by calling the
`connection_handler=` setter. This will means that threads don't
share a connection pool, so there will be more connections (threads
*pool_size), but it means that we can use separate connection pool
per thread allowing us to use `establish_connection` safely.
vladra pushed a commit to vladra/apartment that referenced this issue May 9, 2016
The problem: with connection-based adapters (adapters that use
`establish_connection`) switching the tenant in one thread would
switch it for all other threads. This is because of how AR's
connection handler stores a global connection pool to class name
mapping.

The solution: AR has a strange way that you can override the
connection handler _for the local thread_, by calling the
`connection_handler=` setter. This will means that threads don't
share a connection pool, so there will be more connections (threads
*pool_size), but it means that we can use separate connection pool
per thread allowing us to use `establish_connection` safely.
josefsj3m pushed a commit to josefsj3m/apartment that referenced this issue Oct 27, 2018
gmhawash pushed a commit to downhome/apartment that referenced this issue Jul 19, 2022
**Implemented enhancements:**

- Increase errors visibility by showing more information on the underlying error rather than a generic error 'Apartment::TenantNotFound' (influitive#176)
- Resolved influitive#177 - Added rails 7 support (influitive#178)

**Fixed bugs:**

- Fixing tenant_presence_check config in the README (influitive#180)
- Resolved influitive#161 and influitive#81 - Fixed sequence name (influitive#187)

**Closed issues:**

- Resolved influitive#151 - removed reloader and console overwrite of reload method (influitive#174)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants