Skip to content

Conversation

@shreyas-gopalakrishna
Copy link
Member

@shreyas-gopalakrishna shreyas-gopalakrishna commented Sep 22, 2021

Fixes

  • For java 11 a synchronized singleton classloader will be used
  • This fixes the issue - multiple instances of the static variable even though it's in the same class

Code changes

  • Creating a singleton of the URLClassLoader which will be reused as necessary.
  • In order to handle synchronization across threads, the double checked locking with singleton is done.
  • Using the volatile keyword to avoid any cache incoherence issues
  • Test cases are updated to check if static variables are updated under the same instance

#412


urls.add(url);
}
private final Set<URL> urls;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final Set<URL> urls;
private final Set<URL> urls;
private final Object lock = new Object();

*/
private URLClassLoader getURLClassLoaderInstance(URL[] urlsForClassLoader) {
if (classLoaderInstance == null) {
synchronized (URLClassLoader.class) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
synchronized (URLClassLoader.class) {
synchronized (lock) {

* @param urlsForClassLoader Array of URLs to construct a new URLClassLoader
* @return single instance of URLClassLoader
*/
private URLClassLoader getURLClassLoaderInstance(URL[] urlsForClassLoader) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to pass args here, calculate below instead

public ClassLoader createClassLoader() {
URL[] urlsForClassLoader = new URL[urls.size()];
urls.toArray(urlsForClassLoader);
return createURLClassLoaderInstance();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a style thought, can inline the createURLClassLoaderInstance method here

Comment on lines 35 to 36
classLoaderInstance = new URLClassLoader(urlsForClassLoader);
loadDrivers(classLoaderInstance);
Copy link
Member

@trask trask Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to assign the field at the very end, so that another thread can't come in and return classLoaderInstance prior to it being fully initialized

Suggested change
classLoaderInstance = new URLClassLoader(urlsForClassLoader);
loadDrivers(classLoaderInstance);
URLClassLoader loader = new URLClassLoader(urlsForClassLoader);
loadDrivers(loader);
classLoaderInstance = loader;

trask
trask previously approved these changes Sep 27, 2021
amamounelsayed
amamounelsayed previously approved these changes Sep 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java11: Static variable appears to be difference instance even though it's in the same class.

4 participants