http://www.perlmonks.org?node_id=785366

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

Hello Monks,
I will be used DBI module.I wanted to create a database connection with perl a time,The connection will be global Whenever I need to execute the postgres query,I will check the connection and execute the sql query is it good method or whenever i need to execute the postgres query on that time I need to connect with database and execute the query,once operation is succeed,close the connection.is it good method.which is the best method in this module.

Replies are listed 'Best First'.
Re: Perl with Database connection.
by scorpio17 (Canon) on Aug 03, 2009 at 13:35 UTC

    The answer is "it depends".

    In most situations, database connections are a limited resource, just like disk space or bandwidth, etc. For example, if you had a maximum limit of 100 database connections, and many clients trying to connect at the same time, you would want each client to get its data then disconnect as soon as possible, in order to not block other clients. This would be like an ATM machine checking an account balance at a bank.

    On the other hand, if you had a server connected to the database, and lots of clients making similar requests at the same time, it might be better for the server to establish a single, persistent connection to the database, and cache commonly accessed data to avoid redundant queries. This would be like a web site where users read book reviews, for example.

Re: Perl with Database connection.
by Anonymous Monk on Aug 03, 2009 at 07:37 UTC
    Either one will work

      Is it you are telling that both the method are same?

      I have been using the ping method from the DBI module every time I execute a Postgresql query.

        Is it you are telling that both the method are same?

        Essentially. Keep a connection open is only better under high traffic (like perlmonks)

        hello ree,
        I am not asking which method you are using.I need to know creating the many connection is better or creating a single connection(global) is better.I need to know will it cause any data base and performance issue.

Re: Perl with Database connection.
by bichonfrise74 (Vicar) on Aug 03, 2009 at 18:57 UTC
    I think it is better to just have a single connection if possible. So that if anything goes wrong, you can just terminate a single process in the database.

    Imagine if you have multiple connection and something goes wrong then you can end up saturating your database with your script and making it unusable.