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

  • Home
  • SEARCH
  • 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 722967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:03:08+00:00 2026-05-14T06:03:08+00:00

I have built a pretty simple REST service in Sinatra, on Rack. It’s backed

  • 0

I have built a pretty simple REST service in Sinatra, on Rack. It’s backed by 3 Tokyo Cabinet/Table datastores, which have connections that need to be opened and closed. I have two model classes written in straight Ruby that currently simply connect, get or put what they need, and then disconnect. Obviously, this isn’t going to work long-term.

I also have some Rack middleware like Warden that rely on these model classes.

What’s the best way to manage opening and closing the connections? Rack doesn’t provide startup/shutdown hooks as I’m aware. I thought about inserting a piece of middleware that provides reference to the TC/TT object in env, but then I’d have to pipe that through Sinatra to the models, which doesn’t seem efficient either; and that would only get be a per-request connection to TC. I’d imagine that per-server-instance-lifecycle would be a more appropriate lifespan.

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-14T06:03:09+00:00Added an answer on May 14, 2026 at 6:03 am

    If you have other Rack middleware that depend on these connections (by way of a dependence on your model classes), then I wouldn’t put the connection logic in Sinatra — what happens if you rip out Sinatra and put in another endpoint?

    Since you want connection-per-application rather than connection-per-request, you could easily write a middleware that initialized and cleaned up connections (sort of the Guard Idiom as applied to Rack) and install it ahead of any other middleware that need the connections.

    class TokyoCabinetConnectionManagerMiddleware
      class <<self
        attr_accessor :connection
      end
    
      def initialize(app)
        @app = app
      end
    
      def call(env)
        open_connection_if_necessary!
        @app.call(env)
      end
    
      protected
    
      def open_connection_if_necessary!
        self.class.connection ||= begin
          ... initialize the connection ..
          add_finalizer_hook!
        end
      end
    
      def add_finalizer_hook!
        at_exit do
          begin
            TokyoCabinetConnectionManagerMiddleware.connection.close!
          rescue WhateverTokyoCabinetCanRaise => e
            puts "Error closing Tokyo Cabinet connection. You might have to clean up manually."
          end
        end
      end
    end
    

    If you later decide you want connection-per-thread or connection-per-request, you can change this middleware to put the connection in the env Hash, but you’ll need to change your models as well. Perhaps this middleware could set a connection variable in each model class instead of storing it internally? In that case, you might want to do more checking about the state of the connection in the at_exit hook because another thread/request might have closed it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a PHP calendar system and have pretty much everything done but
I have a pretty large site and every page is built from several included
I have built a simple C#.Net app on a M/C with only .Net FX
I have built a web page which contains a Crystal Report built using the
I built a (very simple) library, which I can call just fine from another
I have a pretty simple HABTM set of models class Tag < ActiveRecord::Base has_and_belongs_to_many
I have a table with a few relational columns and one XML column which
I have an embedded system posting data to a JSON REST service via HTTP.
I have to develop a pretty simple php website so I don't need framework.
I have built a website using php and simple HTML/CSS for a client. 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.