node-java icon indicating copy to clipboard operation
node-java copied to clipboard

API for destroying JVM

Open drobiazko opened this issue 11 years ago • 14 comments

Is there any way to invoke this deconstructor from Node code? We'd like to have an API for destroying the JVM.

drobiazko avatar Aug 05 '14 10:08 drobiazko

Not really. Can you give me some insight into why you might want to do this? Once the JVM is destroyed any references to the Java object in JavaScript land would become unusable, then we would need a method to restart it I guess.

joeferner avatar Aug 05 '14 11:08 joeferner

We have a Java worker that executes Java jobs. Each job is actually a completely independent Java app with its own set of dependencies. We would like to isolate the classpaths of these Java jobs. I guess just clearing the classpath array won't help.

Do you have any ideas?

drobiazko avatar Aug 05 '14 12:08 drobiazko

Last time I've tried to do so, I've hit a long-standing bug that didn't allow you to re-initialize JVM for the process once it has been deinitialized. I don't know if situation is any different right now.

jzakrzewski avatar Aug 05 '14 12:08 jzakrzewski

We could try moving that into a separate method that can be callable from both the deconstructor and a module method. We'd need to define a way to initialize it again though.

jsdevel avatar Aug 05 '14 15:08 jsdevel

This would be awesome. Let me know if you need an alpha tester for this feature.

drobiazko avatar Aug 06 '14 14:08 drobiazko

@jzakrzewski I doubt it. The only way to do this is to unload the JVM shared library.

DemiMarie avatar Apr 16 '17 20:04 DemiMarie

Any progress on this?

Tomas2D avatar Mar 22 '22 23:03 Tomas2D

Doubtful. The JVM simply doesn’t have good unloading support right now. @Tomas2D what is your use-case? @drobiazko you can do something similar with classloader trickery.

DemiMarie avatar Mar 23 '22 13:03 DemiMarie

For example using application with need of different database connections based on application module. Next and not least is E2E tests, where Jest obviously fails to init multiple JVM.

Tomas2D avatar Mar 23 '22 15:03 Tomas2D

For example using application with need of different database connections based on application module.

Is classloader trickery an option?

Next and not least is E2E tests, where Jest obviously fails to init multiple JVM.

I think your best option is going to be using a cached JVM instance for all tests, or spawning a separate process.

DemiMarie avatar Mar 23 '22 15:03 DemiMarie

I was able to achieve caching via pretty hacky way in Jest with help of this article http://www.petecorey.com/blog/2018/11/05/bending-jest-to-our-will-caching-modules-across-tests/ .

But I do not understand what do you mean by classloader trickery, I am using this package - https://github.com/CraZySacX/node-jdbc

Tomas2D avatar Mar 23 '22 15:03 Tomas2D

But I do not understand what do you mean by classloader trickery, I am using this package - https://github.com/CraZySacX/node-jdbc

I know basically nothing about JDBC, but I would imagine there is a way to have multiple database connections in the same JVM.

DemiMarie avatar Mar 23 '22 15:03 DemiMarie

I see your point, but it looks like, that the mentioned library does not supports that.

Tomas2D avatar Mar 23 '22 15:03 Tomas2D

Please file a ticket with that library.

DemiMarie avatar Mar 23 '22 15:03 DemiMarie