I am using the mongodb-driver-sync-4.1.0-rc0 dependency and whenever I connect to the database I can use it within the same method but as soon as i store it as a static var and try accessing it from another class it says "state should be: open". My connection instantly closes after establishing a connection.
public static void initializeClient(){
String uri = "mongodb+srv://myusername:mypassword@userstats.9vs4b.mongodb.net/izzi?retryWrites=true&w=majority";
try ( MongoClient mongoClient = MongoClients.create(uri)) {
client = mongoClient;
izzi = client.getDatabase("izzi").getCollection("userStats");
System.out.println(getOverallLikes());
System.out.println(getWinnersLastMonth());
//after this it closes the connection
}
}
I get this message when it disconnects: [main] INFO org.mongodb.driver.connection - Closed connection [connectionId{localValue:4, serverValue:14654}] to userstats-shard-00-02.9vs4b.mongodb.net:27017 because the pool has been closed.