Class: Record::BASE
- Inherits:
-
Object
- Object
- Record::BASE
- Defined in:
- lib/record/record.rb
Instance Attribute Summary collapse
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(dbconn) ⇒ BASE
constructor
A new instance of BASE.
Constructor Details
#initialize(dbconn) ⇒ BASE
Returns a new instance of BASE.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/record/record.rb', line 7 def initialize(dbconn) begin $db_conn = Mysql.real_connect(dbconn[:host], dbconn[:username], dbconn[:password], dbconn[:database], dbconn[:port]) #$logger.debug("Primary database initialized => #{dbconn[:host]}") # Check to make sure there is a read db in the configs if dbconn[:read_host].nil? $db_conn_read = $db_conn else $db_conn_read = Mysql.real_connect(dbconn[:read_host], dbconn[:read_username], dbconn[:read_password], dbconn[:read_database], dbconn[:read_port]) end #$logger.debug("Secondary database initialized => #{dbconn[:read_host]}") rescue # No db information include. No problem. We just won't have a database connection #$logger.warn("No database connection initialized") end end |
Instance Attribute Details
#status ⇒ Object (readonly)
Returns the value of attribute status.
5 6 7 |
# File 'lib/record/record.rb', line 5 def status @status end |
Class Method Details
.query(query_string) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/record/record.rb', line 24 def self.query(query_string) n = 0 # track how many times that the system had to reconnect to the db begin # Test to see if the query starts with a select which would mean it was a read query if query_string.split[0].upcase == "SELECT" #$logger.debug("Using read database => \"#{query_string}\"") res = $db_conn_read.query(query_string) else #$logger.debug("Using update database => \"#{query_string}\"") res = $db_conn.query(query_string) end rescue Mysql::Error => e $logger.error("Mysql query => #{query_string}") $logger.error("Mysql::Error '#{e.to_s}'") case e.to_s when 'MySQL server has gone away' $logger.warn("Connection to database #{dbconn[:host]} has gone away. Reconnecting.") if n == 0 self.new n += 1 retry when 'Lost connection to MySQL server during query' $logger.warn("Lost connection #{dbconn[:host]}. Reconnecting.") if n == 0 self.new n += 1 retry else # Don't know what to do because of an unknown error so to play it safe we'll just break instead looping $logger.warn("ERROR: #{e.to_s} Not sure what this error is from #{dbconn[:host]}.") end end return res end |