Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7647235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:24:51+00:00 2026-05-31T10:24:51+00:00

I need to share a database handle among a few Ruby scripts. I am

  • 0

I need to share a database handle among a few Ruby scripts. I am using the DBI gem to connect to the database. Consider the following example

#a.rb
class A
  require 'dbi'

  def connect
    if a database handle is already open return it
    else create a new one and return it
  end
end

#b.rb
class B
  require 'a'
  a = A.new
  dbh = a.connect
  #some database queries here
end

#c.rb
class C
  require 'a'
  a = A.new
  dbh = a.connect #Here the database handle created by class B should be returned 
end

I understand that class instance variables are the way to go to achieve the said goal. Can someone please provide some insight on this?

Does DBI have something similar to Log4r

class A
  require 'log4r'
  Log4r::Logger.new('SO') #create a new instance here
end

class B
  require 'a'
  Log4r::Logger['SO'] # get the existing instance here
end

Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:24:52+00:00Added an answer on May 31, 2026 at 10:24 am

    The following uses a Singleton to manage a hash of DBI handles. It will create a new handle for a given set of connect options (the args one would pass to DBI.connect) the first time a connect is requested with those options. Subsequent requests with the same options will get the already open handle.

    # there_can_be_only_one_dbh.rb
    
    require 'dbi'
    require 'singleton'
    
    class ThereCanBeOnlyOneDBH
      include Singleton
    
      DEFAULT_CONNECT_OPTIONS = ['dsn','user','pw']
    
      def connect( *options )
        options = DEFAULT_CONNECT_OPTIONS unless options and !options.empty?
        @handles ||= {}
        @handles[options.join('|')] ||= DBI.connect( *options )
      end
    end
    
    # b.rb
    require 'there_can_be_only_one_dbh'
    
    class B
      def initialize( *dbi_connect_options )
        @dbh = ThereCanBeOnlyOneDBH.instance.connect( *dbi_connect_options )
      end
    
      # methods which issue queries using @dbh
    end
    
    # c.rb
    require 'there_can_be_only_one_dbh'
    
    class C
      def initialize( *dbi_connect_options )
        @dbh = ThereCanBeOnlyOneDBH.instance.connect( *dbi_connect_options )
      end
    
      # other methods which issue queries using @dbh
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create several applications that all share a Microsoft SQL Server database.
Our application has a need to share an existing database table with another locally
I need to share SSO information between two different domains with a cookie, can
I need to share a stack of strings between processes (possibly more complex objects
I have 2 Java EE applications which need to share some common user related
I have Client-Server environment and developed a project for Client-Server. I need to share
I need a script to run on Vista Ultimate to share an external drive
I need to perform some operations on all folders within a file share which
I need to find an open source alternative to VSS where people will share
I'm new in using boost and have a problem. I need shared_mutex function in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.