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;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import com.iluwatar.caching.database.DbManager;
|
import com.iluwatar.caching.database.DbManager;
|
||||||
|
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@ -68,7 +69,11 @@ public class AppManager {
|
|||||||
* to (temporarily) store the data/objects during runtime.
|
* to (temporarily) store the data/objects during runtime.
|
||||||
*/
|
*/
|
||||||
public void initDb() {
|
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;
|
package com.iluwatar.caching.database;
|
||||||
|
|
||||||
import com.iluwatar.caching.UserAccount;
|
import com.iluwatar.caching.UserAccount;
|
||||||
|
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>DBManager handles the communication with the underlying data store i.e.
|
* <p>DBManager handles the communication with the underlying data store i.e.
|
||||||
@ -11,7 +12,7 @@ public interface DbManager {
|
|||||||
/**
|
/**
|
||||||
* Connect to DB.
|
* Connect to DB.
|
||||||
*/
|
*/
|
||||||
void connect();
|
void connect() throws DatabaseConnectionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read from DB.
|
* 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.UserAccount;
|
||||||
import com.iluwatar.caching.constants.CachingConstants;
|
import com.iluwatar.caching.constants.CachingConstants;
|
||||||
|
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
import com.mongodb.client.MongoDatabase;
|
import com.mongodb.client.MongoDatabase;
|
||||||
import com.mongodb.client.model.UpdateOptions;
|
import com.mongodb.client.model.UpdateOptions;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of DatabaseManager.
|
* Implementation of DatabaseManager.
|
||||||
* implements base methods to work with MongoDb.
|
* implements base methods to work with MongoDb.
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class MongoDb implements DbManager {
|
public class MongoDb implements DbManager {
|
||||||
/**
|
private static final String DATABASE_NAME = "test";
|
||||||
* Mongo db.
|
|
||||||
*/
|
|
||||||
private MongoDatabase db;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connect to Db.
|
* Connect to Db. Check th connection
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void connect() {
|
public void connect() throws DatabaseConnectionException {
|
||||||
MongoClient mongoClient = new MongoClient();
|
try (MongoClient mongoClient = new MongoClient()) {
|
||||||
db = mongoClient.getDatabase("test");
|
mongoClient.getDatabase("test");
|
||||||
|
} catch (NoClassDefFoundError e) {
|
||||||
|
throw new DatabaseConnectionException("Could not connect to DB.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,19 +42,23 @@ public class MongoDb implements DbManager {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public UserAccount readFromDb(final String userId) {
|
public UserAccount readFromDb(final String userId) {
|
||||||
if (db == null) {
|
try (MongoClient mongoClient = new MongoClient()) {
|
||||||
connect();
|
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
|
@Override
|
||||||
public UserAccount writeToDb(final UserAccount userAccount) {
|
public UserAccount writeToDb(final UserAccount userAccount) {
|
||||||
if (db == null) {
|
try (MongoClient mongoClient = new MongoClient()) {
|
||||||
connect();
|
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
|
@Override
|
||||||
public UserAccount updateDb(final UserAccount userAccount) {
|
public UserAccount updateDb(final UserAccount userAccount) {
|
||||||
if (db == null) {
|
try (MongoClient mongoClient = new MongoClient()) {
|
||||||
connect();
|
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
|
@Override
|
||||||
public UserAccount upsertDb(final UserAccount userAccount) {
|
public UserAccount upsertDb(final UserAccount userAccount) {
|
||||||
if (db == null) {
|
try (MongoClient mongoClient = new MongoClient()) {
|
||||||
connect();
|
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