First commit
[anna.git] / include / anna / dbms / Delivery.hpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_dbms_Delivery_hpp
38 #define anna_dbms_Delivery_hpp
39
40 #include <anna/comm/Delivery.hpp>
41
42 namespace anna {
43
44 namespace dbms {
45
46 class Connection;
47
48 /**
49    Agrupacion logica de conexiones con la base de datos. Reparte la carga de las transacciones contra
50    la base de datos entre las distintas conexiones que contenga esta instancia. Ademas en caso de estar
51    en una ejecucion con soporte para multithread (ver anna::functions::supportMultithread) asegura
52    que cada uno de los threads siempre utiliza la misma conexion lo cual asegura el mantinimiento de la
53    integridad de cada una de las transacciones de los threads.
54 */
55 class Delivery : comm::Delivery {
56 public:
57   /**
58      Constructor.
59      @param name Nombre logico de este grupo de conexiones.
60   */
61   Delivery(const char *name) : comm::Delivery(name) {;}
62
63   /**
64      Crea automaticamente las conexiones a la base de datos recibida como parametro con el usuario/password
65      indicado.
66      \param database Instancia de la base de datos contra la que realizamos la conexion.
67      \param prefixName Prefijo del nombre logico de la conexiones que vamos a crear. El resto del nombre vendra
68      dado por el numero secuencial de la conexion.
69      \param user Nombre del usuario con el que realizamos la conexion.
70      \param password Codigo de acceso del usuario.
71      \param n Numero de conexion a crear.
72      \warning Recordar que el numero maximo de conexiones a una base de datos esta limitado por Database::maxConnection.
73   */
74   void createConnections(Database& database, const char* prefixName, const char* user, const char* password, const int n)
75   throw(RuntimeException, DatabaseException);
76
77   /**
78      Incorpora al conexion recibida como parametro a la agrupacion logica.
79      \param connection Conexion que vamos a incorporar a la agrupacion logica.
80   */
81   void addConnection(Connection* connection) throw(RuntimeException) {
82     this->add(connection);
83     a_iiConnection = this->begin();
84   }
85
86   /**
87      Devuelve la instancia de la conexion a base de datos con la que debemos trabajar.
88      @return la instancia de la conexion a base de datos con la que debemos trabajar.
89      \warning La conexion debe ser bloqueada por el Thread que la recibe (ver anna::Guard) para asegurar que
90      cualquier otro thread que intente acceder a ella queda bloqueado a la espera de que terminemos de
91      trabajar sobre ella.
92   */
93   Connection& getConnection() throw(RuntimeException);
94
95 private:
96   iterator a_iiConnection;
97
98   void do_initialize() throw() { a_iiConnection = begin(); }
99   comm::Resource* do_apply() throw(RuntimeException);
100   static Connection* connection(iterator& ii) { return (Connection*) comm::Delivery::resource(ii); }
101
102 };
103
104 }
105 }
106
107 #endif