Fix Bug with mongo connection. Used "Try with resources"
This commit is contained in:
parent
49039843e5
commit
50755b7215
@ -24,6 +24,7 @@
|
||||
package com.iluwatar.caching;
|
||||
|
||||
import com.iluwatar.caching.database.DbManager;
|
||||
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
|
||||
import java.util.Optional;
|
||||
|
||||
import lombok.Data;
|
||||
@ -68,7 +69,11 @@ public class AppManager {
|
||||
* to (temporarily) store the data/objects during runtime.
|
||||
*/
|
||||
public void initDb() {
|
||||
dbManager.connect();
|
||||
try {
|
||||
dbManager.connect();
|
||||
} catch (DatabaseConnectionException e) {
|
||||
LOGGER.error("Could not connect to DB: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.iluwatar.caching.database;
|
||||
|
||||
import com.iluwatar.caching.UserAccount;
|
||||
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
|
||||
|
||||
/**
|
||||
* <p>DBManager handles the communication with the underlying data store i.e.
|
||||
@ -11,7 +12,7 @@ public interface DbManager {
|
||||
/**
|
||||
* Connect to DB.
|
||||
*/
|
||||
void connect();
|
||||
void connect() throws DatabaseConnectionException;
|
||||
|
||||
/**
|
||||
* Read from DB.
|
||||
|
@ -7,28 +7,31 @@ import static com.iluwatar.caching.constants.CachingConstants.USER_NAME;
|
||||
|
||||
import com.iluwatar.caching.UserAccount;
|
||||
import com.iluwatar.caching.constants.CachingConstants;
|
||||
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.Document;
|
||||
|
||||
/**
|
||||
* Implementation of DatabaseManager.
|
||||
* implements base methods to work with MongoDb.
|
||||
*/
|
||||
@Slf4j
|
||||
public class MongoDb implements DbManager {
|
||||
/**
|
||||
* Mongo db.
|
||||
*/
|
||||
private MongoDatabase db;
|
||||
private static final String DATABASE_NAME = "test";
|
||||
|
||||
/**
|
||||
* Connect to Db.
|
||||
* Connect to Db. Check th connection
|
||||
*/
|
||||
@Override
|
||||
public void connect() {
|
||||
MongoClient mongoClient = new MongoClient();
|
||||
db = mongoClient.getDatabase("test");
|
||||
public void connect() throws DatabaseConnectionException {
|
||||
try (MongoClient mongoClient = new MongoClient()) {
|
||||
mongoClient.getDatabase("test");
|
||||
} catch (NoClassDefFoundError e) {
|
||||
throw new DatabaseConnectionException("Could not connect to DB.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,19 +42,23 @@ public class MongoDb implements DbManager {
|
||||
*/
|
||||
@Override
|
||||
public UserAccount readFromDb(final String userId) {
|
||||
if (db == null) {
|
||||
connect();
|
||||
try (MongoClient mongoClient = new MongoClient()) {
|
||||
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
|
||||
var iterable = db
|
||||
.getCollection(CachingConstants.USER_ACCOUNT)
|
||||
.find(new Document(USER_ID, userId));
|
||||
if (iterable.first() == null) {
|
||||
return null;
|
||||
}
|
||||
Document doc = iterable.first();
|
||||
if (doc != null) {
|
||||
String userName = doc.getString(USER_NAME);
|
||||
String appInfo = doc.getString(ADD_INFO);
|
||||
return new UserAccount(userId, userName, appInfo);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
var iterable = db
|
||||
.getCollection(CachingConstants.USER_ACCOUNT)
|
||||
.find(new Document(USER_ID, userId));
|
||||
if (iterable.first() == null) {
|
||||
return null;
|
||||
}
|
||||
Document doc = iterable.first();
|
||||
String userName = doc.getString(USER_NAME);
|
||||
String appInfo = doc.getString(ADD_INFO);
|
||||
return new UserAccount(userId, userName, appInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,15 +69,15 @@ public class MongoDb implements DbManager {
|
||||
*/
|
||||
@Override
|
||||
public UserAccount writeToDb(final UserAccount userAccount) {
|
||||
if (db == null) {
|
||||
connect();
|
||||
try (MongoClient mongoClient = new MongoClient()) {
|
||||
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
|
||||
db.getCollection(USER_ACCOUNT).insertOne(
|
||||
new Document(USER_ID, userAccount.getUserId())
|
||||
.append(USER_NAME, userAccount.getUserName())
|
||||
.append(ADD_INFO, userAccount.getAdditionalInfo())
|
||||
);
|
||||
return userAccount;
|
||||
}
|
||||
db.getCollection(USER_ACCOUNT).insertOne(
|
||||
new Document(USER_ID, userAccount.getUserId())
|
||||
.append(USER_NAME, userAccount.getUserName())
|
||||
.append(ADD_INFO, userAccount.getAdditionalInfo())
|
||||
);
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -81,15 +88,15 @@ public class MongoDb implements DbManager {
|
||||
*/
|
||||
@Override
|
||||
public UserAccount updateDb(final UserAccount userAccount) {
|
||||
if (db == null) {
|
||||
connect();
|
||||
try (MongoClient mongoClient = new MongoClient()) {
|
||||
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
|
||||
Document id = new Document(USER_ID, userAccount.getUserId());
|
||||
Document dataSet = new Document(USER_NAME, userAccount.getUserName())
|
||||
.append(ADD_INFO, userAccount.getAdditionalInfo());
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT)
|
||||
.updateOne(id, new Document("$set", dataSet));
|
||||
return userAccount;
|
||||
}
|
||||
Document id = new Document(USER_ID, userAccount.getUserId());
|
||||
Document dataSet = new Document(USER_NAME, userAccount.getUserName())
|
||||
.append(ADD_INFO, userAccount.getAdditionalInfo());
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT)
|
||||
.updateOne(id, new Document("$set", dataSet));
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -100,21 +107,21 @@ public class MongoDb implements DbManager {
|
||||
*/
|
||||
@Override
|
||||
public UserAccount upsertDb(final UserAccount userAccount) {
|
||||
if (db == null) {
|
||||
connect();
|
||||
try (MongoClient mongoClient = new MongoClient()) {
|
||||
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
|
||||
String userId = userAccount.getUserId();
|
||||
String userName = userAccount.getUserName();
|
||||
String additionalInfo = userAccount.getAdditionalInfo();
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
|
||||
new Document(USER_ID, userId),
|
||||
new Document("$set",
|
||||
new Document(USER_ID, userId)
|
||||
.append(USER_NAME, userName)
|
||||
.append(ADD_INFO, additionalInfo)
|
||||
),
|
||||
new UpdateOptions().upsert(true)
|
||||
);
|
||||
return userAccount;
|
||||
}
|
||||
String userId = userAccount.getUserId();
|
||||
String userName = userAccount.getUserName();
|
||||
String additionalInfo = userAccount.getAdditionalInfo();
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
|
||||
new Document(USER_ID, userId),
|
||||
new Document("$set",
|
||||
new Document(USER_ID, userId)
|
||||
.append(USER_NAME, userName)
|
||||
.append(ADD_INFO, additionalInfo)
|
||||
),
|
||||
new UpdateOptions().upsert(true)
|
||||
);
|
||||
return userAccount;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.iluwatar.caching.database.exceptions;
|
||||
|
||||
public class DatabaseConnectionException extends Exception {
|
||||
public DatabaseConnectionException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user