public static function get_lock_factory($type) {
        global $CFG, $DB;
        $lockfactory = null;

        if (during_initial_install()) {
            $lockfactory = new \core\lock\installation_lock_factory($type);
        } else if (isset($CFG->lock_factory) && $CFG->lock_factory != 'auto') {
            if (!class_exists($CFG->lock_factory)) {
                // In this case I guess it is not safe to continue. Different cluster nodes could end up using different locking
                // types because of an installation error.
                throw new \coding_exception('Lock factory set in $CFG does not exist: ' . $CFG->lock_factory);
            }
            $lockfactoryclass = $CFG->lock_factory;
            $lockfactory = new $lockfactoryclass($type);
        } else {
            $dbtype = clean_param($DB->get_dbfamily(), PARAM_ALPHA);

            // DB Specific lock factory is preferred - should support auto-release.
            $lockfactoryclass = "\\core\\lock\\${dbtype}_lock_factory";
            if (!class_exists($lockfactoryclass)) {
                $lockfactoryclass = '\core\lock\file_lock_factory';
            }
            /* @var lock_factory $lockfactory */
            $lockfactory = new $lockfactoryclass($type);
            if (!$lockfactory->is_available()) {
                // Final fallback - DB row locking.
                $lockfactory = new \core\lock\db_record_lock_factory($type);
            }
        }

        return $lockfactory;
    }