active record query does not terminate if run in thread using jruby

52 views Asked by At

I have a query looking up postgres -tables inside a thread in jruby.

Although there is no difference two me, active record does not terminate with some of the queries in concern of some tables, although it does terminate with some other tables.

I checked if it depends on indezes of these tables, or their structure, but it does not, or I couldn't find it, restricted to all the structure I could differentiate.

So I coded the same algorithm using raw-sql and it worked.

Hence there must be a bug with gem 'activerecord-jdbcpostgresql-adapter', '>= 61.0-java'.

I use 'rails', '~> 6.1.0' and jruby-9.2.14.0.

It worked as follows: Thread.new { query = <<~SQL [...] SQL ActiveRecord::Base.connection.execute(query) }

It only works with some tables, but not with all: Thread.new { Table.where(p1: s1) }

Sadly I cannot give you a hint how to reproduce it so far, as I don't catch the difference of the tables. Ask me, if you have any background-inspired guess.

1

There are 1 answers

0
Theores On

As a workaround it is possible to restrict the number of threads called by something like.

array1 = [...]; thread_index = 0; max_threads = 2; index = 0; continue = true; result = []; threads = [] while continue; while thread_index < max_threads threads << Thread.new { if array1[index] == nil; continue = false; else; result << function(array1[index]); end } index += 1; thread_index += 1 end threads.join; thread_index = 0 end

So somehow the maximal number of threads is not looked up for all database tables?

Restricted to my degree of understanding I would say, the above workaround is not thread-save, as it might be, that a tread is used somehow external?!