Skip to content

Commit 18d1687

Browse files
authored
close java-db client (#5273)
1 parent eb60e9f commit 18d1687

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

pkg/fanal/analyzer/language/java/jar/jar.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
"path/filepath"
88
"strings"
9-
"sync"
109

1110
"golang.org/x/xerrors"
1211

@@ -35,7 +34,6 @@ var requiredExtensions = []string{
3534

3635
// javaLibraryAnalyzer analyzes jar/war/ear/par files
3736
type javaLibraryAnalyzer struct {
38-
once sync.Once
3937
client *javadb.DB
4038
slow bool
4139
}
@@ -49,18 +47,13 @@ func newJavaLibraryAnalyzer(options analyzer.AnalyzerOptions) (analyzer.PostAnal
4947
func (a *javaLibraryAnalyzer) PostAnalyze(ctx context.Context, input analyzer.PostAnalysisInput) (*analyzer.AnalysisResult, error) {
5048
// TODO: think about the sonatype API and "--offline"
5149
var err error
52-
a.once.Do(func() {
53-
log.Logger.Info("JAR files found")
54-
a.client, err = javadb.NewClient()
55-
if err != nil {
56-
log.Logger.Errorf("Unable to initialize the Java DB: %s", err)
57-
return
58-
}
59-
log.Logger.Info("Analyzing JAR files takes a while...")
60-
})
50+
log.Logger.Info("JAR files found")
51+
a.client, err = javadb.NewClient()
6152
if err != nil {
62-
return nil, err
53+
return nil, xerrors.Errorf("Unable to initialize the Java DB: %s", err)
6354
}
55+
defer func() { _ = a.client.Close() }()
56+
log.Logger.Info("Analyzing JAR files takes a while...")
6457

6558
// Skip analyzing JAR files as the nil client means the Java DB was not downloaded successfully.
6659
if a.client == nil {

pkg/javadb/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,10 @@ func (d *DB) SearchByArtifactID(artifactID string) (string, error) {
170170

171171
return groupID, nil
172172
}
173+
174+
func (d *DB) Close() error {
175+
if d == nil {
176+
return nil
177+
}
178+
return d.driver.Close()
179+
}

0 commit comments

Comments
 (0)