1 |
NAME |
2 |
AnyEvent::DBI - asynchronous DBI access |
3 |
|
4 |
SYNOPSIS |
5 |
use AnyEvent::DBI; |
6 |
|
7 |
my $cv = AnyEvent->condvar; |
8 |
|
9 |
my $dbh = new AnyEvent::DBI "DBI:SQLite:dbname=test.db", "", ""; |
10 |
|
11 |
$dbh->exec ("select * from test where num=?", 10, sub { |
12 |
my ($rows, $rv) = @_; |
13 |
|
14 |
print "@$_\n" |
15 |
for @$rows; |
16 |
|
17 |
$cv->broadcast; |
18 |
}); |
19 |
|
20 |
# asynchronously do sth. else here |
21 |
|
22 |
$cv->wait; |
23 |
|
24 |
DESCRIPTION |
25 |
This module is an AnyEvent user, you need to make sure that you use and |
26 |
run a supported event loop. |
27 |
|
28 |
This module implements asynchronous DBI access my forking or executing |
29 |
separate "DBI-Server" processes and sending them requests. |
30 |
|
31 |
It means that you can run DBI requests in parallel to other tasks. |
32 |
|
33 |
The overhead for very simple statements ("select 0") is somewhere around |
34 |
120% to 200% (dual/single core CPU) compared to an explicit |
35 |
prepare_cached/execute/fetchrow_arrayref/finish combination. |
36 |
|
37 |
METHODS |
38 |
$dbh = new AnyEvent::DBI $database, $user, $pass, [key => value]... |
39 |
Returns a database handle for the given database. Each database |
40 |
handle has an associated server process that executes statements in |
41 |
order. If you want to run more than one statement in parallel, you |
42 |
need to create additional database handles. |
43 |
|
44 |
The advantage of this approach is that transactions work as state is |
45 |
preserved. |
46 |
|
47 |
Example: |
48 |
|
49 |
$dbh = new AnyEvent::DBI |
50 |
"DBI:mysql:test;mysql_read_default_file=/root/.my.cnf", "", ""; |
51 |
|
52 |
Additional key-value pairs can be used to adjust behaviour: |
53 |
|
54 |
on_error => $callback->($dbh, $filename, $line, $fatal) |
55 |
When an error occurs, then this callback will be invoked. On |
56 |
entry, $@ is set to the error message. $filename and $line is |
57 |
where the original request was submitted. |
58 |
|
59 |
If this callback returns and this was a fatal error ($fatal is |
60 |
true) then AnyEvent::DBI die's, otherwise it calls the original |
61 |
request callback without any arguments. |
62 |
|
63 |
If omitted, then "die" will be called on any errors, fatal or |
64 |
not. |
65 |
|
66 |
$dbh->exec ("statement", @args, $cb->($rows, $rv, ...)) |
67 |
Executes the given SQL statement with placeholders replaced by |
68 |
@args. The statement will be prepared and cached on the server side, |
69 |
so using placeholders is compulsory. |
70 |
|
71 |
The callback will be called with the result of "fetchall_arrayref" |
72 |
as first argument (or "undef" if the statement wasn't a select |
73 |
statement) and the return value of "execute" as second argument. |
74 |
Additional arguments might get passed as well. |
75 |
|
76 |
If an error occurs and the "on_error" callback returns, then no |
77 |
arguments will be passed and $@ contains the error message. |
78 |
|
79 |
SEE ALSO |
80 |
AnyEvent, DBI. |
81 |
|
82 |
AUTHOR |
83 |
Marc Lehmann <schmorp@schmorp.de> |
84 |
http://home.schmorp.de/ |
85 |
|