Giter Club home page Giter Club logo

pdoi's Introduction

Pdoi - The Improved PDO

This library provides class whose API matches PDO API, and can be used as PDO replacement with little application changes. This class is called Pdoi. It supports most (but not all) PDO features. For drivers other than MySQL, it works exactly like PDO (function calls are forwarded to internal PDO instance), and for MySQL it explicitly uses mysqli extension.

This is to achieve 2 goals:

  1. Saner connections pooling.
  2. Access to mysqli-specific features.

About database connections pooling

As of January 2021, my eyes don't see usable database connections pooling solution in PHP world.

Persistent connections can be created with mysqli using "p:" prefix for hostname. And with PDO they can be created with help of PDO::ATTR_PERSISTENT attribute. Together with PHP-FPM process pool this can build up the true database connections pooling system.

This worked even on PHP4, but nobody practically used them, because there are problems reusing a dirty connection.

  1. SET @flag=1 issued once, will be initially set on next connection usage.
  2. Started and not committed transactions will hold locks on touched rows.
  3. SELECT Get_lock('Busy', 10) will prevent other parallel connections in pool from entering critical section.

Current persistent connections implementation in mysqli and PDO, as of January 2021, resets connection state by calling mysqli_change_user() function. But my experience reveals that user-locks are not released. I suppose that this function is called when a connection is picked up from pool, before using it. So whole time period since the connection was left alone, and till it's new usage (that can not happen at all), locks are locking what they want to lock.

Each connection should be reset immediately at script end. This library calls mysqli_change_user() in destructor of Pdoi object. What i love in PHP is reference-counting objects, and that destructors are called immediately when the object is not in use.

Installation

Create a directory for your application, cd to it, and issue:

composer require jeremiah-shaulov/pdoi

Example

<?php

require_once 'vendor/autoload.php';
use Pdoi\Pdoi as Pdo;

$db = new Pdo
(	'mysql:host=localhost',
	'root',
	'',
	[	PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
		PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
		PDO::MYSQL_ATTR_MULTI_STATEMENTS => false,
		PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false,
		PDO::ATTR_PERSISTENT => true,
	]
);

var_dump($db->query("SELECT Connection_id()")->fetchColumn());

Running this example from CLI will show different Connection_id() each time, because there will be only 1 connection in process lifecycle. But running from PHP-FPM will show that the same connection numbers appear many times.

pdoi's People

Contributors

jeremiah-shaulov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.