morgon has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I need advice about possible issues using DBI/DBD::Oracle (plus DBIx::Class) in a multi-threaded Perl-application on Linux.

Firstly I would like to know whether the whole stack (Oracle client-library, DBD::Oracle ... all the way up to DBIx::Class) is threadsafe at all.

Secondly (assuming it is threadsafe) I wonder if you can establish a database-connection and then start a number of threads (so that each thread then has access to the one connection) or if it would be better not to share the connection between threads but rather have each thread establish it's own connection.

In case DBD::Oracle cannot be used concurrently from several threads would it be possible for just one thread to have a database-connection or do threads and DBD::Oracle just do not mix (I can vaguely remember a warning I once got when DBD::Oracle discovered that my Perl had threading-support compiled in but I may remember wrongly)?

Could you also provide some "gut feeling" on what you think about multi-threaded database-apps - is it something you would consider for a business-critical application?

The reason I ask is that we are currently discussing several options of implementing some project infrastructure and I simply would like to eliminate problematic designs as early as possible.

Many thanks!

Replies are listed 'Best First'.
Re: DBD::Oracle and threading
by Corion (Patriarch) on Apr 21, 2009 at 21:19 UTC
Re: DBD::Oracle and threading
by BrowserUk (Patriarch) on Apr 22, 2009 at 00:51 UTC
    Could you also provide some "gut feeling" on what you think about multi-threaded database-apps

    If your proposed concurrent accesses to the DB would be accessing the same tables, then those accesses would most likely get serialised at the DB anyway, so you will likely not see much gain from the concurrency. And potentially you may suffer degradation. This is true regardless of whether you achieve the concurrency through, threads, processes or event loop architectures. It is also true whether you use Perl or C or Java.

    Whether there is any gain to be had from using threads as a part of a DB app will depend if there is anything else in the application that could usefully be processed whilst you are waiting for the DB to do its thing.

    If, for example, you need to perform some calculations on one set of results, whilst quering the next set: using a thread to perform those calculations whilst the main thread waits for the results of the next query has the potential for benefits.

    Likewise, if you are retrieving data for insertion from a disk or the network, then overlapping the insertion of one batch with the reading of then next batch can increase throughput.

    The thing to remember is, if your multi-threaded app only communicates with the DB via one thread, then it does not matter if the DB libraries are thread-safe, because from the point of view of the DB, your app looks and acts like a single-threaded application.

    Given that, and the fact that there is rarely any gain from accessing a DB concurrently for a single application due to the inherent serialisation that occurs at the DB itself, the "trick" to successfully exploiting threads in a DB application is to get away from your natural instinct to want to have multiple threads talking to the DB.

    Instead, consider how to partition the processing so that your application can be doing something useful whilst it is waiting for the DB to fulfill your requests. One key to efficiency, is to query your results, or post your updates, in smaller batches to strike a balance between the time taken to prepare updates or deal with results; and the time taken to send or recieve them.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: DBD::Oracle and threading
by perrin (Chancellor) on Apr 22, 2009 at 03:17 UTC
    You'd really be better off asking this on the dbi-users mailing list.