Giter Site home page Giter Site logo

GT.M can not be re-opened about nodem HOT 4 CLOSED

dlwicksell avatar dlwicksell commented on May 31, 2024
GT.M can not be re-opened

from nodem.

Comments (4)

dlwicksell avatar dlwicksell commented on May 31, 2024

That is the expected behavior. Nodem relies upon the GT.M Call-in interface, which allows a process to open the database one time, and once it has closed its database connection, that process is no longer allowed to open it. So you should only open the connection to GT.M one time in any Node.js process, and you should only close the connection to GT.M one time in any Node.js process, unless you are finished accessing it in that process.

The initialization is what adds Nodem into your Node.js process. And that is something you only need to do one time as well. Initializing Nodem creates the appropriate Nodem object that gives you access to Nodem, using a constructor method which wraps a C++ class which exposes the various Nodem methods. If you require a module more than once, Node.js will only add that module to that process's image one time.

So you only need to require Nodem one time per process, and you only need to initialize Nodem one time per process, and you only need to open the database connection one time per process, and you should only close the connection to the database after you are done talking to GT.M, as you cannot close it and then re-open it in the same process, which is a limitation in the Call-in API of GT.M. Actually you don't even need to close the database connection, because all of its resources will be released whenever a process that is linked with GT.M ends anyway.

You should therefore not try requiring, initializing, opening, or closing the Nodem interface for each function or method in your Node.js code. I hope that helps.

from nodem.

wpoosanguansit avatar wpoosanguansit commented on May 31, 2024

As this is quite unusual from the normal practice with other db, I hope you do not mind I rehash it a bit. So the pattern found in the example is only for short term process. And for long running processes, we just need to open the connection, init but do not care to close it as it will be garbage collected if and when the process is terminated. And there is no memory leak as a result of not closing the db. Is my understanding correct? Thanks of your help.

from nodem.

dlwicksell avatar dlwicksell commented on May 31, 2024

Yes, that is basically correct. When the process is terminated, its resources will be cleaned up and not leaked. That is actually a function of the operating system. When a process dies, its virtual address space is removed and any physical memory mapped to it is freed. In addition, Nodem is using V8 C++ APIs, which themselves allocate memory that is cleaned up by a garbage collector (the JavaScript garbage collector in Node.js). And when I have to allocate dynamic memory on the heap in C code, to work with GT.M, I am careful (I believe) to deallocate it properly.

But, closing the database connection will ensure that the terminal is reset to the settings it had before it opened a connection to GT.M (which changes those settings in surprising ways), as well as explicitly allowing GT.M the chance to clean up its resources. I'd still advise that you use the close API in your code. Again, only close the database connection when you no longer need to access GT.M in that process.

from nodem.

dlwicksell avatar dlwicksell commented on May 31, 2024

For long-running Node.js processes, you should probably close the database in the handler code for stopping the Node process. E.g.

process.on('exit', function() { 
    db.close(); 
});

and

process.on('SIGINT', function() {
    db.close();
});

Where you can put your own cleanup code right before or after closing the connection to GT.M.

from nodem.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.