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

Good Morning Monks,
I have a question regarding DB Connections..

I have script 'A', which performs some DB operations.. I need 'A' to invoke script 'B' which uses the same DB connection as 'A'.. But, somehow, the connection is not available..

Also please note, that the connections are made in a common perl Module.. So, there are functions to create a connection, disconnect, and execute statements.. This means that, i am calling this 'connect' subroutine in the perl module from Script 'A', but the connection created in script 'A' is non-existent in script 'B'(when i call the function to execute a statement, it gives me an error 'OCISessionEnd')..

P.S:-All this is in CGI

TIA
Blub:)
  • Comment on DB Connections across scripts from a module

Replies are listed 'Best First'.
Re: DB Connections across scripts from a module
by GrandFather (Saint) on Jun 03, 2010 at 04:17 UTC

    How do you 'invoke' script B? If it is by running the script using exec or system then you can't share the DB connection - it's a different application using different memory.

    If you are loading script B using require or some similar technique show us the code you are using that is failing. With a little imagination you should be able to reduce the code to a couple of small sample scripts that demonstrate the issue (by attempting to share a variable for example).

    True laziness is hard work
      In Script A, i am having a form.. I populate the text fields in the form, and onSubmit of the form, i am invoking Script B.. Script A, will populate the contents of the form using a DBConnection.. When Script B is invoked, will the DBConnection from Script A still exist, can i re-use it?
      Hope, you are clear with the problem..
        Uhm, that looks like the description of a simple Web setup. But one in which scriptA does not invoke scriptB. It's more like:
        1. Browser connects to server.
        2. Browser makes a request to server.
        3. Server calls scriptA.
        4. scriptA finishes.
        5. Server send output of scriptA to browser.
        6. Connection terminates.
        7. User goes off to buy a latte.
        8. User comes back, watches some youtube videos.
        9. User fills in form.
        10. User hits submit.
        11. Browser connects to server.
        12. Browser makes request to server.
        13. Server calls scriptB.
        14. scriptB finishes.
        15. Server sends output of scriptB to browser.
        16. Connection terminates.
        By default, connections used in one script will not be available in another script. But that doesn't mean you cannot use a server framework where connections are shared - but even then, a connection will only be reused if the second request comes quickly enough.
Re: DB Connections across scripts from a module
by ikegami (Patriarch) on Jun 03, 2010 at 04:18 UTC

    What's the question, "why?" or "how?"?

    The former will require us to know what you mean by "invoking a script" and how you pass the connection from one script to the other to give a good answer.

    As for the latter, I'd want more details about what you are trying to do before I start contemplating possibilities.

Re: DB Connections across scripts from a module
by codeacrobat (Chaplain) on Jun 03, 2010 at 06:24 UTC
    Sounds like you need http://sqlrelay.sourceforge.net/sqlrelay. It interfaces nicely with DBI and I used SQLRelay a lot to reduce the connection open/close fire when I had to deal with hundreds nagios triggered db monitoring scripts.

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
Re: DB Connections across scripts from a module
by proceng (Scribe) on Jun 03, 2010 at 14:32 UTC
    Just a guess (since you do not show any code:
    When you execute the connect:
    $dbh = DBI->connect($data_source, $username, $auth, \%attr);
    are you declaring the database handle globally or are you declaring it within the "connect" subroutine? If it is the latter, you will never see a connection in other subroutines.

    Also, are you checking to make sure that the connect was successful? See perldoc DBI.

      Hi proceng,
      I am declaring it globally, so i guess, the var does not go out of context and also, i am able to access the var from other subs..

      As JavaFan had stated, the connection is closed when the first script A's execution is complete.. And for the next script to execute, (as it is executed a bit later) we need to initiate a new connection.. Perl modules get unloaded and loaded back in the memory(for every script execution), i guess..
      If i have said something wrong, please correct me

      Regards,
      Blub:)

        If i have said something wrong

        Most definitely. It's not clear what since I don't know how you invoke each script.

        If the process in which script A executed exited, then the var did go out of scope contrary to what you said.

        If the process in which script A executed did not exit, then no modules got unloaded contrary to what you said.