From 3ba108f51ec2bb42f903e7c7ab4ec0b1d4cb2d83 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Tue, 19 Dec 2017 10:11:04 -0500 Subject: [PATCH] Ping the mongo session when the connection is retrieved. This was in the deprecated backend where it fixed a similar issue a long time ago but for some reason didn't make it over. Additionally the function wasn't being locked properly. Hopefully fixes #2973 --- plugins/database/mongodb/connection_producer.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/database/mongodb/connection_producer.go b/plugins/database/mongodb/connection_producer.go index 9674d10bf7c1..aa674aafe3a2 100644 --- a/plugins/database/mongodb/connection_producer.go +++ b/plugins/database/mongodb/connection_producer.go @@ -90,12 +90,18 @@ func (c *mongoDBConnectionProducer) Initialize(ctx context.Context, conf map[str // Connection creates a database connection. func (c *mongoDBConnectionProducer) Connection(_ context.Context) (interface{}, error) { + c.Lock() + defer c.Unlock() + if !c.Initialized { return nil, connutil.ErrNotInitialized } if c.session != nil { - return c.session, nil + if err := c.session.Ping(); err == nil { + return c.session, nil + } + c.session.Close() } dialInfo, err := parseMongoURL(c.ConnectionURL)