… | |
… | |
12 | |
12 | |
13 | (Note that in this manual, "thread" refers to real threads as implemented |
13 | (Note that in this manual, "thread" refers to real threads as implemented |
14 | by the Coro module, not to the built-in windows process emulation which |
14 | by the Coro module, not to the built-in windows process emulation which |
15 | unfortunately is also called "threads") |
15 | unfortunately is also called "threads") |
16 | |
16 | |
17 | This module "patches" DBD::mysql database handles so that they do not |
17 | This module replaces the I/O handlers for a database connection, with the |
18 | block the whole process, but only the thread that they are used in. |
18 | effect that "patched" database handles no longer block the all threads of |
|
|
19 | a process, but only the thread that does the request. |
19 | |
20 | |
20 | This can be used to make parallel sql requests using Coro, or to do other |
21 | This can be used to make parallel sql requests using Coro, or to do other |
21 | stuff while mysql is rumbling in the background. |
22 | stuff while mysql is rumbling in the background. |
22 | |
23 | |
23 | =head2 CAVEAT |
24 | =head2 CAVEAT |
24 | |
25 | |
25 | Note that this module must be linked against exactly the same (shared, |
26 | Note that this module must be linked against exactly the same (shared, |
26 | possibly not working with all OSes) F<libmysqlclient> library as |
27 | possibly not working with all OSes) F<libmysqlclient> library as |
27 | DBD::mysql, otherwise it will not work. |
28 | DBD::mysql, otherwise it will not work. |
28 | |
29 | |
|
|
30 | Also, this module requires a header file that apparently isn't installed |
|
|
31 | everywhere (F<violite.h>), and therefore comes with it's own copy, which |
|
|
32 | might or might not be compatible to the F<violite.h> of your library - |
|
|
33 | when in doubt, make sure all the libmysqlclient header files are installed |
|
|
34 | and delete the F<violite.h> header that comes with this module. |
|
|
35 | |
|
|
36 | On the good side, this module does a multitude of checks to ensure that |
|
|
37 | the libray versions match on the binary level, so on incompatibilities you |
|
|
38 | should expect an exception when trying to unblock a handle, rather than |
|
|
39 | data corruption. |
|
|
40 | |
29 | Also, while this module makes database handles non-blocking, you still |
41 | Also, while this module makes database handles non-blocking, you still |
30 | cannot run multiple requests in parallel on the same database handle. If |
42 | cannot run multiple requests in parallel on the same database handle. If |
31 | you want to run multiple queries in parallel, you have to create multiple |
43 | you want to run multiple queries in parallel, you have to create multiple |
32 | database connections, one for each thread that runs queries. Not doing so |
44 | database connections, one for each thread that runs queries. Not doing |
33 | can corrupt your data - use a Coro::Semaphore when in doubt. |
45 | so can corrupt your data - use a Coro::Semaphore to protetc access to a |
|
|
46 | shared database handle when in doubt. |
34 | |
47 | |
35 | If you make sure that you never run two or more requests in parallel, you |
48 | If you make sure that you never run two or more requests in parallel, you |
36 | can freely share the database handles between threads, of course. |
49 | can freely share the database handles between threads, of course. |
37 | |
|
|
38 | Also, this module uses a number of "unclean" techniques (patching an |
|
|
39 | internal libmysql structure for one thing) and was initially hacked within |
|
|
40 | a few hours on a long flight to Malaysia. |
|
|
41 | |
|
|
42 | It does, however, check whether it indeed got the structure layout |
|
|
43 | correct, so you should expect perl exceptions or early crashes as opposed |
|
|
44 | to data corruption when something goes wrong during patching. |
|
|
45 | |
50 | |
46 | =head2 SPEED |
51 | =head2 SPEED |
47 | |
52 | |
48 | This module is implemented in XS, and as long as mysqld replies quickly |
53 | This module is implemented in XS, and as long as mysqld replies quickly |
49 | enough, it adds no overhead to the standard libmysql communication |
54 | enough, it adds no overhead to the standard libmysql communication |
… | |
… | |
100 | |
105 | |
101 | sub readable { &Coro::Handle::FH::readable } |
106 | sub readable { &Coro::Handle::FH::readable } |
102 | sub writable { &Coro::Handle::FH::writable } |
107 | sub writable { &Coro::Handle::FH::writable } |
103 | |
108 | |
104 | BEGIN { |
109 | BEGIN { |
105 | our $VERSION = '1.2'; |
110 | our $VERSION = '1.24'; |
106 | |
111 | |
107 | require XSLoader; |
112 | require XSLoader; |
108 | XSLoader::load Coro::Mysql::, $VERSION; |
113 | XSLoader::load Coro::Mysql::, $VERSION; |
109 | } |
114 | } |
110 | |
115 | |
… | |
… | |
140 | if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { |
145 | if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { |
141 | require Coro::Handle; |
146 | require Coro::Handle; |
142 | $fh = Coro::Handle::unblock ($fh); |
147 | $fh = Coro::Handle::unblock ($fh); |
143 | } |
148 | } |
144 | |
149 | |
|
|
150 | warn "$sock,$DBH->{sockfd},$DBH->{mysql_clientversion},$fh\n";#d# |
145 | _patch $sock, $DBH->{sockfd}, $DBH->{mysql_clientversion}, $fh, tied ${$fh}; |
151 | _patch $sock, $DBH->{sockfd}, $DBH->{mysql_clientversion}, $fh, tied *$$fh; |
146 | } |
152 | } |
147 | |
153 | |
148 | $DBH |
154 | $DBH |
149 | } |
155 | } |
150 | |
156 | |
… | |
… | |
200 | |
206 | |
201 | =head1 SEE ALSO |
207 | =head1 SEE ALSO |
202 | |
208 | |
203 | L<Coro>, L<PApp::SQL> (a user friendly but efficient wrapper around DBI). |
209 | L<Coro>, L<PApp::SQL> (a user friendly but efficient wrapper around DBI). |
204 | |
210 | |
|
|
211 | =head1 HISTORY |
|
|
212 | |
|
|
213 | This module was initially hacked together within a few hours on a long |
|
|
214 | flight to Malaysia, and seems to have worked ever since, with minor |
|
|
215 | adjustments for newer libmysqlclient libraries. |
|
|
216 | |
205 | =head1 AUTHOR |
217 | =head1 AUTHOR |
206 | |
218 | |
207 | Marc Lehmann <schmorp@schmorp.de> |
219 | Marc Lehmann <schmorp@schmorp.de> |
208 | http://home.schmorp.de/ |
220 | http://home.schmorp.de/ |
209 | |
221 | |