added threadsafe code to make compatible with rail4 and jruby production