Here I am building the rental REST API and I want to perform transaction but it not rolling back the changes ,and it not giving any error problem is only changes are not rolling back.
const session = await startSession();
const { error } = validateRental(req.body);
if (error?.details[0].message)
return res.status(400).send(error.details[0].message);
const movie = await Movies.findById(req.body.movieID, null, {
$session: session,
}); // here i am adding the session
const customer = await Customer.findById(req.body.customerID, null, {
$session: session,
}); // same
if (!movie || !customer)
return res.status(400).send("Please check the customer or Movie ID");
try {
session.startTransaction();
movie.numberOfStock--;
customer.numberOfRental++;
await movie.save();
await customer.save();
await session.commitTransaction();
} catch (e) {
console.log(e);
await session.abortTransaction();
res.status(500).send("Internal System error");
} finally {
session.endSession();
}
After spending much time i got to know that my mongodb server is standalone , for write property we need to convert into Replica Set.
Follow the official documentation
Convert Standalone to a Replica set
Main point you should first close your server
My OS is window so - net stop mongob
For ios you can find at out yourself
Related
When i send multi request from frontend (react) to backend (nodejs) simultaneously, the server gives me this error MongoError: WriteConflict. i am using transaction when i want to update some data in database:
code:
const db = require("../../utils/database-connection/database");
const FollowersFollowing = require("../../models/followers-following/followers-following-models");
const session = db.client().startSession();
session.startTransaction();
let whoFollowed;
try {
whoFollowed = await FollowersFollowing.updateFollow(
userId,
targetUserId,
{ session },
"follow"
);
await Notice.updateNotice(
userId,
targetUserId,
{ session },
"showUserOnTargetUserNotice"
);
await session.commitTransaction();
session.endSession();
} catch (error) {
console.log(error);
await session.abortTransaction();
return next(
new HttpError("could not follow the user, please try again.", 500)
);
}
all things works fine but when i want to send multi request (just for testing) the server throw this error.
See https://docs.mongodb.com/manual/core/transactions/ for the correct usage patterns. You must retry on those errors which withTransaction helper does for you.
I'm trying to prevent concurrent requests to a specific record, see the following example:
function addMoney(orderID,orderID){
const status = Database.getOrder(orderID);
if (status === 1){
return "Money Already Added";
}
Database.udateOrder(orderID, {status: 1});
Database.addMoney(userID, 300);
return true;
}
Assume someone made this request exactly at the same time, therefore the "status" check passed, they'd be able to get Database.addMoney run twice.
Using MySQL, I'd start a transction to lock the row but not sure how to do so using MongoDB.
You can do the transactions in mongodb like MySQL. Consider having an order document with id:123 and status:0. Then you can check for status in a transaction and return if it's already paid or fall through in order to add money document and update order status.
If you face any issue like Transaction numbers are only allowed on a replica set member or mongos this link might help.
In order to use transactions, you need a MongoDB replica set, and
starting a replica set locally for development is an involved process.
The new run-rs npm module makes starting replica sets easy.
const uri = 'mongodb://localhost:27017';
const dbName = 'myDB';
const MongoClient = require('mongodb').MongoClient;
async function main() {
const client = new MongoClient(uri);
await client.connect();
const session = client.startSession();
try {
await session.withTransaction(async () => {
const orders = client.db(dbName).collection('orders');
const money = client.db(dbName).collection('money');
let doc = await orders.findOne({orderID: 123});
if (doc && doc.status === 1) {
console.log("Money Already Added");
return
}
await orders.updateOne({orderID: 123}, {'$set': {status: 1}});
await money.insertOne({orderID: 123, userID: 100, amount: 300}, {session});
console.log("Money added");
});
await session.commitTransaction();
} catch (e) {
console.log(e);
} finally {
await session.endSession();
await client.close();
}
}
main()
The code above may need improvement because I couldn't test it on MongoDB with replica set.
Hey everyone i'm developer a simple app in last days using nodejs and create this function to return client instance from mongodb
const mongodb = require("mongodb");
const { db } = require("../config/env");
const conection = async () => {
try {
const client = await mongodb.MongoClient.connect(db.uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
return client;
} catch (error) {
throw error;
}
};
module.exports = conection;
and i make this simple function for acess data layer and return records instered
const index = async ({ limit = 10, offset = 0, filter = {} }) => {
const client = await conection();
if (filter._id) {
filter._id = mongodb.ObjectID(filter._id);
}
try {
const collection = client.db("api").collection("user");
const data = await collection
.find({ ...filter })
.skip(offset)
.limit(limit)
.toArray();
return data;
} catch (error) {
throw new Error(error);
} finally {
await client.close();
}
};
I would like to know if I really need to make the connection and close it with each query or should I keep the connection open
NOTE: in this case I am using a simple Atlas cluster (free) but I would like to know if I should do this also when working with sql banks like postgres
Don't close your connection unless you are exiting your app, and then make sure you do. Ensure that you are using the same connection when you come back to do more I/O. Database connections are resource-intensive and should be established as few times as possible and shared as much as possible. You can also get middleware problems with connections like ODBC if you don't pick up existing connections and get weird errors like connection pools running out. Get to know your connector and how to use it most effectively, it will be a rewarding activity :-)
You can use the mongoose module for managing MongoDB.
Installation
npm install mongoose
Usage
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true });
I am sure that Mongoose can help you solve your issues.
It's a good practice to do so. so that after every operation(insert etc) you close the connection and before every operation(insert etc) you reopen the connection.
I am writing a web application that uses asynchronous database requests as a part of the api. Currently, I have an async express route that awaits function returns from async functions. Both of these functions return booleans and both query the database. One works correctly, however the second one does not.
Here is the MongoClient setup:
const MongoClient = require('mongodb').MongoClient;
const uri = config.uri; // Contains custom url for accessing database
const client = new MongoClient(uri, { useUnifiedTopology: true}, { useNewUrlParser: true }, { connectTimeoutMS: 30000 }, { keepAlive: 1});
where config is from a file imported as.
const config = require("./config.js");
and functions properly.
Here is the express setup:
app.post("/signup", async function(request, response) {
log("POST request at /signup");
log("BEFORE UNIQUE USER");
const isUniqueUser = await validateUniqueUser(request.body.email, request.body.password);
log(isUniqueUser);
const status = {
status: null
};
if (isUniqueUser) {
log("AFTER UNIQUE USER");
let userCreated = await createPracticeProfile(request.body.email, request.body.password);
log("user created: " + userCreated);
if (userCreated) {
status.status = "user_created";
}
response.json(status);
} else {
response.json(status);
}
console.log("********************************end");
});
The console outputs:
BEFORE UNIQUE USER
true (which it should be)
AFTER UNIQUE USER
MongoError: Topology is closed.
user created: undefined
***...***end
Here is the function for validating that a user is unique:
/* VALIDATE_UNIQUE_USER
USE: ensure user does not have existing profile
PARAMS: email (string), password (string)
RETURN: isUniqueUser (bool)
*/
async function validateUniqueUser(email, password) {
// connect to database
const database = await client.connect().catch(err => {
log("ERROR while connecting to database at: validateUniqueUser");
console.log(err);
client.close();
});
// database connection failed
if (!database) {
return false;
}
// connection successful => find user
let user;
try {
user = await database.db("guitar-practice-suite").collection("users").findOne({email: email});
} catch(err) {
log("ERROR while finding user in database at: validateUniqueUser");
console.log(err);
client.close();
return false;
} finally {
client.close();
// user not found (unique)
if (user === null || user === undefined) {
return true;
}
return false;
}
}
Here is the function for inserting the user into the collections:
/* CREATE_PRACTICE_PROFILE
USE: insert a practice profile into the database
PARAMS: email (string), password (string)
RETURN: userCreated (bool)
*/
async function createPracticeProfile(email, password) {
// hash password
let hashedPassword;
try {
hashedPassword = await new Promise((resolve, reject) => {
bcrypt.hash(password, null, null, function(err, hash) {
if (err) {
reject(err);
}
resolve(hash)
});
});
} catch(err) {
log("ERROR while hashing password at: createPracticeProfile");
console.log(err);
return false;
}
// connect to database
const database = await client.connect().catch(err => {
log("ERROR while connecting to database at: validateUniqueUser");
console.log(err);
client.close();
});
// database connection failed
if (!database) {
return false;
}
// database connection successful => insert user into database
let insertUserToUsers;
let insertUserToExercises;
let insertUserToCustomExercises;
try {
insertUserToUsers = await database.db("guitar-practice-suite").collection("users").insertOne({email: email, password: hashedPassword});
insertUserToExercises = await database.db("guitar-practice-suite").collection("exercises").insertOne({email: email});
insertUserToCustomExercises = await database.db("guitar-practice-suite").collection("custom-exercises").insertOne({email: email, exercises: []});
} catch(err) {
log("ERROR while inserting user into database at: createPracticeProfile");
console.log(err);
client.close();
return false;
} finally {
client.close();
return insertUserToUsers && insertUserToExercises && insertUserToCustomExercises;
}
}
I've found the solution to the problem, but I'm not sure I understand the reasoning.
The client.close() in the finally block of the validateUniqueUser function. It was closing the connection before the connection in the createPracticeProfile function was finished inserting the user.
When that line is taken out, the function works.
The issue is client variable needs to be reinstantiated again,
const client = new MongoClient(uri, { useUnifiedTopology: true}, { useNewUrlParser: true }, { connectTimeoutMS: 30000 }, { keepAlive: 1});
Try putting this in start of createPracticeProfile, validateUniqueUser and other functions
I was getting the error
MongoError: Topology is closed
because of the authentication problem
MongoEror: Authentication failed
In my case, the problem was with the password of my database. My password only contained numerical digits.
I changed the password to all characters and both the errors were solved.
Configure your client connection like below example
var MongoClient = require('mongodb').MongoClient;
var Server = require('mongodb').Server;
var mongoClient = new MongoClient(new Server('localhost', 27017));
mongoClient.open(function(err, mongoClient) {
var db1 = mongoClient.db("mydb");
mongoClient.close();
});
In my case - connecting to AtlasDB using the MongoClient - I had to whitelist the IP i was accessing the cluster from
I think your mongodb service is stopped, to start it
Task Manager -> Services -> Mongodb -> RightClick -> Start
My code has been working fine for a long time and hasn't thrown this error before: MongoError: Topology is closed.
But due to the fact that my laptop was turned on for a long time and I was simultaneously developing other projects on it, while the main one was running in the terminal, mongo most likely did not close one of the connections to the database and opened another in parallel, creating some kind of collision.
In general, in my case, the usual restart of the computer helped and a similar error did not occur again.
I have the following code, and can't understand why my process hangs on the line that tries to close the connection to mongodb, here is my code:
async function save(clientCredentials, newFieldsToUpdate){
const url = `mongodb://${clientCredentials.username}:${clientCredentials.password}#my.server.ip:22222/${clientCredentials.database}`
const client = await MongoClient.connect(url, {useNewUrlParser:true, useUnifiedTopology: true})
.catch(err => { console.log(err); });
const db = client.db(clientName);
const collection = await db.collection("products");
let execute = false;
const updateOps = [];
for(let objectIdentifier in newFieldsToUpdate){
let updateOperation = {};
updateOperation['$set'] = newFieldsToUpdate[objectIdentifier];
let id = mongodb.ObjectID(objectIdentifier);
execute = true;
updateOps.push({ updateOne: { filter: {_id: id}, update: {$set: newFieldsToUpdate[objectIdentifier]}, upsert:true } })
}
if(execute){
try {
console.log('executing'); // I see this line
let report = await collection.bulkWrite(updateOps);
console.log('executed'); // I see this line
await client.close();
console.log('closed conn'); // I don't see this line! why? it's weird
return report;
} catch(ex){
console.error(ex);
}
} else {
console.log('not executing');
}
}
Thank you in advance for any help!
EDIT: The bulk operation is of about 200 documents only, if I try with a single document it works, this is weird. Mongodb driver for node version 3.3.2
EDIT2: I notice that using the parameter poolSize:1 on the mongo connect it closes the connection with success, but using the default poolSize of 5 it doesnt close, any suggestions why this might be happening?
Make sure you are using latest version of mongodb driver (delete node_modules just to be sure) as older version has a bug in which bulkWrite method fails silently because of some bug mentioned here - https://stackoverflow.com/a/46700933/1021796
Before trying to close the connection, you shou also check if it is connected or not. so code will be
if (client.isConnected) {
await client.close();
}
Hope this helps