lots of warnings on webserver using objectbox

Viewed 94

I am using a SparkJava (basically Jetty) web server behind an nginx proxy with ObjectBox.

I'm starting to notice a lot of

Hint: use closeThreadResources() to avoid finalizing recycled transactions (initial commit count: 213).
Hint: use closeThreadResources() to avoid finalizing recycled transactions (initial commit count: 213).
Hint: use closeThreadResources() to avoid finalizing recycled transactions (initial commit count: 213).
Hint: use closeThreadResources() to avoid finalizing recycled transactions (initial commit count: 213).
Hint: use closeThreadResources() to avoid finalizing recycled transactions (initial commit count: 213).
Hint: use closeThreadResources() to avoid finalizing recycled transactions (initial commit count: 213).
038-22:43:52.3260 [WARN ] Skipping low-level close for read-only cursor (non-creator thread 'java')
038-22:43:52.5815 [WARN ] Skipping low-level close for read-only cursor (non-creator thread 'java')
038-22:43:52.5815 [WARN ] Skipping low-level close for read-only cursor (non-creator thread 'java')
038-22:43:52.5815 [WARN ] Destroyed recycled transaction from non-owner thread 'java'
038-22:43:52.5820 [WARN ] Skipping low-level close for read-only cursor (non-creator thread 'java')
038-22:43:52.5820 [WARN ] Destroyed recycled transaction from non-owner thread 'java'
038-22:43:52.5821 [WARN ] Skipping low-level close for read-only cursor (non-creator thread 'java')

warnings, in my logs, and I'm thinking of ways around this.

What I've seen suggested, is possibly to call closeThreadResources() as suggested in the hints, possibly after each call has finished. However, I'm no expert on Jetty and not sure if each thread is killed or reused possibly. Maybe calling closeThreadResources() might break if threads are reused? Plus, I have multiple ObjectBox boxes, so I'd probably have to either remember which boxes have been used, or call closeThreadResources() on all of them. Not perfect, but doable.

Then there is also the possible reuse of queries, that might fix things, after reading https://github.com/objectbox/objectbox-java/issues/753#issuecomment-525314808. I can potentially redo most of my code to create the queries once, then set the parameter values when they're used. How thread safe is this, when being used by a webserver? i.e. if i change the value of a query to use it, and it's already being used by another thread, what happens?

1 Answers

What you likely want to do is to call closeThreadResources() once you are done with a web request from the thread serving it. This works fine with threads pools, which are also used by web servers. You can do this for all requests with e.g. servlet filters, but not sure if there are better approaches these days to do that.

Some background: each thread using ObjectBox "caches" some resources (thread local). The best way to clean up after a thread is "done" (at least for now, e.g. served the eb request) with ObjectBox, it to call closeThreadResources(). Once that thread is back (e.g. from thread pool) it will simply allocate the resources it needs again.

Queries: yes you can reuse them, but ensure to lock e.g. the Query object while you query as you might race with other threads serving requests too.

Related