I am using AWS DocumentDB as my database in my application which is developed in node.js which is MongoDB compatible. I have used MongoClient to connect to my database. My application executes but it makes approx 1000 DB connections for each execution and then it fails with an error:
MongoServerSelectionError: connection <monitor> to xxx.xx.xx.xxx:27017 closed.
I tried to use client.close() as well to close the connections then it gave that the connection pool is closed. I believe that issue is due to so many database connections.
How can I release the connections in MongoDB? Any help is appreciated.
It appears from your question that what is happening is that you are defining a new MongoClient object each time you invoke your function resulting in the driver creating a new database connection with each function call. Instead, define the client connection outside the AWS Lambda handler function and reuse the connection with each function call.
Related
I'm using SQLAlchemy inside a Lambda with RDS Proxy (PostgreSQL).
This configuration works, but when I'm invoking about 100 lambdas at the same time some lambdas receive a timeout (3 seconds) even when I'm increasing the timeout for 30 seconds I still get a timeout.
After investigating where this timeout comes from, I concluded that the SQLAlchemy hangs when it tries to get a connection to the database.
I thought the RDS proxy could easily handle 100+ concurrent connections.
Here is my engine configuration:
db_url = f"postgresql://{db_username}:{secret}#{db_hostname}:{db_port}/{db_name}"
engine = create_engine(
db_url,
echo=True,
echo_pool="debug",
poolclass=NullPool)
Usually, when SQLAlchemy creates a connection it logs the following:
DEBUG sqlalchemy.pool.impl.NullPool Created new connection <connection object at 0x7f074ebe5580; dsn: 'user=user password=xxx dbname=dbname host=rds-default.{rds-proxy-id}.{region}.rds.amazonaws.com port=port', closed: 0>
But sometimes it will just hang and won't log anything and eventually timeout.
Any help would be appreciated
Spring boot application.
MongoDb Database hosted on mongodb cloud.
Application propperties
#Database connection string
spring.data.mongodb.host=cluster0.mbdhj.mongodb.net
spring.data.mongodb.port=27017
spring.data.mongodb.database=sample_supplies
spring.data.mongodb.username=user
spring.data.mongodb.password=[my password]
spring.data.mongodb.auto-index-creation=true
Error
com.mongodb.MongoSocketException: No such host is known (cluster0.mbdhj.mongodb.net)
If I use the same host/port/user/password combination with MongoDb Compass, i can connect without issues.
If I use a cluster connection like this
spring.data.mongodb.host=cluster0-shard-00-02.mbdhj.mongodb.net
the application throws error
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
I've opened the firewall, to accept any connection.
Analitics show some actions, but i still get error in Spring.
I was just following a single youtube tutorial. In their example this didn't cause any issue.
What am I doing wrong?
Should I write a class that connects via Java to the database?
Mongo cloud instances do not allow direct connection while debugging
I'm accessing mongo db (v2.6) through java driver (v2.10) (through spring data). When the application receive a socket timeout exception, is the underlying connection closed as well?
The connection is getting closed, but spring automatically establishes a new connection. So u need not create a new connection.
Assuming that no statements to close the connection are made before my script ends and no exception is encountered before closing the connection, does the database's connection stay open?
I'm connecting to the database programmatically via Python Psycopg2 and via Java JDBC4 driver.
Not entirely sure what you want exactly, but let's try:
You can see the connections that exist at any time with PGAdmin or this SQL command
SELECT * FROM pg_stat_activity;
It should be fairly simple to spot when - for your specific use case - the connection closes.
If an SQL query is running at the time you close a connection, I think it will run to completion, ie the backend serving it will remain alive, even if the connection is closed from the client side.
I fixed and use Connection Pool to get the DB Connection from JSP to Oracle Database now.
But then I found out from the OC4J console at once that there are many UNCLOSED/NOT-CLOSED connection.
I am using OC4J 9.0.4.1 and Oracle 10g. Is there any effective way to find out the location or area that the connection is not closed.
In addition, I would like to know the followings:
1) The UNCLOSED connection means the Connection or ResultSet or Statment ?
2) IF Connection for the ResultSet and Statement is closed, the corresponding ResultSet and Statement are closed automatically too ?
I'd expect that this is easiest to detect on the server side - one assumes (!) that there is a 'show me all the open connections' report or command on the server that should show up a list of those connections together with the last executed SQL on that connection (am coming from the MSSQL side of things here - this doesn't sound like too much of a stretch, tho').
Unclosed means that the connection has not been closed.