BlogTechnologyAre You Struggling with Slow Database Connections? Discover Connection Pooling!

Are You Struggling with Slow Database Connections? Discover Connection Pooling!

  • Slow Query Execution: Establishing a new connection involves network latency along with the overhead of authentication and authorization.
  • Resource Exhaustion: Multiple clients requesting connections can lead to server limits being exceeded, resulting in frustrating connection errors.
  • Connection Timeouts: Lengthy queries might unexpectedly lose their connections midway through execution.

What is Connection Pooling and How Does It Work?

  1. Creating a Connection Pool: A fixed number of database connections are initialized and maintained within the pool.
  2. Connection Reuse: When a query is executed, an available connection from the pool is provided, eliminating the need to establish a new one.
  3. Connection Release: After the query finishes, the connection is returned to the pool, ready for the next query.
  4. Handling Limits: If all connections are occupied, new requests either wait in a queue or time out if they exceed the pool’s threshold.

Code Implementation: Node.js Connection Pool

JavaScript

  • connectTimeout: The time (in milliseconds) allowed for establishing a connection to the database before timing out. Ideal for remote databases with potential network latency.
  • acquireTimeout: The time (in milliseconds) to wait while acquiring a connection from the pool before throwing an error.
  • timeout: The duration (in milliseconds) before the connection itself times out during inactivity. Useful for preventing connections from hanging indefinitely.
  • host: The hostname or IP address of the MySQL server. Use localhost for local testing or a cloud server URL for production environments.
  • user: The username for authenticating with the MySQL database.
  • password: The password associated with the provided username.
  • port: The port number on which the MySQL server is running (default is 3306).
  • connectionLimit: The maximum number of connections allowed in the pool at any given time. For instance, use 4 if the database server supports only five concurrent connections.
  • multipleStatements: Allows executing multiple SQL statements in a single query. Useful for batch updates but comes with an increased risk of SQL injection attacks.
  • maxIdleTime: The duration (in milliseconds) a connection can remain idle in the pool before being closed. Setting it to 30000 helps free up unused resources quickly.
  • database: The name of the MySQL database to use for executing queries.
  • charset: Defines the character set for the connection. For example, use utf8mb4 if your data includes emojis or special characters.
  • Connection Acquisition: The function retrieves a connection from the pool using pool.getConnection(), rejecting the promise if there’s an error (e.g., if the pool is exhausted).
  • Executing the Query: Once a connection is acquired, it executes the SQL query with connection.query(), passing the query and its parameters.
  • Connection Release: After the query finishes, the connection is returned to the pool using connection.release(). This step is crucial to prevent connection leaks, which can exhaust the pool and block new queries.
  • Promise Handling: The function handles the query results with promises—rejecting if there’s an error, and resolving with the retrieved data (rows) if the query is successful.

Example Usage of the query Function in Code

  • You can define this function in your Data Access (DA) layer and use it in your controller for execution, promoting better separation of concerns in your application architecture.
  • This approach allows your controllers to focus on handling requests and responses while the DA layer efficiently manages database operations.

How it Works:

  • Promise Creation: The function returns a Promise, simplifying the handling of asynchronous operations.
  • Connection Request: It uses pool.getConnection() to request a connection from the pool, performing an asynchronous operation that either provides a connection or returns an error.
  • Error Handling: If an error occurs while acquiring the connection (e.g., pool exhaustion or network issues), it logs the error message and resolves the promise with false, informing the caller of the unsuccessful connection attempt.
  • Successful Connection: If a connection is successfully acquired, it’s released back to the pool using connection.release(). The promise resolves with true, indicating a successful connection, along with a success log message.

Bootstrapping the MySQL Connection

Benefits of Bootstrapping

  • Early Validation: By checking the database connection at startup, you can catch any potential issues early in the application lifecycle, avoiding runtime errors down the line.
  • Better Error Handling: You can decide to halt the server or take alternative actions before the application starts processing requests.

Conclusion

Software Developer



Creating digital solutions for your business

Subscribe

Subscribe to stay updated with our latest Tech News & Blogs

Copyright Synclovis System Pvt. Ltd. © 2024. All Rights Reserved
  • About Us
  • Services
  • Industries
  • Technologies
  • Portfolio
  • Blog