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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:12:31+00:00 2026-05-13T20:12:31+00:00

I want to execute a database query in a background thread. The OmniThread library

  • 0

I want to execute a database query in a background thread. The OmniThread library will help me with all the thread stuff, but there is one thing I don’t understand so far:

Every thread needs a separate database connection. The background thread therefore creates the DB connection, creates the query and then executes it.

Now I could access the query results using the query object of the background thread.
But after the query is executed, I want to access the query results in the main thread.

If I just refer to the background-thread query object, does this cause problems because I am accessing a DB connection in another thread?

As I understand, in this case the main thread would not have it’s separate DB connection and use the one from the background thread which is not good.

Where is my thinking distorted and what’s the right way to do this?

  • 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-13T20:12:32+00:00Added an answer on May 13, 2026 at 8:12 pm

    If your OTL task needs to load a sorted list of companies that match the criteria:

    // create and open query to fetch list of companies
    while not qryCompanies.Eof do begin
      C := TCompany.Create;
      try
        C.LoadFromDataset(qryCompanies);
        Companies.Add(C);
      except
        C.Free;
        raise;
      end;
      qryCompanies.Next;
    end;
    

    C is your business object for a company. It may by an object (TCompany) or an interface (ICompany) implemented by the object. Companies is a TList<TCompany> or TList<ICompany>. At the end of the task you send the list of companies to the VCL thread:

    Task.Comm.Send(TOmniMessage.Create(MSGID_LIST_OF_COMPANIES, Companies));
    

    In the form where you want to display the list of companies you handle the OnTaskMessage event of the otlEventMonitor instance that is monitoring your task:

    procedure TListBaseFrame.otlEventMonitorTaskMessage(
      const task: IOmniTaskControl);
    var
      MsgID: word;
      MsgValue: TOmniValue;
    begin
      task.Comm.Receive(MsgID, MsgValue);
      Assert(MsgValue.IsInterface);
      if fLoaderTask = task then begin
        SetLoadedData(MsgID, MsgValue.AsInterface); // or MsgValue.AsObject);
        fLoaderTask := nil;
      end;
    end;
    

    The list of companies replaces the previous list and can be displayed in the grid.

    Similarly you could return a single company object / interface to be displayed and edited.

    Two things that are worth thinking about:

    • If you have so far preferred objects to interfaces, writing multi-threaded programs may be a reason to reconsider that. If you create your objects in a background thread, then pass them to the VCL thread and forget about them in the background thread, then objects may work well. I found however that much better performance can be had by caching objects in the application, and loading only records from the database that haven’t been loaded yet, or that have changed. All my tables have a change index (64 bit integer, a time stamp may work as well) attached to it, that is changed with every update. Instead of executing a

      select * from foo where (...) order by (...)
      

      I only ever execute a

      select id, change_index from foo where (...) order by (...)
      

      then check in the cache whether an object with the same id (primary key) and change index already exists, if so return the cached object, and only if not create a new business object and load all columns.

      But if you cache objects you will have references to them from multiple threads, and ownership issues soon get so complicated that lifetime management based on reference counts is the only way to stay sane. Using interfaces instead of objects helps a lot in this regard.

    • Adding a synchronization object to each business object is necessary, if multiple threads can access them concurrently. It is of course possible, but may introduce additional complexity and potential deadlocks. If you implement your business objects as immutable, then no locks are needed at all. I am using that approach more and more, and while it takes some getting used to it it can simplify things a lot.

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

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Entity Framework designer is terrible - I've had the… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If by "hijack" you meant sniff the packets then what… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If you want two actions to be atomic, embed them… May 14, 2026 at 5:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.