Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:42:08+00:00 2026-05-26T02:42:08+00:00

Short Version Are class variables thread-safe for the duration of a controller action? Long

  • 0

Short Version

Are class variables thread-safe for the duration of a controller action?

Long Version

I’m writing a Rails 3 app that implements multi-tenancy using a single database schema with tenant_id columns in each table to identify to whom the data belongs.

Naturally I need a way to scope all queries that are run on the database to ensure that access to data is properly isolated per-tenant.

Several people have suggested achieving this by always accessing the database through the tenant instance, e.g.:

current_tenant.associate_collection.where(...)

etc.

However, since multi-tenancy is an architectural concern (not a business domain concern), I’d prefer to keep this as transparent to the model as possible by setting it up the scope globally. It’s also much DRYer.

I’ve found I can do this using the multitenant gem and around_filters:

class ApplicationController
  around_filter do
    Multitenant.with_tenant current_tenant
      yield
    end
  end
end

This manipulates the default_scope of the appropriate models so that all data access is automatically scoped to the current tenant.

This works great in development, but I have concerns about thread safety. Multitenant.with_tenant preserves the current_tenant reference in a class variable.

The question is can I rely on the integrity of that variable while the controller action is on the stack? or could it be corrupted by another request?

Also, what’s a good source on general concurrency issues in Rails?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:42:09+00:00Added an answer on May 26, 2026 at 2:42 am

    It’s not a matter of duration, and there isn’t anything special about a controller that makes it an atomic (uninterruptible) sequence of code. Thread safety is about understanding locks, synchronization, and the thread scheduler.

    I don’t think your question is even Rails specific in this case. In a general sense, thread safety is achieved through locking data while a write is happening, and unlocking it when the write is finished. It’s very similar database locking mechanisms, if you’re familiar with those.

    For a good reference that focuses on Ruby, I would go here: http://www.rubycentral.com/pickaxe/tut_threads.html

    For more info about the concept the thread safety (threads run at the OS level, after all, so it’s more about understanding how thread safety works in any program), I would go here: http://en.wikipedia.org/wiki/Thread_safety

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version: assuming I don't want to keep the data for long, how do
Short version of question: see title Long version of question: I've used jquery's show()
I got the following code for my object (short version) : public class PluginClass
Short version: For a cache class I need to get notified if an object
Ladies & Gentlemen, Short Version: Trying to dynamically create a Spine Model Class. Basically
The short version is that I have a protocol which has an optional parameter.
I divided my problem into a short and a long version for the people
Short version: Can a COM class modify its own virtual table entries at runtime?
Short version : VB.Net Windows forms feature controls that are often dragged from the
Short version: Is it possible in NHibernate to create a mapping for a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.