Broad Network


Locking a Table and Ending a Session with EMySQL API

Using the EMySQL API Part 7

Foreword: In this part of the series, I talk about Locking a Table and Ending a Session with EMySQL API.

By: Chrysanthus Date Published: 28 Jul 2016

Introduction

This is part 7 of my series, Using the EMySQL API. In this part of the series, I talk about Locking a Table and Ending a Session with EMySQL API. You should have read the previous parts of the series before reaching here, as this is a continuation.

Locking Tables
The following code will lock the table, pet for writing.

    var locStr = `LOCK TABLES pet WRITE`;
    con.query(locStr, function(err, OK)
        {
            if (err)
                console.log(err);
            else
                console.log(OK);
        });

Again just place the correct SQL statement without the ending semicolon as argument to the query() function.

After locking you should unlock. The following code unlock all tables locked:

    var unlocStr = `UNLOCK TABLES`;
    con.query(unlocStr, function(err, OK)
        {
            if (err)
                console.log(err);
            else
                console.log(OK);
        });

Ending a Session
The connect statement opens a socket. Any socket opened has to be closed. At the end of your session, you have to close the socket. However, you are not to do the low-level coding; you just use the Mysql package close() function, as follows:

    con.close();

Shutting Down the Server
If you have the privilege to shut down the MySQL server, you can do so. This command should be issued before closing the connection. The shutdown() function shuts down the server (remote) and not the socket. The function is defined in the Mysql package, but in your script, you type something like,

    con.shutdown('', function(err, result)
        {
            if (err)
                console.log(err);
            else
                console.log(result);
        });

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Free Pure ECMAScript MySQL API for Node.js
More Related Links
Node Mailsend
Node.js Web Development Course
Major in Website Design
Writing ECMAScript Module
ECMAScript 2015 Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message