Mitchell McKenna - tagged with mysql http://mitchmckenna.com/feed en-us http://blogs.law.harvard.edu/tech/rss LifePress mitchellmckenna@gmail.com Laravel Queued Jobs + Doctrine - Fix Mysql Has Gone Away Errors http://mitchmckenna.com/post/15630/laravel-queued-jobs-doctrine-fix-mysql-has-gone-away-errors

With long running processes you can get a 2006 MySQL server has gone away error. This adds a class that calls Doctrine's DBAL Connection's $conn->ping() to see if it's gone away, if so close the connection and let DBAL automatically open a new connection on the next query attempt. This is talked about in the Doctrine's Github repo here. Here's some pseudo code on how to do it:

<?php

namespace Acme\Doctrine\ORM;

use Doctrine\ORM\EntityManager;

class ConnectionSaver
{
    /**
     * Ping the DB server, if connection is lost close it and DBAL will
     * automatically reconnect on the next DB call.
     */
    public function checkDatabaseConnection()
    {
        /** @var \Doctrine\DBAL\Connection */
        $conn = app(EntityManager::class)->getConnection();

        try {
            $ping = $conn->ping();
        } catch (\ErrorException $e) {
            $conn->close();
        }
    }
}

Then inside the command handler you just inject the class and call checkDatabaseConnection() function.

<?php

namespace App\Listeners\Commands;

use Acme\Doctrine\ORM\ConnectionSaver;

class FooHandler
{
    public function __construct(ConnectionSaver $connectionSaver) {
        $connectionSaver->checkDatabaseConnection();
    }
]]>
Wed, 20 Jan 2016 03:34:54 -0500 http://mitchmckenna.com/post/15630/laravel-queued-jobs-doctrine-fix-mysql-has-gone-away-errors/laravel-queued-jobs-doctrine-fix-mysql-has-gone-away-errors
Database Backup Manager for PHP http://mitchmckenna.com/post/15702/database-backup-manager-for-php

Database backup manager for dumping to and restoring databases from S3, Dropbox, FTP, SFTP, and Rackspace Cloud. Supports MySQL and PostgreSQL. A laravel specific package is also available for easy integration.

]]>
Fri, 04 Sep 2015 18:24:00 -0400 http://mitchmckenna.com/post/15702/database-backup-manager-for-php/database-backup-manager-for-php
Sequelize - Mysql ORM for NodeJS http://mitchmckenna.com/post/13811/sequelize-mysql-orm-for-nodejs

The Sequelize library provides easy access to MySQL, MariaDB, SQLite or PostgreSQL databases by mapping database entries to objects and vice versa. To put it in a nutshell, it's an ORM (Object-Relational-Mapper). The library is written entirely in JavaScript and can be used in the Node.JS environment.

]]>
Mon, 16 Dec 2013 17:49:00 -0500 http://mitchmckenna.com/post/13811/sequelize-mysql-orm-for-nodejs/sequelize-mysql-orm-for-nodejs
A Better Way to Use PHP's MySQLi http://mitchmckenna.com/post/7994/a-better-way-to-use-phps-mysqli

"PHP’s prepared statements (for database access) are fantastic. Not only do they help secure your database queries, but they’re also particularly more efficient for larger products. However, there are a couple issues that appear to make these methods less flexible than we’d hope. For one, we must utilize the bind_result method, and pass in a specific number of variables. However, what happens when this code is within a class, and we won’t immediately know how many variables to pass? Luckily, there’s a solution!"

]]>
Sun, 08 Aug 2010 23:48:00 -0400 http://mitchmckenna.com/post/7994/a-better-way-to-use-phps-mysqli/a-better-way-to-use-phps-mysqli
Managing Hierarchical Data in MySQL http://mitchmckenna.com/post/6357/managing-hierarchical-data-in-mysql

"Most users at one time or another have dealt with hierarchical data in a SQL database and no doubt learned that the management of hierarchical data is not what a relational database is intended for. The tables of a relational database are not hierarchical (like XML), but are simply a flat list. Hierarchical data has a parent-child relationship that is not naturally represented in a relational database table. In this article we will examine two models for dealing with hierarchical data in MySQL"

]]>
Wed, 17 Feb 2010 14:54:00 -0500 http://mitchmckenna.com/post/6357/managing-hierarchical-data-in-mysql/managing-hierarchical-data-in-mysql