Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

import POE::Component::Pool::DBI 0.014 from CPAN

git-cpan-module: POE::Component::Pool::DBI
git-cpan-version: 0.014
git-cpan-authorid: TAG
  • Loading branch information...
commit b9da1ee57b0e7a1ac7aabb3113506833951a9550 1 parent a0492e0
Scott S. McCoy authored February 19, 2009 schwern committed December 07, 2009
123  DBI.pm
@@ -18,7 +18,7 @@ use constant {
18 18
     REFCOUNT_IDENTIFIER => "queries",
19 19
 };
20 20
 
21  
-our $VERSION = 0.012;
  21
+our $VERSION = 0.014;
22 22
 
23 23
 sub new {
24 24
     my ($class, %args) = @_;
@@ -28,9 +28,6 @@ sub new {
28 28
     # For tracking the job id.
29 29
     $self->{current_id} = 0;
30 30
 
31  
-    use Data::Dumper;
32  
-    print Dumper($self);
33  
-
34 31
     print "Connections: $args{connections}";
35 32
 
36 33
     # This creates the threadpool which does the actual work.  The entry point
@@ -125,57 +122,65 @@ sub query_database {
125 122
     # object.  (perl threads are strange that way).
126 123
     my ($action, $dsn, $user, $pass, $id, $caller, $cb, $query, @args) = @_;
127 124
 
128  
-    DEBUG && warn "testing & creating database connection";
129  
-
130  
-    # "our" is a shared variable, but it's only shared within each thread
131  
-    # (assuming that crunch_data is never called outside a thread).  This
132  
-    # statement will call DBI->connect if, and only if, the $dbh is not defined
133  
-    # and you cannot call $dbh->ping if it is defined.
134  
-    #
135  
-    # I do this rather than connect_cached, just incase any initialized
136  
-    # connections were made before the ithread was created.  This mechanism
137  
-    # provides a bit more safety in terms of ensuring that we get a unique
138  
-    # connection per thread.
139  
-    our $dbh = DBI->connect($dsn, $user, $pass)
140  
-        unless defined $dbh && $dbh->ping;
141  
-
142  
-    DEBUG && warn "preparing query";
143  
-    # prepare_cached should be safe, since we should have a unique connection
144  
-    # per our previous statement, and my tests showed this optimizes querying
145  
-    # by up to 8x over creating a new prepared statement each time.
146  
-    my $sth = $dbh->prepare_cached($query);
147  
-
148  
-    DEBUG && warn "executing query";
149  
-    $sth->execute(@args);
150  
-
151  
-    if ($action eq "do") {
152  
-        $sth->finish;
153  
-        return $id, $caller, $cb;
154  
-    }
155  
-    elsif ($action eq "query") {
156  
-        DEBUG && warn "fetching results";
157  
-        my $results = $sth->fetchall_arrayref({});
158  
-        my @response;
159  
-
160  
-        for my $record (@$results) {
161  
-            # Create a shared hashref.
162  
-            my $r = &share({});
163  
-
164  
-            # Copy all values into the hashref.
165  
-            @$r{keys %$record} = values %$record;
166  
-
167  
-            # Put the shared hashref into the response
168  
-            push @response, $r;
  125
+    my @response = eval {
  126
+        DEBUG && warn "testing & creating database connection";
  127
+    
  128
+        # "our" is a shared variable, but it's only shared within each thread
  129
+        # (assuming that crunch_data is never called outside a thread).  This
  130
+        # statement will call DBI->connect if, and only if, the $dbh is not defined
  131
+        # and you cannot call $dbh->ping if it is defined.
  132
+        #
  133
+        # I do this rather than connect_cached, just incase any initialized
  134
+        # connections were made before the ithread was created.  This mechanism
  135
+        # provides a bit more safety in terms of ensuring that we get a unique
  136
+        # connection per thread.
  137
+        our $dbh = DBI->connect($dsn, $user, $pass)
  138
+            unless defined $dbh && $dbh->ping;
  139
+    
  140
+        DEBUG && warn "preparing query";
  141
+        # prepare_cached should be safe, since we should have a unique connection
  142
+        # per our previous statement, and my tests showed this optimizes querying
  143
+        # by up to 8x over creating a new prepared statement each time.
  144
+        my $sth = $dbh->prepare_cached($query);
  145
+    
  146
+        DEBUG && warn "executing query";
  147
+        $sth->execute(@args);
  148
+    
  149
+        if ($action eq "do") {
  150
+            $sth->finish;
  151
+            return $id, $caller, $cb;
169 152
         }
170  
-
171  
-        DEBUG && warn "returnning to caller";
172  
-
173  
-        $sth->finish;
174  
-
175  
-        # This is automatically placed in a shared array, as per
176  
-        # PoCo::Thread::Pool
177  
-        return $id, $caller, $cb, @response;
  153
+        elsif ($action eq "query") {
  154
+            DEBUG && warn "fetching results";
  155
+            my $results = $sth->fetchall_arrayref({});
  156
+            my @response;
  157
+    
  158
+            for my $record (@$results) {
  159
+                # Create a shared hashref.
  160
+                my $r = &share({});
  161
+    
  162
+                # Copy all values into the hashref.
  163
+                @$r{keys %$record} = values %$record;
  164
+    
  165
+                # Put the shared hashref into the response
  166
+                push @response, $r;
  167
+            }
  168
+    
  169
+            DEBUG && warn "returnning to caller";
  170
+    
  171
+            $sth->finish;
  172
+    
  173
+            # This is automatically placed in a shared array, as per
  174
+            # PoCo::Thread::Pool
  175
+            return @response;
  176
+        }
  177
+    };
  178
+    if ($@) {
  179
+        warn "An unexpected error was trapped while trying to run a query: $@";
  180
+        warn "The query was: $query";
178 181
     }
  182
+
  183
+    return $id, $caller, $cb, @response;
179 184
 }
180 185
 
181 186
 # This component will send a message to the caller session, with the data
@@ -221,17 +226,17 @@ use POE qw( Component::Pool::DBI );
221 226
              my ($kernel, $heap) = @_[ KERNEL, HEAP ];
222 227
  
223 228
              my $dbpool = POE::Component::Pool::DBI->new(
224  
-                 MaxConnections  => 10,
225  
-                 DSN             => "DBI:mysql:database=test",
226  
-                 Username        => "username",
227  
-                 Password        => "password"
  229
+                 connections     => 10,
  230
+                 dsn             => "DBI:mysql:database=test",
  231
+                 username        => "username",
  232
+                 password        => "password"
228 233
              );
229 234
  
230 235
              # Outstanding queries keep the calling session alive.
231 236
              $dbpool->query(
232 237
                  callback => "handle_result",
233 238
                  query    => "select foo from bar where foo = ?",
234  
-                 args     => [ "foo" ],
  239
+                 params   => [ "foo" ],
235 240
                  userdata => "example"
236 241
              );
237 242
 
@@ -329,7 +334,7 @@ The query argument holds the SQL or PL/SQL statement to execute.  The statement
329 334
 will be invoked immediately, but it will be cached within the connection
330 335
 (see L<DBI> prepare_cached).
331 336
 
332  
-=item args
  337
+=item params
333 338
 
334 339
 The arguments to provide to DBI's execute method (see L<DBI>).
335 340
 
24  META.yml
... ...
@@ -1,12 +1,16 @@
1  
-# http://module-build.sourceforge.net/META-spec.html
2  
-#XXXXXXX This is a prototype!!!  It will change in the future!!! XXXXX#
3  
-name:         POE-Component-Pool-DBI
4  
-version:      0.012
5  
-version_from: DBI.pm
6  
-installdirs:  site
7  
-requires:
  1
+--- #YAML:1.0
  2
+name:                POE-Component-Pool-DBI
  3
+version:             0.014
  4
+abstract:            A POE Component for managing Boss/Worker DBI pools
  5
+license:             ~
  6
+author:              
  7
+    - Scott S. McCoy (tag@cpan.org)
  8
+generated_by:        ExtUtils::MakeMaker version 6.42
  9
+distribution_type:   module
  10
+requires:     
8 11
     POE:                           0.3
  12
+    POE::Component::Pool::Thread:  0.015
9 13
     threads:                       1.03
10  
-
11  
-distribution_type: module
12  
-generated_by: ExtUtils::MakeMaker version 6.30_01
  14
+meta-spec:
  15
+    url:     http://module-build.sourceforge.net/META-spec-v1.3.html
  16
+    version: 1.3
1  Makefile.PL
@@ -15,6 +15,7 @@ WriteMakefile
15 15
 
16 16
     PREREQ_PM    => {
17 17
         POE      => 0.30,
  18
+        'POE::Component::Pool::Thread'      => 0.015,
18 19
         threads  => 1.03,
19 20
     },
20 21
 
3  t/no_tests_dont_email_me.t
@@ -4,5 +4,6 @@ use Test;
4 4
 BEGIN {
5 5
     plan tests => 1;
6 6
 }
  7
+use POE qw( Component::Pool::DBI );
7 8
 
8  
-ok 1;
  9
+ok(defined POE::Component::Pool::DBI->can("new"));

0 notes on commit b9da1ee

Please sign in to comment.
Something went wrong with that request. Please try again.