Skip to content

Use DBIx::Connector

yuki-kimoto edited this page Oct 25, 2012 · 1 revision

Use DBIx::Connector

DBIx::Custom Documents >

When you want to run web application in production environment, you will need database connection management. If database conection remain for a long time, the connection will be dropped automatically by RDBMS. If you use prefork web server or mod_perl, connection will be dropeed unexpectedly.

Let's use connection manager. DBIx::Connector, which is one of connection managers, fix database connection automatically. When you use DBIx::Connector with DBIx::Connector, you set connector attribute to 1.

my $dbi = DBIx::Custom->connect(
  dsn => "dbi:mysql:database=$database",
  user => $user,
  password => $password,
  dbi_option => {mysql_enable_utf8 => 1},
  connector => 1
);

You can get DBIx::Connector by connector attribute.

my $connector = $dbi->connector;

Execute one transaction.

$dbi->connector->txn(sub {
  # Process1
  ...

  # Process2
  ...
});

See also DBIx::Connector document.

Use other connection manager

You can use other connection manager. Connection manager must have dbh method.

my $connector = DBIx::SomeConnector->new(
  "dbi:mysql:database=$database",
  $user,
  $password,
  {
      %{DBIx::Custom->new->default_dbi_option},
      mysql_enable_utf8 => 1
  }
);

my $dbi = DBIx::Custom->connect(connector => $connector);

You can set connector attribute to connection manager. Set DBI option to DBIx::Custom default options(which is get by default_dbi_option method) and your options.

Clone this wiki locally