@@ -317,7 +317,8 @@ Rather than creating and managing connections one-by-one, this module also
317
317
provides built-in connection pooling using ` mysql.createPool(config) ` .
318
318
[ Read more about connection pooling] ( https://en.wikipedia.org/wiki/Connection_pool ) .
319
319
320
- Use pool directly.
320
+ Create a pool and use it directly:
321
+
321
322
``` js
322
323
var mysql = require (' mysql' );
323
324
var pool = mysql .createPool ({
@@ -334,34 +335,22 @@ pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
334
335
});
335
336
```
336
337
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:
356
343
357
344
``` js
358
345
var mysql = require (' mysql' );
359
346
var pool = mysql .createPool (... );
360
347
361
348
pool .getConnection (function (err , connection ) {
349
+ if (err) throw err; // not connected!
350
+
362
351
// Use the connection
363
352
connection .query (' SELECT something FROM sometable' , function (error , results , fields ) {
364
- // And done with the connection.
353
+ // When done with the connection, release it .
365
354
connection .release ();
366
355
367
356
// Handle error after the release.
0 commit comments