Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube "Close this ConfigurableApplicationContext" in Spring Boot project

Tags:

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?

like image 485
kimreik Avatar asked May 06 '16 11:05

kimreik


People also ask

What is the default configuration for SonarQube?

By default, SonarQube way came preinstalled with the server. The default configuration for SonarQube way flags the code as failed if: the coverage on new code is less than 80%. percentage of duplicated lines on new code is greater than 3. maintainability, reliability or security rating is worse than A.

When does the SonarQube way flag the code as failed?

The default configuration for SonarQube way flags the code as failed if: the coverage on new code is less than 80% percentage of duplicated lines on new code is greater than 3 maintainability, reliability or security rating is worse than A

What is coverage exclusion in SonarQube?

sonar.coverage.exclusions: This provides us to exclude any code file unrelated to the coverage report. For example Spring Boot Main class. You can find more configuration keys in the Sonarqube server Administration -> Configuration -> Analysis Scope.

What is static source code analysis with SonarQube?

1. Overview In this article, we're going to be looking at static source code analysis with SonarQube – which is an open-source platform for ensuring code quality. Let's start with a core question – why analyze source code in the first place?


2 Answers

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.

like image 156
Andy Wilkinson Avatar answered Sep 17 '22 12:09

Andy Wilkinson


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.

like image 45
Assen Kolov Avatar answered Sep 16 '22 12:09

Assen Kolov