import mysql.connector
import config
import threading

enabled = False

db_lock = threading.Lock()
conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
cur = conn.cursor()

def log(message, kind):
    if enabled:
        with db_lock:
            cur.execute('INSERT INTO vkbot_logmessage VALUES (NULL, %s, %s, NOW())', (message, kind))
            conn.commit()

import mysql.connector
import config
import threading

enabled = False
connected = False

db_lock = threading.Lock()


def log(message, kind):
    if enabled:
        with db_lock:
            global conn, cur, connected
            if not connected:
                conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
                cur = conn.cursor()
                connected = True
            cur.execute('INSERT INTO vkbot_logmessage VALUES (NULL, %s, %s, NOW())', (message, kind))
            conn.commit()

Connect to MySQL only when needed
Connect to MySQL only when needed

  import mysql.connector
  import config
  import threading
  
  enabled = False
+ connected = False
  
  db_lock = threading.Lock()
+ 
- conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
- cur = conn.cursor()
  
  def log(message, kind):
      if enabled:
          with db_lock:
+             global conn, cur, connected
+             if not connected:
+                 conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
+                 cur = conn.cursor()
+                 connected = True
              cur.execute('INSERT INTO vkbot_logmessage VALUES (NULL, %s, %s, NOW())', (message, kind))
              conn.commit()
  
Connect to MySQL only when needed
## Code Before:
import mysql.connector
import config
import threading

enabled = False

db_lock = threading.Lock()
conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
cur = conn.cursor()

def log(message, kind):
    if enabled:
        with db_lock:
            cur.execute('INSERT INTO vkbot_logmessage VALUES (NULL, %s, %s, NOW())', (message, kind))
            conn.commit()

## Instruction:
Connect to MySQL only when needed
## Code After:
import mysql.connector
import config
import threading

enabled = False
connected = False

db_lock = threading.Lock()


def log(message, kind):
    if enabled:
        with db_lock:
            global conn, cur, connected
            if not connected:
                conn = mysql.connector.connect(host=config.get('db_logger.host'), user=config.get('db_logger.username'), password=config.get('db_logger.password'), database=config.get('db_logger.database'))
                cur = conn.cursor()
                connected = True
            cur.execute('INSERT INTO vkbot_logmessage VALUES (NULL, %s, %s, NOW())', (message, kind))
            conn.commit()
