DBD::DB2: setting the application name

Hildo Biersma <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.dbi.sybase.devel
Organization Morgan Stanley
Message-ID <[email protected]>
DB2 allows client applications to set the application name using the 
SQL_ATTR_INFO_PROGRAMNAME attribute.  If you have a number of perl 
applications connecting to the same database, this allows you to 
distinguish between them by setting the application name to the script / 
library name instead of the default "perl".  The application name set is 
visible when an administrator performs "list applications".

I have long wanted to do this from DBD::DB2.  I asked IBM to add this to 
the driver and received an internal defect number for this (166029); 
however, the latest DBD::DB2 release (1.3) does not include this feature.

The attached patch shows how this feature can be added.  I hope IBM gets 
around to including this in the next DBD::DB2 release; in the meantime, 
other users of DBD::DB2 may benefit from this change.

This feature is used as follows:

   my $dbh = DBI->connect("dbi:DB2:$dsn", $userid, $password,
                          { 'db2_application_name' => $name });

Cheers,
   Hildo Biersma
dbdimp.c.diff (text/plain, 761 B)
*** dbdimp.c.orig	Tue Dec 30 10:03:29 2008
--- dbdimp.c	Tue Dec 30 10:02:58 2008
***************
*** 417,422 ****
--- 417,440 ----
        if( SQL_SUCCESS != ret )
          goto exit;
      }
+ 
+     pval = hv_fetch( attrh, "db2_application_name", 20, 0 );
+     if(pval && SvOK(*pval)) {
+         STRLEN vl;
+ 	SQLPOINTER app_name;
+ 	SQLINTEGER app_name_len;
+ 
+         app_name = (SQLPOINTER)SvPV(*pval, vl);
+         app_name_len = (SQLINTEGER)vl;
+ 
+ 	ret = SQLSetConnectAttr(imp_dbh->hdbc,
+ 				SQL_ATTR_INFO_PROGRAMNAME,
+ 				app_name, 
+ 				app_name_len);
+ 	ret = check_error( dbh, ret, "Set application name failed" );
+ 	if( SQL_SUCCESS != ret )
+ 	    goto exit;
+     }
    }
  
    /* If the string contains a =, use SQLDriverConnect */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.