Skip to content

Commit 50755b7

Browse files
committed
Fix Bug with mongo connection. Used "Try with resources"
1 parent 4903984 commit 50755b7

File tree

4 files changed

+73
-53
lines changed

4 files changed

+73
-53
lines changed

caching/src/main/java/com/iluwatar/caching/AppManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package com.iluwatar.caching;
2525

2626
import com.iluwatar.caching.database.DbManager;
27+
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
2728
import java.util.Optional;
2829

2930
import lombok.Data;
@@ -68,7 +69,11 @@ public AppManager(final DbManager newDbManager) {
6869
* to (temporarily) store the data/objects during runtime.
6970
*/
7071
public void initDb() {
71-
dbManager.connect();
72+
try {
73+
dbManager.connect();
74+
} catch (DatabaseConnectionException e) {
75+
LOGGER.error("Could not connect to DB: {}", e.getMessage());
76+
}
7277
}
7378

7479
/**

caching/src/main/java/com/iluwatar/caching/database/DbManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.iluwatar.caching.database;
22

33
import com.iluwatar.caching.UserAccount;
4+
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
45

56
/**
67
* <p>DBManager handles the communication with the underlying data store i.e.
@@ -11,7 +12,7 @@ public interface DbManager {
1112
/**
1213
* Connect to DB.
1314
*/
14-
void connect();
15+
void connect() throws DatabaseConnectionException;
1516

1617
/**
1718
* Read from DB.

caching/src/main/java/com/iluwatar/caching/database/MongoDb.java

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,31 @@
77

88
import com.iluwatar.caching.UserAccount;
99
import com.iluwatar.caching.constants.CachingConstants;
10+
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
1011
import com.mongodb.MongoClient;
1112
import com.mongodb.client.MongoDatabase;
1213
import com.mongodb.client.model.UpdateOptions;
14+
import lombok.extern.slf4j.Slf4j;
1315
import org.bson.Document;
1416

1517
/**
1618
* Implementation of DatabaseManager.
1719
* implements base methods to work with MongoDb.
1820
*/
21+
@Slf4j
1922
public class MongoDb implements DbManager {
20-
/**
21-
* Mongo db.
22-
*/
23-
private MongoDatabase db;
23+
private static final String DATABASE_NAME = "test";
2424

2525
/**
26-
* Connect to Db.
26+
* Connect to Db. Check th connection
2727
*/
2828
@Override
29-
public void connect() {
30-
MongoClient mongoClient = new MongoClient();
31-
db = mongoClient.getDatabase("test");
29+
public void connect() throws DatabaseConnectionException {
30+
try (MongoClient mongoClient = new MongoClient()) {
31+
mongoClient.getDatabase("test");
32+
} catch (NoClassDefFoundError e) {
33+
throw new DatabaseConnectionException("Could not connect to DB.");
34+
}
3235
}
3336

3437
/**
@@ -39,19 +42,23 @@ public void connect() {
3942
*/
4043
@Override
4144
public UserAccount readFromDb(final String userId) {
42-
if (db == null) {
43-
connect();
44-
}
45-
var iterable = db
46-
.getCollection(CachingConstants.USER_ACCOUNT)
47-
.find(new Document(USER_ID, userId));
48-
if (iterable.first() == null) {
49-
return null;
45+
try (MongoClient mongoClient = new MongoClient()) {
46+
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
47+
var iterable = db
48+
.getCollection(CachingConstants.USER_ACCOUNT)
49+
.find(new Document(USER_ID, userId));
50+
if (iterable.first() == null) {
51+
return null;
52+
}
53+
Document doc = iterable.first();
54+
if (doc != null) {
55+
String userName = doc.getString(USER_NAME);
56+
String appInfo = doc.getString(ADD_INFO);
57+
return new UserAccount(userId, userName, appInfo);
58+
} else {
59+
return null;
60+
}
5061
}
51-
Document doc = iterable.first();
52-
String userName = doc.getString(USER_NAME);
53-
String appInfo = doc.getString(ADD_INFO);
54-
return new UserAccount(userId, userName, appInfo);
5562
}
5663

5764
/**
@@ -62,15 +69,15 @@ public UserAccount readFromDb(final String userId) {
6269
*/
6370
@Override
6471
public UserAccount writeToDb(final UserAccount userAccount) {
65-
if (db == null) {
66-
connect();
72+
try (MongoClient mongoClient = new MongoClient()) {
73+
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
74+
db.getCollection(USER_ACCOUNT).insertOne(
75+
new Document(USER_ID, userAccount.getUserId())
76+
.append(USER_NAME, userAccount.getUserName())
77+
.append(ADD_INFO, userAccount.getAdditionalInfo())
78+
);
79+
return userAccount;
6780
}
68-
db.getCollection(USER_ACCOUNT).insertOne(
69-
new Document(USER_ID, userAccount.getUserId())
70-
.append(USER_NAME, userAccount.getUserName())
71-
.append(ADD_INFO, userAccount.getAdditionalInfo())
72-
);
73-
return userAccount;
7481
}
7582

7683
/**
@@ -81,15 +88,15 @@ public UserAccount writeToDb(final UserAccount userAccount) {
8188
*/
8289
@Override
8390
public UserAccount updateDb(final UserAccount userAccount) {
84-
if (db == null) {
85-
connect();
91+
try (MongoClient mongoClient = new MongoClient()) {
92+
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
93+
Document id = new Document(USER_ID, userAccount.getUserId());
94+
Document dataSet = new Document(USER_NAME, userAccount.getUserName())
95+
.append(ADD_INFO, userAccount.getAdditionalInfo());
96+
db.getCollection(CachingConstants.USER_ACCOUNT)
97+
.updateOne(id, new Document("$set", dataSet));
98+
return userAccount;
8699
}
87-
Document id = new Document(USER_ID, userAccount.getUserId());
88-
Document dataSet = new Document(USER_NAME, userAccount.getUserName())
89-
.append(ADD_INFO, userAccount.getAdditionalInfo());
90-
db.getCollection(CachingConstants.USER_ACCOUNT)
91-
.updateOne(id, new Document("$set", dataSet));
92-
return userAccount;
93100
}
94101

95102
/**
@@ -100,21 +107,21 @@ public UserAccount updateDb(final UserAccount userAccount) {
100107
*/
101108
@Override
102109
public UserAccount upsertDb(final UserAccount userAccount) {
103-
if (db == null) {
104-
connect();
110+
try (MongoClient mongoClient = new MongoClient()) {
111+
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
112+
String userId = userAccount.getUserId();
113+
String userName = userAccount.getUserName();
114+
String additionalInfo = userAccount.getAdditionalInfo();
115+
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
116+
new Document(USER_ID, userId),
117+
new Document("$set",
118+
new Document(USER_ID, userId)
119+
.append(USER_NAME, userName)
120+
.append(ADD_INFO, additionalInfo)
121+
),
122+
new UpdateOptions().upsert(true)
123+
);
124+
return userAccount;
105125
}
106-
String userId = userAccount.getUserId();
107-
String userName = userAccount.getUserName();
108-
String additionalInfo = userAccount.getAdditionalInfo();
109-
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
110-
new Document(USER_ID, userId),
111-
new Document("$set",
112-
new Document(USER_ID, userId)
113-
.append(USER_NAME, userName)
114-
.append(ADD_INFO, additionalInfo)
115-
),
116-
new UpdateOptions().upsert(true)
117-
);
118-
return userAccount;
119126
}
120127
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.caching.database.exceptions;
2+
3+
public class DatabaseConnectionException extends Exception {
4+
public DatabaseConnectionException(String s) {
5+
super(s);
6+
}
7+
}

0 commit comments

Comments
 (0)