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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:26:44+00:00 2026-06-09T12:26:44+00:00

For example, suppose there is the code in Rails 3.2.3 def test_action a =

  • 0

For example, suppose there is the code in Rails 3.2.3

def test_action
  a = User.find_by_id(params[:user_id])
  # some calculations.....
  b = Reporst.find_by_name(params[:report_name])
  # some calculations.....
  c = Places.find_by_name(params[:place_name])
end

This code does 3 requests to database and opens 3 different connections. Most likely it’s going to be a quite long action.

Is there any way to open only one connection and do 3 requests within it? Or I want to control which connection to use by myself.

  • 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-06-09T12:26:46+00:00Added an answer on June 9, 2026 at 12:26 pm

    You would want to bracket the calls with transaction:

    Transactions are protective blocks where SQL statements are only
    permanent if they can all succeed as one atomic action. The classic
    example is a transfer between two accounts where you can only have a
    deposit if the withdrawal succeeded and vice versa. Transactions
    enforce the integrity of the database and guard the data against
    program errors or database break-downs. So basically you should use
    transaction blocks whenever you have a number of statements that must
    be executed together or not at all.

    def test_action
      User.transaction do
        a = User.find_by_id(params[:user_id])
        # some calculations.....
        b = Reporst.find_by_name(params[:report_name])
        # some calculations.....
        c = Places.find_by_name(params[:place_name])
      end
    end
    

    Even though they invoke different models the actions are encapsulated into one call to the DB. It is all or nothing though. If one fails in the middle then the entire capsule fails.

    Though the transaction class method is called on some Active Record
    class, the objects within the transaction block need not all be
    instances of that class. This is because transactions are per-database
    connection, not per-model.

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

Sidebar

Related Questions

Let's suppose there is a piece of code like this: my $str = 'some
For example, suppose I have the code: class Foo(object): def bar(self, x): '''blah blah
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
Suppose there is a piece of JavaScript code that is supported by different browsers,
Let's suppose there is a table user id_user | name | id_state 1 |
Suppose I have some code such as: float a, b = ...; // both
Is there any way of executing the already executed code in java. for example
Lets suppose there is a stored procedure that has 3 params. Out of all
Example: Suppose in the following example I want to match strings that do not
For example suppose I have the following two functions function a(param1) { console.log(param1); }

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.