From 8a3c137b5e34ff52cdbd890ee3b9201360667c93 Mon Sep 17 00:00:00 2001
From: unknown <tulin@mysql.com>
Date: Mon, 21 Feb 2005 23:29:07 +0100
Subject: [PATCH] corrected errors in how ndbcluster handler closes down in
 case of error

---
 sql/ha_ndbcluster.cc | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc
index 62bc1cc41b9..12e2e3672cc 100644
--- a/sql/ha_ndbcluster.cc
+++ b/sql/ha_ndbcluster.cc
@@ -4616,7 +4616,11 @@ ndbcluster_init()
     (opt_ndb_optimized_node_selection);
 
   // Create a Ndb object to open the connection  to NDB
-  g_ndb= new Ndb(g_ndb_cluster_connection, "sys");
+  if ( (g_ndb= new Ndb(g_ndb_cluster_connection, "sys")) == 0 )
+  {
+    DBUG_PRINT("error", ("failed to create global ndb object"));
+    goto ndbcluster_init_error;
+  }
   g_ndb->getDictionary()->set_local_table_data_size(sizeof(Ndb_table_local_info));
   if (g_ndb->init() != 0)
   {
@@ -4669,6 +4673,10 @@ ndbcluster_init()
   if (pthread_create(&tmp, &connection_attrib, ndb_util_thread_func, 0))
   {
     DBUG_PRINT("error", ("Could not create ndb utility thread"));
+    hash_free(&ndbcluster_open_tables);
+    pthread_mutex_destroy(&ndbcluster_mutex);
+    pthread_mutex_destroy(&LOCK_ndb_util_thread);
+    pthread_cond_destroy(&COND_ndb_util_thread);
     goto ndbcluster_init_error;
   }
   
@@ -4676,7 +4684,12 @@ ndbcluster_init()
   DBUG_RETURN(&ndbcluster_hton);
 
  ndbcluster_init_error:
-  ndbcluster_end();
+  if(g_ndb)
+    delete g_ndb;
+  g_ndb= NULL;
+  if (g_ndb_cluster_connection)
+    delete g_ndb_cluster_connection;
+  g_ndb_cluster_connection= NULL;
   DBUG_RETURN(NULL);
 }
 
@@ -4691,6 +4704,9 @@ bool ndbcluster_end()
 {
   DBUG_ENTER("ndbcluster_end");
 
+  if (!ndbcluster_inited)
+    DBUG_RETURN(0);
+
   // Kill ndb utility thread
   (void) pthread_mutex_lock(&LOCK_ndb_util_thread);  
   DBUG_PRINT("exit",("killing ndb util thread: %lx", ndb_util_thread));
@@ -4703,8 +4719,7 @@ bool ndbcluster_end()
   if (g_ndb_cluster_connection)
     delete g_ndb_cluster_connection;
   g_ndb_cluster_connection= NULL;
-  if (!ndbcluster_inited)
-    DBUG_RETURN(0);
+
   hash_free(&ndbcluster_open_tables);
   pthread_mutex_destroy(&ndbcluster_mutex);
   pthread_mutex_destroy(&LOCK_ndb_util_thread);
-- 
2.30.9