Skip to content

Conversation

rsteph-de
Copy link
Member

@Mewel
Copy link
Member

Mewel commented Jul 15, 2025

This is not a fix in my eyes. The problem is the LazyInstanceHolder.

The test should look like this:

    @Test
    public void instance() {
        try {
            MCREventManager.getInstance();
        } catch (ExceptionInInitializerError e) {
            assertEquals("Configuration property MCR.EventHandler.Mode.Foo is not set.", e.getCause().getMessage());
        }
    }

Thats why I don't like this pattern for singletons. You're kind of violating the contract of the MCREventManager which wants to throw MCRConfigurationExceptions. Due to the fact that this is now initialized in static initialization, every exception will always be a ExceptionInInitializerError.


We should use the double locking pattern here:

    private static volatile MCREventManager instance;
    private MCREventManager() throws MCRConfigurationException {
    }
    public static MCREventManager getInstance() throws MCRConfigurationException {
        if (instance == null) {
            synchronized (MCREventManager.class) {
                if (instance == null) {
                    instance = new MCREventManager(); // can propagate exception
                }
            }
        }
        return instance;
    }

@toKrause
Copy link
Contributor

In the remaining parts of MyCoRe, we now use the Bill Pugh Singleton patern instead of the double locking pattern everywhere.

@rsteph-de rsteph-de marked this pull request as draft July 15, 2025 13:45
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.

3 participants