Re: using callbacks with connect_cached
On Aug 24, 2013, at 9:40 PM, Vincent Veyron vv.li...@wanadoo.fr wrote: Not quite : I thought connected_cache.new would enable me to set the correct datestyle once for a cached connection, and then be cached with it, but we've seen that won't work. Using the connected callback would do a 'set datestyle to xyz' for each connection, even if cached. I realize it's no big deal (.5ms on my lowly Atom processor), but every little bit helps, as I'm sure you know. So I now set the datestyle when the user logs in (hence once, with $dbh-do('SET datestyle TO xyz'), and the database handle is cached and reused as is. You can also use a private attribute in connect_cached.new to tell the connected callback to do its thing: my $cb = { 'connect_cached.new' = sub { $_[4]-{private_is_new} = 1; return; }, connected = sub { my $dbh = shift; warn connected\n if delete $dbh-{private_is_new}; return; } }; for (1..3) { DBI-connect_cached('dbi:SQLite:', '', '', { PrintError = 0, RaiseError = 1, AutoCommit = 1, Callbacks = $cb, }); } This emits connected only once. Best, David
DBI - SQL Server - DAC Connection
I am running Activestate Perl 5.8.9 on Windows 7. I have been able to establish a connection to MS SQL Server using DBI for years. What I need now is to establish a Dedicated Administrator Connection (DAC) to SQL Server via DBI. I can establish a DAC connection to SQL Server using sqlcmd in a cmd window by prepending ADMIN: to the server name or by using the -A option, and this works fine. When I prepend ADMIN: to the server name in my DBI connection string I get the following error message: DBI connect('Driver={SQL Server};Server=ADMIN:localhost\SQLEXPRESS;Database=pubs','sa',...) failed: MicrosoftODBC SQL Server DriverDBNETLIBSQL Server does not exist or access denied. (SQL-08001) state was 08001 now 01000 When I add the DAC listening port to the end of the server name in my DBI connection string I get the following error message: DBI connect('Driver={SQL Server};Server=localhost\SQLEXPRESS,60488;Database=pubs','sa',...) failed: MicrosoftODBC SQL Server DriverDBNETLIBInvalid connection. (SQL-08001) state was 08001 now 01000 I have tried Super Search and Google, and have come up empty. Does anyone know how to establish a DAC connection to SQL Server using DBI? Thanks, Roy Fulbright Also posted on Perlmonks at: http://perlmonks.org/?node_id=1050822
Re: using callbacks with connect_cached
On Sun, Aug 25, 2013 at 01:38:57PM +0200, David E. Wheeler wrote: On Aug 24, 2013, at 9:40 PM, Vincent Veyron vv.li...@wanadoo.fr wrote: You can also use a private attribute in connect_cached.new to tell the connected callback to do its thing: my $cb = { 'connect_cached.new' = sub { $_[4]-{private_is_new} = 1; return; }, connected = sub { my $dbh = shift; warn connected\n if delete $dbh-{private_is_new}; return; } }; for (1..3) { DBI-connect_cached('dbi:SQLite:', '', '', { PrintError = 0, RaiseError = 1, AutoCommit = 1, Callbacks = $cb, }); } This emits connected only once. If you're going to use a callback on connected and an attribute then there's no need to also use connect_cached.new. Just this should do: connected = sub { my $dbh = shift; if (++$dbh-{private_connected_count} == 1) { ... # is new connection } return; } Tim. p.s. I'd be delighted to get a doc patch that notes the need to use a lexical for the callbacks hashref on connect_cached. p.p.s. I'd also consider a patch to add a 'connect_cached.connected' callback for new connections.
Re:
On Aug 26, 2013, at 2:42 AM, Vincent Veyron vv.li...@wanadoo.fr wrote: Right now, I'm left wondering *how* I am going to pass said lexical to the package 'db_handle' I pasted in the message that started the thread, in the context of mod_perl, so not sure about writing up on it yet. Even though you’re using connect_cached(), you should consider holding on to the DBI handle yourself somewhere. Like in an attribute of a singleton object. You might find DBIx::Connector useful to use that way, rather than connect_cached(). Best, David