Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit50755b7

Browse files
committed
Fix Bug with mongo connection. Used "Try with resources"
1 parent4903984 commit50755b7

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
packagecom.iluwatar.caching;
2525

2626
importcom.iluwatar.caching.database.DbManager;
27+
importcom.iluwatar.caching.database.exceptions.DatabaseConnectionException;
2728
importjava.util.Optional;
2829

2930
importlombok.Data;
@@ -68,7 +69,11 @@ public AppManager(final DbManager newDbManager) {
6869
* to (temporarily) store the data/objects during runtime.
6970
*/
7071
publicvoidinitDb() {
71-
dbManager.connect();
72+
try {
73+
dbManager.connect();
74+
}catch (DatabaseConnectionExceptione) {
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
packagecom.iluwatar.caching.database;
22

33
importcom.iluwatar.caching.UserAccount;
4+
importcom.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-
voidconnect();
15+
voidconnect()throwsDatabaseConnectionException;
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
importcom.iluwatar.caching.UserAccount;
99
importcom.iluwatar.caching.constants.CachingConstants;
10+
importcom.iluwatar.caching.database.exceptions.DatabaseConnectionException;
1011
importcom.mongodb.MongoClient;
1112
importcom.mongodb.client.MongoDatabase;
1213
importcom.mongodb.client.model.UpdateOptions;
14+
importlombok.extern.slf4j.Slf4j;
1315
importorg.bson.Document;
1416

1517
/**
1618
* Implementation of DatabaseManager.
1719
* implements base methods to work with MongoDb.
1820
*/
21+
@Slf4j
1922
publicclassMongoDbimplementsDbManager {
20-
/**
21-
* Mongo db.
22-
*/
23-
privateMongoDatabasedb;
23+
privatestaticfinalStringDATABASE_NAME ="test";
2424

2525
/**
26-
* Connect to Db.
26+
* Connect to Db. Check th connection
2727
*/
2828
@Override
29-
publicvoidconnect() {
30-
MongoClientmongoClient =newMongoClient();
31-
db =mongoClient.getDatabase("test");
29+
publicvoidconnect()throwsDatabaseConnectionException {
30+
try (MongoClientmongoClient =newMongoClient()) {
31+
mongoClient.getDatabase("test");
32+
}catch (NoClassDefFoundErrore) {
33+
thrownewDatabaseConnectionException("Could not connect to DB.");
34+
}
3235
}
3336

3437
/**
@@ -39,19 +42,23 @@ public void connect() {
3942
*/
4043
@Override
4144
publicUserAccountreadFromDb(finalStringuserId) {
42-
if (db ==null) {
43-
connect();
44-
}
45-
variterable =db
46-
.getCollection(CachingConstants.USER_ACCOUNT)
47-
.find(newDocument(USER_ID,userId));
48-
if (iterable.first() ==null) {
49-
returnnull;
45+
try (MongoClientmongoClient =newMongoClient()) {
46+
MongoDatabasedb =mongoClient.getDatabase(DATABASE_NAME);
47+
variterable =db
48+
.getCollection(CachingConstants.USER_ACCOUNT)
49+
.find(newDocument(USER_ID,userId));
50+
if (iterable.first() ==null) {
51+
returnnull;
52+
}
53+
Documentdoc =iterable.first();
54+
if (doc !=null) {
55+
StringuserName =doc.getString(USER_NAME);
56+
StringappInfo =doc.getString(ADD_INFO);
57+
returnnewUserAccount(userId,userName,appInfo);
58+
}else {
59+
returnnull;
60+
}
5061
}
51-
Documentdoc =iterable.first();
52-
StringuserName =doc.getString(USER_NAME);
53-
StringappInfo =doc.getString(ADD_INFO);
54-
returnnewUserAccount(userId,userName,appInfo);
5562
}
5663

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

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

95102
/**
@@ -100,21 +107,21 @@ public UserAccount updateDb(final UserAccount userAccount) {
100107
*/
101108
@Override
102109
publicUserAccountupsertDb(finalUserAccountuserAccount) {
103-
if (db ==null) {
104-
connect();
110+
try (MongoClientmongoClient =newMongoClient()) {
111+
MongoDatabasedb =mongoClient.getDatabase(DATABASE_NAME);
112+
StringuserId =userAccount.getUserId();
113+
StringuserName =userAccount.getUserName();
114+
StringadditionalInfo =userAccount.getAdditionalInfo();
115+
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
116+
newDocument(USER_ID,userId),
117+
newDocument("$set",
118+
newDocument(USER_ID,userId)
119+
.append(USER_NAME,userName)
120+
.append(ADD_INFO,additionalInfo)
121+
),
122+
newUpdateOptions().upsert(true)
123+
);
124+
returnuserAccount;
105125
}
106-
StringuserId =userAccount.getUserId();
107-
StringuserName =userAccount.getUserName();
108-
StringadditionalInfo =userAccount.getAdditionalInfo();
109-
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
110-
newDocument(USER_ID,userId),
111-
newDocument("$set",
112-
newDocument(USER_ID,userId)
113-
.append(USER_NAME,userName)
114-
.append(ADD_INFO,additionalInfo)
115-
),
116-
newUpdateOptions().upsert(true)
117-
);
118-
returnuserAccount;
119126
}
120127
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
packagecom.iluwatar.caching.database.exceptions;
2+
3+
publicclassDatabaseConnectionExceptionextendsException {
4+
publicDatabaseConnectionException(Strings) {
5+
super(s);
6+
}
7+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp