connectionmanager.hxx

来自「vovida的软交换」· HXX 代码 · 共 196 行

HXX
196
字号
/* ==================================================================== * The Vovida Software License, Version 1.0  *  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. *  * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. *  * 3. The names "VOCAL", "Vovida Open Communication Application Library", *    and "Vovida Open Communication Application Library (VOCAL)" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor *    may "VOCAL" appear in their name, without prior written *    permission of Vovida Networks, Inc. *  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. *  * ==================================================================== *  * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc.  For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */#ifndef CONNECTIONMANAGER_HXX#define CONNECTIONMANAGER_HXX#include "DBConnection.hxx"#include "Mutex.hxx"#include "Lock.hxx"#include <vector>/** *  This class gives the Cache access to the database. * *  ConnectionManager maintains a 'pool' of database  *  connection info (DBConInfo) objects each of which *  contain a pointer to the DBConnection.  *  *  A client can obtain a DBConnection* by calling the  *  public accessor function "searchDBConPool()", and *  'free' this connection when its duty is finished.     *  *  ConnectionManager.hxx   version 1.0 -- 14 Mar 2003 *                          Suha Cubukcuoglu * *//** Forward Declare DBConnection class */class DBConnection;/** Maximum number of Database Connections * that are allowed in the pool.  */#define MAX_CONNECTIONS 5class ConnectionManager{    private:            /** Pointer to the instance of this class */        static ConnectionManager *myConManager;            /** Constructor */        ConnectionManager(void):        // initialize an empty pool of DBConInfo objects        DBConPool()           {   // initialize Pointer to DBConnection                                                                                             myDBConnectionP = 0;        };	    /** Pointer to DBConnection */        DBConnection* myDBConnectionP;                /** DBConnection objects         *  @param myDBCon Pointer to DBConnection obj          *  @param used    Status of the connection          *  @param myTime  Timestamp for creation         */          typedef struct dbconninfo        {            DBConnection * myDBCon;             bool used;            time_t myTime;                } DBConInfo;                /** Total number of connections to the database */        static int NumDBCons;                /** DBConPool vector has elements of type DBConInfo */           vector <DBConInfo> DBConPool;                /** Number of unused DBConnections */        static int freeDBCon;        	    /** Used to lock access to the DBConPool */        static Vocal::Threads::Mutex myMutex;            public:                /** Returns a pointer to the existing instance         *  of the ConnectionManager object, or creates         *  one if none exist.         */        static ConnectionManager* getInstance(void);                /** Destructor */        virtual ~ConnectionManager(void)        {   // destroy pointer to DBConnection            delete myDBConnectionP;        };            /** handle instructions from DBInfo */        DBConnection* GetDBConnection(const DBInfo dbInfo);        /**         *  Search for existing DB connections in DBConPool.         *  If the pool is empty, get a new connection, mark         *  it 'used' and return it. Otherwise, find a conn         *  which is unused, if unsuccessful, create a new           *  conn, mark it 'used' and pass it back to the         *  calling environment.         *  @param dbInfo used to set up DB connection         *  @return DBConnection to be passed back         */                  DBConnection* searchDBConPool(const DBInfo dbInfo);          /**          * Checks DBConnections in DBConPool.          * Removes those DBConInfo objects which have broken         * DBConnections's.         */        void checkDBConns();                 /**          * Process a DBConnection free'd by a client.         * @param dbconn connection to return         * @return false if the connection does not exist.         */        bool returnDBCon(DBConnection * dbconn);                 /**          * Size of DBConPool.         * @return size of pool of DBConInfo objects          */         int dbPoolSize(){ return NumDBCons; }                /**         * Increment number of unused DBConnections.         */        void incrementFreeDBCon() { freeDBCon++; }        /**          * Decrement number of unused DBConnections          */        void decrementFreeDBCon() { freeDBCon--; }        /**         * Return number of unused DBConnections         * @return free DB connctions          */        int unusedDBCon(){ return freeDBCon; }   }; #endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?