perl - How to disconnect from all database connection strings -


i have perl script connects many databases using different connection drivers.

is there way can write single disconnect function in end disconnect if there active session?

example :

connection 1: $dbh->oracle; connection 2: $dbh->sql 

can have common disconnect string both databases?

you can implement using visit_handles dbi.

use strict; use warnings; use dbi;  $dbh  = dbi->connect( 'dbi:mysql:database=test;host=localhost', 'root', 'pw' ); $dbh2 = dbi->connect( 'dbi:mysql:database=test;host=localhost', 'root', 'pw' );  dbi->visit_handles(     sub {         ( $driver_handle, $info ) = @_;          if ($driver_handle->{type} eq 'db') {             # clean transaction or disconnect each handle             $driver_handle->disconnect;         }          return 1;     } ); 

the code reference passed visit_handles called each driver handle. if returns true value, subsequently call visit_child_handles same code reference. way, can match ones database handles (db) , disconnect them explicitly.

as borodin states in answer need take care of half-done transactions before disconnecting. solution gives means connection handles single place.


Comments