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
| [reply] |
|
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..
| [reply] |
|
Uhm, that looks like the description of a simple Web setup. But one in which scriptA does not invoke scriptB. It's more like:
- Browser connects to server.
- Browser makes a request to server.
- Server calls scriptA.
- scriptA finishes.
- Server send output of scriptA to browser.
- Connection terminates.
- User goes off to buy a latte.
- User comes back, watches some youtube videos.
- User fills in form.
- User hits submit.
- Browser connects to server.
- Browser makes request to server.
- Server calls scriptB.
- scriptB finishes.
- Server sends output of scriptB to browser.
- 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.
| [reply] |
|
| [reply] |
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.
| [reply] |
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']});
| [reply] [d/l] |
|
| [reply] |
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. | [reply] [d/l] |
|
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:)
| [reply] |
|
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.
| [reply] |