From ca4862e20c031ca0b2007ec8310a888856a03dcd Mon Sep 17 00:00:00 2001 From: Seth Chisamore Date: Mon, 25 Apr 2011 16:15:53 -0400 Subject: [PATCH] [COOK-521] fix mysql lost connection error when chaining actions --- mysql/libraries/database.rb | 6 ++--- mysql/metadata.json | 2 +- mysql/metadata.rb | 2 +- mysql/providers/database.rb | 51 ++++++++++++++++++++++--------------- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/mysql/libraries/database.rb b/mysql/libraries/database.rb index 4ddb306fc..3403e254a 100644 --- a/mysql/libraries/database.rb +++ b/mysql/libraries/database.rb @@ -8,11 +8,11 @@ module Opscode module Mysql module Database def db - @@db ||= ::Mysql.new new_resource.host, new_resource.username, new_resource.password + @db ||= ::Mysql.new new_resource.host, new_resource.username, new_resource.password end def close - @@db.close rescue nil - @@db = nil + @db.close rescue nil + @db = nil end end end diff --git a/mysql/metadata.json b/mysql/metadata.json index 0666bd62d..fc4d724ff 100644 --- a/mysql/metadata.json +++ b/mysql/metadata.json @@ -222,5 +222,5 @@ "mysql::server": "Installs packages required for mysql servers w/o manual intervention", "mysql::server_ec2": "Performs EC2-specific mountpoint manipulation" }, - "version": "1.0.1" + "version": "1.0.2" } \ No newline at end of file diff --git a/mysql/metadata.rb b/mysql/metadata.rb index 6423c7935..c9bbb1b05 100644 --- a/mysql/metadata.rb +++ b/mysql/metadata.rb @@ -3,7 +3,7 @@ license "Apache 2.0" description "Installs and configures mysql for client or server" long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version "1.0.1" +version "1.0.2" recipe "mysql", "Includes the client recipe to configure a client" recipe "mysql::client", "Installs packages required for mysql clients using run_action magic" recipe "mysql::server", "Installs packages required for mysql servers w/o manual intervention" diff --git a/mysql/providers/database.rb b/mysql/providers/database.rb index ee73998fb..36f5ca0a8 100644 --- a/mysql/providers/database.rb +++ b/mysql/providers/database.rb @@ -20,27 +20,31 @@ include Opscode::Mysql::Database action :flush_tables_with_read_lock do - begin - Chef::Log.info "mysql_database: flushing tables with read lock" - db.query "flush tables with read lock" - new_resource.updated_by_last_action(true) - ensure - db.close + if exists? + begin + Chef::Log.info "mysql_database: flushing tables with read lock" + db.query "flush tables with read lock" + new_resource.updated_by_last_action(true) + ensure + db.close + end end end action :unflush_tables do - begin - Chef::Log.info "mysql_database: unlocking tables" - db.query "unlock tables" - new_resource.updated_by_last_action(true) - ensure - db.close + if exists? + begin + Chef::Log.info "mysql_database: unlocking tables" + db.query "unlock tables" + new_resource.updated_by_last_action(true) + ensure + db.close + end end end action :create_db do - unless @mysqldb.exists + unless exists? begin Chef::Log.info "mysql_database: Creating database #{new_resource.database}" db.query("create database #{new_resource.database}") @@ -52,12 +56,14 @@ end action :query do - begin - Chef::Log.info "mysql_database: Performing Query: #{new_resource.sql}" - db.query(new_resource.sql) - new_resource.updated_by_last_action(true) - ensure - db.close + if exists? + begin + Chef::Log.info "mysql_database: Performing Query: #{new_resource.sql}" + db.query(new_resource.sql) + new_resource.updated_by_last_action(true) + ensure + db.close + end end end @@ -67,6 +73,9 @@ def load_current_resource @mysqldb = Chef::Resource::MysqlDatabase.new(new_resource.name) @mysqldb.database(new_resource.database) - exists = db.list_dbs.include?(new_resource.database) - @mysqldb.exists(exists) +end + +private +def exists? + db.list_dbs.include?(new_resource.database) end