Managing Persistent Connections

Using PerlEx::DBI

PerlEx::DBI provides a mechanism to handle persistent database connections within an Interpreter Class, providing better scalability than persisting the connections within a BEGIN block. PerlEx::DBI will cache the database handles used by all scripts that execute within a particular interpreter. As long as the connection information is identical across scripts, the same handle can be reused across scripts executed within the same interpreter. To enable PerlEx::DBI, you must add:


to the CommandLineOptions setting, or add:

    use PerlEx::DBI ();

to the StartupCode setting.

Your script does not require any modification to work with PerlEx::DBI. See the PerlEx::DBI reference documentation for more information.

Using BEGIN and END Blocks

If you cannot make use of the PerlEx::DBI approach described above, you can create your own persistent database connections using BEGIN and END blocks. The following sample code illustrates how to do it:

    # start of .plex file
    BEGIN {
      use DBI;
      $dbh = DBI->connect('dbi:ODBC:DSN', 'user', 'password');

    <the main body of your Perl code goes here>

    END {

The above example opens a connection to a database in the BEGIN block, keeps it open for the duration of the script, and closes it in the END block.

This method of persisting database connections does not provide all the features of PerlEx::DBI, such as managing connection timeouts.

For more information, see BEGIN and END blocks.