Skip to content

Commit ad014c8

Browse files
borekbdougwilson
authored andcommitted
docs: add more info about pool.query vs pool.getConnection
closes #1668 closes #2031
1 parent 55265b8 commit ad014c8

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

Readme.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ Rather than creating and managing connections one-by-one, this module also
317317
provides built-in connection pooling using `mysql.createPool(config)`.
318318
[Read more about connection pooling](https://en.wikipedia.org/wiki/Connection_pool).
319319

320-
Use pool directly.
320+
Create a pool and use it directly:
321+
321322
```js
322323
var mysql = require('mysql');
323324
var pool = mysql.createPool({
@@ -334,34 +335,22 @@ pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
334335
});
335336
```
336337

337-
Connections can be pooled to ease sharing a single connection, or managing
338-
multiple connections.
339-
340-
```js
341-
var mysql = require('mysql');
342-
var pool = mysql.createPool({
343-
host : 'example.org',
344-
user : 'bob',
345-
password : 'secret',
346-
database : 'my_db'
347-
});
348-
349-
pool.getConnection(function(err, connection) {
350-
// connected! (unless `err` is set)
351-
});
352-
```
353-
354-
When you are done with a connection, just call `connection.release()` and the
355-
connection will return to the pool, ready to be used again by someone else.
338+
This is a shortcut for the `pool.getConnection()` -> `connection.query()` ->
339+
`connection.release()` code flow. Using `pool.getConnection()` is useful to
340+
share connection state for subsequent queries. This is because two calls to
341+
`pool.query()` may use two different connections and run in parallel. This is
342+
the basic structure:
356343

357344
```js
358345
var mysql = require('mysql');
359346
var pool = mysql.createPool(...);
360347

361348
pool.getConnection(function(err, connection) {
349+
if (err) throw err; // not connected!
350+
362351
// Use the connection
363352
connection.query('SELECT something FROM sometable', function (error, results, fields) {
364-
// And done with the connection.
353+
// When done with the connection, release it.
365354
connection.release();
366355

367356
// Handle error after the release.

0 commit comments

Comments
 (0)