Why YottaDB connection can't be opened by worker thread?
xoxloviwan opened this issue · 1 comments
> Uncaught Error: YottaDB connection must be managed by main thread
at Object.<anonymous> (/data/src/get.js:6:17)
at Module._compile (internal/modules/cjs/loader.js:999:30)
I try to get access to YottaDB with ExpressJS server, and I found #9 issue where there is statement about short term access operations. #9 (comment)
I assume that if there is conception of one node process per one "open-get-close" operation, moving logic to work thread fit into this concept...
How is it supposed to interact with short-term operations if node process can be open only one connect with db? How share this access with other consumers if we need read/write to db in multithread mode?
That's how threads work. Threads are part of the same process, and YottaDB requires one, and only one, connection from a single process. Threads share some state with each other, but they maintain their own execution stacks, and can be scheduled to run independently from each other by the OS scheduler. Nodem manages synchronization of threads communicating with YottaDB for you.
The README.md document explains how to use worker threads with Nodem. Here is the relevant portion:
Nodem supports the Worker Threads, for both synchronous and asynchronous calls.
Since YottaDB and GT.M are single-threaded, opening and closing a connection to
their database and runtime, should only be done once per process lifetime.
Nodem'sopen
andclose
APIs will only work when called from the main thread
of the process. In order to work with the worker threads API, you should call
the Nodemopen
API in the main thread before creating any worker threads, and
you should call the Nodemclose
API in the main thread, after all the worker
threads have exited. You will still need to make sure that you require Nodem in
each worker thread, as well as the main thread, in order to have access to the
Nodem API in each thread.
So if the connection to YottaDB is open when you create a new worker thread, that thread will automatically have the same access to the YottaDB database runtime. I hope that helps. As this is not an issue, I am closing this.