22

I have blocker issue "Close this "ConfigurableApplicationContext"" in main method

public static void main(String[] args)
{
    SpringApplication.run(MyApplication.class, args);
}

I've tried code from SonarQube example

public static void main(String[] args)
{
    ConfigurableApplicationContext context = null;
    try
    {
        context = SpringApplication.run(MyApplication.class, args);
    }
    finally
    {
        if (context != null) {
            context.close();
        }
    }
}

but it closes the context right after starting.

How to fix this issue?

kimreik
  • 635
  • 1
  • 7
  • 25
  • 2
    We are actually looking for feedback on this : https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/sonarqube/Omg-sEBEneU/Gx8wdHGGAwAJ Feel free to weigh in so we can improve the rule. – benzonico May 06 '16 at 14:18

3 Answers3

26

The issue that SonarQube is reporting is a false positive and should be ignored. SonarQube's FAQ lists some options for removing false positives:

False-Positive and Won't Fix

You can mark individual issues as False Positive or Won't Fix through the issues interface. However, this solution doesn't work across branches - you'll have to re-mark the issue False Positive for each branch under analysis. So an in-code approach may be preferable if multiple branches of a project are under analysis:

//NOSONAR

You can use the mechanism embedded in rules engine (//NOPMD...) or the generic mechanism implemented in SonarQube: put //NOSONAR at the end of the line of the issue. This will suppress the issue.

Switch Off Issues

You can review an issue to flag it as false positive directly from the user interface.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • 8
    While //NOSONAR works, it disables all sonar checks although only for that line. Alternative is to specifically disable check for a single rule; which in this case you can do by adding `@SuppressWarnings("squid:S2095")` on your main method. – NikhilWanpal Oct 15 '16 at 13:57
1

If you have a web application, the application context will be destroyed (I think by ContextLoaderListener, not sure), no explicit code is needed.

In the case of a command line application, the context must be destroyed manually, otherwise beans will not be destroyed properly - @PreDestroy methods will not be called. E.g:

@Bean
public ApplicationRunner applicationRunner() {
    return new ApplicationRunner() {
        public void run(ApplicationArguments args) throws Exception {

            try {
                doStuff();
            } finally {
                context.close();
            }
        }

I noticed this when a Cassandra session stayed open after my spring boot command line application has finished.

Assen Kolov
  • 4,143
  • 2
  • 22
  • 32
1

I was always thinking, that it is false/positive.

But you can test this with a this few lines.

@RunWith(SpringRunner.class)
@SpringBootTest
public class YourApplicationTest {

    @Test
    public void shouldLoadApplicationContext() {
    }

    @Test
    public void applicationTest() {
        YourApplication.main(new String[] {});
    }

}

Now Sonar is saying, this is tested!
(Kudos goes out to: Robert @ https://stackoverflow.com/a/41775613/863403)

Joergi
  • 1,527
  • 3
  • 39
  • 82