0

I'm porting a working backend written in Python to .NET microservice written in C#, using the MySQLConnector and mySQL. I have a simple C# Database class instantiated from Program main as one of the first lines. It connects to the mysql service, then checks for existence of DB and creates it and the tables if needed, then populates it with a few seed records to make it operational.

I'm trying to do this with proper error checking and structure and learn the right way to do things.

My problems are:

  • I'm not understanding how to structure the async code properly.
  • One of the Async calls is blocking and never returning
  • I'm not sure how to do an Async call to get a connection for a method that needs to make a query

Below is my current class, followed by my questions, and the original working Python database module below that. Any help, tips or tricks appreciated.

using System;
using System.Text.RegularExpressions;
using MySqlConnector;

// MySqlConnector notes and refs
// https://mysqlconnector.net/api/mysqlconnector/mysqlerrorcodetype/
// https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html
// Adventureworks naming conventions: 
// https://stackoverflow.com/questions/3593582/database-naming-conventions-by-microsoft

public class Database
{
    static private Database instance;
    private bool isReady;

    private string dbName;
    private string dbUser;
    private string dbPassword;
    private string dbHost;
    private string dbPort;
    private string connectionString;

    public Database(string dbName, string dbUser, string dbPassword, string dbHost, string dbPort)
    {
        this.dbName = dbName;
        this.dbUser = dbUser;
        this.dbPassword = dbPassword;
        this.dbHost = dbHost;
        this.dbPort = dbPort;
        this.connectionString = $"server={dbHost};user={dbUser};port={dbPort};password={dbPassword}";
        instance = this;
        Initialize();
    }

    // is this the right approach for various methods in the service to get a connection?
    static public MySqlConnection Connect()
    {
        try
        {
            var connection = new MySqlConnection(instance.connectionString);
            connection.Open();

            var command = new MySqlCommand($"USE {instance.dbName}", connection);
            command.ExecuteNonQuery();

            return connection;
        }
        catch (MySqlException ex)
        {
            switch ((MySqlErrorCode)ex.Number)
            {
                case MySqlErrorCode.AccessDenied:
                    Console.WriteLine("Access denied: Something is wrong with your user name or password");
                    break;
                case MySqlErrorCode.UnknownDatabase:
                    Console.WriteLine($"Database {instance.dbName} does not exist");
                    break;
                default:
                    Console.WriteLine(ex.Message);
                    break;
            }

            Environment.Exit(1);
            return null;  // this line will never be reached, but it's necessary to satisfy the return type of the function.
        }
    }

    static public bool IsReady()
    {
        return (instance != null && instance.isReady);
    }

    //*************************************************************************
    // Private methods
    private async Task Initialize()
    {
        // create a connection and try to open the DB service
        using var connection = new MySqlConnection(connectionString);
        try
        {
            await connection.OpenAsync();
        }
        catch (MySqlException exConnection)
        {
            Console.WriteLine(exConnection.Message);
            Environment.Exit(1);
        }

        // service exists, now check if our database exists
        try
        {
            // if it got here, then it found it, need to make it active via USE
            using var command = new MySqlCommand($"USE {dbName}", connection);
            await command.ExecuteNonQueryAsync();
        }
        catch (MySqlException exUseDB)
        {
            if ((MySqlErrorCode)exUseDB.Number == MySqlErrorCode.UnknownDatabase)
            {
                Console.WriteLine($"Database {dbName} does not exist");
                Console.WriteLine($"Creating {dbName}...");
                // create the database...
                try
                {
                    // load the schema file and run the sql to create the db and tables
                    string schema = File.ReadAllText("CLIService/SQL/bbe-schema.sql");
                    string[] sqlCommands = schema.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                    // create the tables
                    foreach (string sqlCommand in sqlCommands)
                    {
                        string cleanedCommand = Regex.Replace(sqlCommand, "[\n\r]", "");

                        if (!string.IsNullOrEmpty(cleanedCommand))
                        {
                            try
                            {
                                var command = new MySqlCommand(cleanedCommand, connection);
                                await command.ExecuteNonQueryAsync();
                            }
                            catch (MySqlException exSqlCommand)
                            {
                                Console.WriteLine(exSqlCommand.Message);
                                Environment.Exit(1);
                            }
                        }
                    }
                }
                catch (MySqlException exCreateDB)
                {
                    Console.WriteLine($"Failed creating database: {exCreateDB.Message}");
                    Environment.Exit(1);
                }
            }
            else
            {
                Console.WriteLine(exUseDB.Message);
                Environment.Exit(1);
            }
        }

        // database exists and is used, now populate it if empty
        try
        {
            // there is always 1 admin account created by default, so try to get one record
            var command = new MySqlCommand("SELECT * FROM account LIMIT 1", connection);
            // should I use ExecuteReaderAsync() instead? If so how?
            using (var reader = command.ExecuteReader())
            {
                // if no records, means it's a new, empty database
                if (!reader.HasRows)
                {
                    // load the data file and run the sql to create the table records of initial data
                    string schema = File.ReadAllText("CLIService/SQL/bbe-data.sql");
                    string[] sqlCommands = schema.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                    // create the records
                    foreach (string sqlCommand in sqlCommands)
                    {
                        string cleanedCommand = Regex.Replace(sqlCommand, "[\n\r]", "");

                        if (!string.IsNullOrEmpty(cleanedCommand))
                        {
                            try
                            {
                                command = new MySqlCommand(cleanedCommand, connection);
#if USE_ASYNC_AWAIT
                                // the following line if used it hangs/blocks and never returns
                                await command.ExecuteNonQueryAsync();
#else
                                // alternatively the following line returns: "56 Inserted System.Threading.Tasks.Task`1[System.Int32] records"
                                // but no records are inserted into the DB
                                var rowCount = command.ExecuteNonQueryAsync();
                                Console.WriteLine($"Inserted {rowCount} records");
#endif
                            }
                            catch (MySqlException exSqlCommand)
                            {
                                Console.WriteLine(exSqlCommand.Message);
                                Environment.Exit(1);
                            }
                        }
                    }
                }
            }
        }
        catch (MySqlException exSelectDB)
        {
            Console.WriteLine($"An error occurred: {exSelectDB.Message}");
        }

        // database opened, selected and populated
        isReady = true;
        Console.WriteLine("Database is ready");
    }
}

Questions:

  1. What am I doing wrong in the Initialize() method for the USE_ASYNC_AWAIT section of the code? The current code successful connects to the service, detects the DB is missing and creates it and the tables using a file of sql commands, detects there are no records, loads the data file and loops through the insert commands. In the code above where is says #if USE_ASYNC_AWAIT, if USE_ASYNC_AWAIT is true then await command.ExecuteNonQueryAsync blocks and never returns, and if false it completes with 56 tasks created but no records are ever created.
  2. Is the overall structure of Initialize method the right way to do things and handle exceptions?
  3. How should the Connect function be structured using Async when services ask for a connection to run queries on?

Original working Python backend:

import mysql
from mysql.connector import errorcode
import re
import os
import tomllib


def connect(config):
    # open the database connection
    try:
        connection = mysql.connector.connect(pool_name="bbepool", **config["DATABASE"]["CONNECTION"])
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Access denied: Something is wrong with your user name or password")
        else:
            print(err)
            exit(1)
    # set the active database
    name = config["DATABASE"]["NAME"]
    connection.name = name
    cursor = connection.cursor()
    try:
        cursor.execute("USE {}".format(name))
    except mysql.connector.Error as err:
        print(err)
        exit(1)

    return connection


def initialize(config):
    connection = None
    cursor = None

    # open the database connection
    try:
        connection = mysql.connector.connect(**config["DATABASE"]["CONNECTION"])
        cursor = connection.cursor()
    except mysql.connector.Error as err:
        print(err)
        exit(1)

    # connection successful, check if the db exists already
    name = config["DATABASE"]["NAME"]
    try:
        cursor.execute("USE {}".format(name))
    except mysql.connector.Error as err:
        print("Database {} does not exist".format(name))
        if err.errno == errorcode.ER_BAD_DB_ERROR:
            print("Creating {}...".format(name))
            try:
                cursor.execute("CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8mb4'".format(name))
            except mysql.connector.Error as err:
                print("Failed creating database: {}".format(err))
                exit(1)

            print("Database {} created successfully".format(name))
            # set the active database
            connection.database = name
        else:
            print(err)
            exit(1)

    # also set connection to use the new database
    config["DATABASE"]["CONNECTION"]["database"] = config["DATABASE"]["NAME"]

    # load and run schema
    # safe to do as it doesn't create tables if they exist
    with open(config["DATABASE"]["SCHEMA"], "rb") as f:
        tables = f.read().decode("utf-8").split(";")
        for table in tables:
            table = re.sub("[\n\r]", "", table)
            if len(table) > 0:
                try:
                    cursor.execute(table)
                    connection.commit()
                except mysql.connector.Error as err:
                    print(err)
                    exit(1)

    # if there is test data
    if config["DATABASE"]["DATA"] != "":
        # if the db is empty
        cursor.execute("SELECT * FROM account LIMIT 1")
        # must fetch the data for the cursor
        cursor.fetchone()
        if cursor.rowcount < 1:
            # populate
            with open(config["DATABASE"]["DATA"], "rb") as f:
                text = f.read().decode("utf-8")
                lines = text.split(");\n")
                for line in lines:
                    if len(line) > 0:
                        try:
                            cursor.execute(line + ");")
                            connection.commit()
                        except mysql.connector.Error as err:
                            print(err)
                            exit(1)

    # cleanup
    cursor.close()
    connection.close()


if __name__ == "__main__":
    # ensure the current working directory is same as script
    os.chdir(os.path.dirname(__file__))

    # module vars we use multiple places
    config = {}

    # In VS Code we can test development or production by setting
    # "DEPLOYMENT_ENVIRONMENT": "development", "testing" or "production" in .vscode/launch.json
    #  but in case the environment variable isn't set, make a default to development
    if "DEPLOYMENT_ENVIRONMENT" not in os.environ:
        os.environ["DEPLOYMENT_ENVIRONMENT"] = "development"
    config_file = "../Settings/config." + os.environ["DEPLOYMENT_ENVIRONMENT"] + ".toml"
    with open(config_file, "rb") as f:
        config = tomllib.load(f)

    # initialize database before switching directories
    initialize(config)

The Program main which uses the Database class:

using System;
using System.Data;

// https://mysqlconnector.net/tutorials/basic-api/
using MySqlConnector;
using CLIShared;

public class Program
{
    static public void Main()
    {
        string dbName = Environment.GetEnvironmentVariable("DB_NAME");
        string dbUser = Environment.GetEnvironmentVariable("DB_USER");
        string dbPassword = Environment.GetEnvironmentVariable("DB_PASSWORD");
        string dbHost = Environment.GetEnvironmentVariable("DB_HOST");
        string dbPort = Environment.GetEnvironmentVariable("DB_PORT");

        Database db = new Database(dbName, dbUser, dbPassword, dbHost, dbPort);

        // wait for db to be ready
        while (Database.IsReady() == false)
        {
            System.Threading.Thread.Sleep(1000);
        }

        Console.WriteLine("Database successfully initialized!");
    }
}
13
  • You shouldnt call async functions from non async function (if you can avoid to) see: stackoverflow.com/a/40329285/18278998. Commented Jun 6, 2024 at 9:00
  • in your #if USE_ASYNC_AWAIT you try to access rowcount which will simply return a Task which won't have the result you expect from it. You should split the functions to async and sync seperatly. Altough I don't completely understand why you would run your code synchronously in this case. I would remove the #if USE_ASYNC_AWAIT alltogether. Commented Jun 6, 2024 at 9:09
  • Thanks for your reply. I just put the 2 different versions in there for testing. I want everything async but unsure how to use command.ExecuteNonQueryAsync correctly. The Initialize method is async, so I am trying not to mix. How can I correct? Commented Jun 6, 2024 at 9:15
  • 2
    Don't call USE, just set the database in the connection string. Don't execute queries on a connection while another query is ongoing, retrieve the full query then execute the next one. catch ...Console.WriteLine ... Environment.Exit(1); is stupid, you might as well let the Unhandled Exception Handler sort it out and remove all your catch blocks. Commented Jun 6, 2024 at 11:16
  • 2
    I haven't looked too closely, but you have var command = new MySqlCommand("SELECT * FROM account LIMIT 1", connection);, var reader = command.ExecuteReader(), then within this code block you attempt to create a new instance: command = new MySqlCommand(cleanedCommand, connection);. This seems like trying to change the tires on a car while you're driving it. Commented Jun 6, 2024 at 14:11

1 Answer 1

0

After reading some MS articles and trying various things, I came up with a working version (below). I'm still unsure if it's perfectly structured, but it:

  • Handles starting up at any stage of database readiness (no service, service but no DB, DB but not data).
  • Uses Async everywhere
  • Initialize() is simplified as the .NET connector doesn't have issues with executing the SQL commands as a single batch, as well as it's now organized in 3 separate logical chunks that correspond to the DB state (no service, no DB, no data)
  • Getting a connection is simplified as the Async connection is already pooled, so can simply return it functions that need it
using System;
using System.Text.RegularExpressions;
using MySqlConnector;

// MySqlConnector notes and refs
// https://mysqlconnector.net/api/mysqlconnector/mysqlerrorcodetype/
// https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html
// Adventureworks naming conventions: 
// https://stackoverflow.com/questions/3593582/database-naming-conventions-by-microsoft

// async and await coding
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/
// https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming

public class Database
{
    static public MySqlConnection? Connection { get { return instance != null ? instance.connection : null; } }

    static private Database? instance;
    private bool isReady;
    private string dbName;
    private string dbUser;
    private string dbPassword;
    private string dbHost;
    private string dbPort;
    private string connectionString;
    private MySqlConnection connection;

    //*************************************************************************
    // constructor
    public Database(string dbName, string dbUser, string dbPassword, string dbHost, string dbPort)
    {
        this.dbName = dbName;
        this.dbUser = dbUser;
        this.dbPassword = dbPassword;
        this.dbHost = dbHost;
        this.dbPort = dbPort;
        this.connectionString = $"server={dbHost};user={dbUser};port={dbPort};password={dbPassword}";
        this.connection = new MySqlConnection(connectionString);
        instance = this;
        Initialize().Wait();
    }

    //*************************************************************************
    // public methods
    static public bool IsReady()
    {
        return (instance != null && instance.isReady);
    }

    //*************************************************************************
    // private methods
    public async Task Initialize()
    {
        // create a connection and try to open the DB service
        connection = new MySqlConnection(connectionString);
        var command = new MySqlCommand();
        try
        {
            await connection.OpenAsync();
            command.Connection = connection;
        }
        catch (MySqlException exConnection)
        {
            Console.WriteLine(exConnection.Message);
            Environment.Exit(1);
        }

        // service exists, now check if our database exists
        try
        {
            // if it got here, then it found it, need to make it active via USE
            command.CommandText = $"USE {dbName}";
            await command.ExecuteNonQueryAsync();
        }
        catch (MySqlException exUseDB)
        {
            if ((MySqlErrorCode)exUseDB.Number == MySqlErrorCode.UnknownDatabase)
            {
                Console.WriteLine($"Database {dbName} does not exist");
                Console.WriteLine($"Creating {dbName}...");
                // create the database...
                try
                {
                    // execute the schema all at once with a single command
                    // this didn't work in Python due to connector implementation (I believe)
                    // but it works in C# with MySqlConnector
                    // load the schema file and run the sql to create the db and tables
                    string schema = File.ReadAllText("CLIService/SQL/bbe-schema.sql");
                    command.Parameters.Clear();
                    command.CommandText = schema;
                    await command.ExecuteNonQueryAsync();
                }
                catch (MySqlException exCreateDB)
                {
                    Console.WriteLine($"Failed creating database: {exCreateDB.Message}");
                    Environment.Exit(1);
                }
            }
            else
            {
                Console.WriteLine(exUseDB.Message);
                Environment.Exit(1);
            }
        }

        // database exists and is used, now populate it if empty
        try
        {
            // there is always 1 admin account created by default, so try to get one record
            command.Parameters.Clear();
            command.CommandText = "SELECT * FROM Account LIMIT 1";
            using var reader = await command.ExecuteReaderAsync();
            // if no records, means it's a new, empty database
            if (!reader.HasRows)
            {
                // reader must be closed before running another command
                reader.Close();

                try
                {
                    // populate the tables in all at once with a single command
                    // this didn't work in Python due to connector implementation (I believe)
                    // but it works in C# with MySqlConnector
                    // load the data file and run the sql to create the table records of initial data
                    string data = File.ReadAllText("CLIService/SQL/bbe-data.sql");
                    command.Parameters.Clear();
                    command.CommandText = data;
                    await command.ExecuteNonQueryAsync();
                }
                catch (MySqlException exSqlCommand)
                {
                    Console.WriteLine(exSqlCommand.Message);
                    Environment.Exit(1);
                }
            }
        }
        catch (MySqlException exSelectDB)
        {
            Console.WriteLine($"An error occurred: {exSelectDB.Message}");
        }

        // database opened, selected and populated
        command.Dispose();
        isReady = true;
        Console.WriteLine("Database is ready");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

A connection should be disposed of as soon as possible which means that the code in this post should be re-evaluated (ie: re-written). The following may be of interest: stackoverflow.com/a/74576869/10024425, stackoverflow.com/a/68510196/10024425, and stackoverflow.com/a/71540398/10024425

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.