GithubHelp home page GithubHelp logo

hoangtrucit / connection-pool Goto Github PK

View Code? Open in Web Editor NEW

This project forked from active911/connection-pool

0.0 1.0 0.0 39 KB

Generic, efficient, thread safe connection pooling for C++

License: Apache License 2.0

Objective-C 71.17% Perl 7.35% C++ 21.48%

connection-pool's Introduction

connection-pool

Generic, efficient, thread safe connection pooling for C++

Introduction

We needed a safe, efficient way to concurrently access MySQL without connection banging.

Features

  • Fast
  • Thread safe
  • Generic (MySQL implementation using Connection/C++ is provided)
  • Pre-opens arbitrary number of connections
  • Used connections can be returned for immediate reuse
  • Unreturned connections are automatically closed and replaced (thanks to shared_ptr reference counting)
  • Apache 2.0 license

Example

#include <string>
#include <boost/shared_ptr.hpp>
#include "MySQLConnection.h"

// Create a pool of 5 MySQL connections
shared_ptr<MySQLConnectionFactory>mysql_connection_factory(new MySQLConnectionFactory("mysql_server","mysql_username","mysql_password"));
shared_ptr<ConnectionPool<MySQLConnection> >mysql_pool(new ConnectionPool<MySQLConnection>(5, mysql_connection_factory));


// Get a connection and do something with it (throws exception if pool is completely used)
shared_ptr<MySQLConnection> conn=mysql_pool->borrow();	
// conn->sql_connection->do_whatever();

/* If our code dies here, the connection will be closed and replaced with a new one! :) */

// Release for immediate reuse
mysql_pool->unborrow(conn);

Design notes

Connections are stored as a std::deque of boost::shared_ptr so we can pop from the front and push from back; this makes sure all connections get cycled through as fast as possible (so we don't accidentally hang onto any dead ones for a long time).

We managed to get all of this WITHOUT a separate curator thread. Calling borrow() should only block very briefly while we access the pool deque, etc. If we have to replace a dead connection with a new one, borrow() will additionally block while the new connection is set up. If we are still unable to serve a live connection, borrow() will throw.

The use of boost::shared_ptr for tracking connections yeilds some nice side effects. If you have a problem with a connection, just let the shared_ptr fall out of scope and the connection will automagically be closed (immediately) and then replaced with a brand new connection (later, when we need one). In this way, it promotes 'safe' handling of connections without the need to manually ping() (or whatever) each connection periodically.

Debugging may be enabled by using the #define _DEBUG(x) macro:

// Log to stdout
#define _DEBUG(x) 								\
	do { 										\
		std::cout << "  (" << x << ")" << endl;	\
	} while(0)

Dependencies

  • Boost
  • Connection/C++ (for MySQL implementation)

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.