1 /*
2    Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "ndb_thd_ndb.h"
26 
27 /*
28   Default value for max number of transactions createable against NDB from
29   the handler. Should really be 2 but there is a transaction to much allocated
30   when lock table is used, and one extra to used for global schema lock.
31 */
32 static const int MAX_TRANSACTIONS= 4;
33 
34 
35 Thd_ndb*
seize(THD * thd)36 Thd_ndb::seize(THD* thd)
37 {
38   DBUG_ENTER("seize_thd_ndb");
39 
40   Thd_ndb* thd_ndb= new Thd_ndb(thd);
41   if (thd_ndb == NULL)
42     return NULL;
43 
44   if (thd_ndb->ndb->init(MAX_TRANSACTIONS) != 0)
45   {
46     DBUG_PRINT("error", ("Ndb::init failed, eror: %d  message: %s",
47                          thd_ndb->ndb->getNdbError().code,
48                          thd_ndb->ndb->getNdbError().message));
49 
50     delete thd_ndb;
51     thd_ndb= NULL;
52   }
53   DBUG_RETURN(thd_ndb);
54 }
55 
56 
57 void
release(Thd_ndb * thd_ndb)58 Thd_ndb::release(Thd_ndb* thd_ndb)
59 {
60   DBUG_ENTER("release_thd_ndb");
61   delete thd_ndb;
62   DBUG_VOID_RETURN;
63 }
64 
65 
66 bool
recycle_ndb(THD * thd)67 Thd_ndb::recycle_ndb(THD* thd)
68 {
69   DBUG_ENTER("recycle_ndb");
70   DBUG_PRINT("enter", ("ndb: 0x%lx", (long)ndb));
71 
72   DBUG_ASSERT(global_schema_lock_trans == NULL);
73   DBUG_ASSERT(trans == NULL);
74 
75   delete ndb;
76   if ((ndb= new Ndb(connection, "")) == NULL)
77   {
78     DBUG_PRINT("error",("failed to allocate Ndb object"));
79     DBUG_RETURN(false);
80   }
81 
82   if (ndb->init(MAX_TRANSACTIONS) != 0)
83   {
84     delete ndb;
85     ndb= NULL;
86     DBUG_PRINT("error", ("Ndb::init failed, %d  message: %s",
87                          ndb->getNdbError().code,
88                          ndb->getNdbError().message));
89     DBUG_RETURN(false);
90   }
91   DBUG_RETURN(true);
92 }
93 
94 
95 bool
valid_ndb(void)96 Thd_ndb::valid_ndb(void)
97 {
98   // The ndb object should be valid as long as a
99   // global schema lock transaction is ongoing
100   if (global_schema_lock_trans)
101     return true;
102 
103   // The ndb object should be valid as long as a
104   // transaction is ongoing
105   if (trans)
106     return true;
107 
108   if (unlikely(m_connect_count != connection->get_connect_count()))
109     return false;
110 
111   return true;
112 }
113 
114 
115 void
init_open_tables()116 Thd_ndb::init_open_tables()
117 {
118   count= 0;
119   m_error= FALSE;
120   my_hash_reset(&open_tables);
121 }
122