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 6036559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:56:33+00:00 2026-05-23T05:56:33+00:00

I’m creating a class that will manage a connection to a Firebird database. The

  • 0

I’m creating a class that will manage a connection to a Firebird database. The Firebird Service will be installed to facilitate multiple connections to the database. Unfortunately, the environments where my software will deploy can be volatile, and I can’t always guarantee that the Firebird Service will be running when I attempt to connect, or that it will continue to run after I’ve established a connection.

In the interest of centralizing error handling, I’ve made the decision that disparate parts of my code won’t directly work with database cursors in any way. Instead, I’ll expose query() and dml() methods from my Connection Manager. This works, to an extend, given the code below (some code not included for brevity).

class DBConnection(object):
  # self._conn is an instance of kinterbasdb.connect()
  def query(self, query, params = None):
    cursor = self._conn.cursor()

    if params:
      cursor.execute(query, params)
    else:
      cursor.execute(query)

    return [[x[0].title() for x in cursor.description]] + [r for r in cursor.fetchall()]

  def dml(self, query, params = None):
    cursor = self._conn.cursor()

    if params:
      cursor.execute(query, params)
    else:
      cursor.execute(query)

    self._conn.commit()

The trouble manifests when the Firebird Service stops or is unreachable for some reason. I would expect that self._conn.cursor() would throw an exception, which would make it simple to do something like this:

class DBConnection(object):
  # self._conn is an instance of kinterbasdb.connect()
  def cursor(self):
    try:
      return self._conn.cursor()
    except:
      # Error handling code here, possibly reconnect, display alert.

  def query(self, query, params = None):
    cursor = self.cursor()

  def dml(self, query, params = None):
    cursor = self.cursor()

Unfortunately, there is no exception thrown when I request a cursor. I don’t become aware of the trouble until the call to cursor.execute(). That means that, if I want to properly centralize my error handling, I have to do something like this:

class DBConnection(object):
  # self._conn is an instance of kinterbasdb.connect()
  def cursor(self):
    try:
      cursor = self._conn.cursor()

      cursor.execute("Select NULL From <sometable>")

      return cursor
    except:
      # Error handling code here, possibly reconnect, display alert.

This requires an extra round-trip to my database, wastes a transaction (Firebird databases have a hard upper limit on total transactions for the life of the database), and generally just feels wrong. I’m wondering, has anyone encountered anything similar with other implementations of the Python Database API, and if so, how were they overcome?

  • 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-23T05:56:33+00:00Added an answer on May 23, 2026 at 5:56 am

    I am testing the following modifications to my class which I believe will achieve the centralized handling that I want with minimal code duplication. They also simplify the query and dml methods slightly, and it eliminates the extra queries (heartbeat) that I wanted to avoid.

    class DBConnection(object):
      # self._conn is an instance of kinterbasdb.connect()
      def query(self, query, params = None):
        cursor = self._conn.cursor()
    
        self.execute(cursor, query, params)
    
        return [[x[0].title() for x in cursor.description]] + 
                [r for r in cursor.fetchall()]
    
      def dml(self, query, params = None):
        cursor = self._conn.cursor()
    
        self.execute(cursor, query, params)
    
        self._conn.commit()
    
      def execute(self, cursor, query, params = None):
        try:
          if params:
            cursor.execute(query, params)
          else:
            cursor.execute(query)
        except Exception, e:
          # Handling
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.