Search code examples
javadb2datasourcetomcat9db2-luw

setClientProgramName in DataSource


In tomcat9 there is a setting in context.xml:

<Resource name="jdbc/db2xx"         auth="Container"    
    type="javax.sql.DataSource"     driverClassName="com.ibm.db2.jcc.DB2Driver" 
    maxTotal="100"                  maxIdle="30"      
    maxWaitMillis="-1"              username="xx" password="xx"         
    url="jdbc:db2://xxxx:xxx/dbname;" />

and in Java is :

    try {
        Class.forName("com.ibm.db2.jcc.DB2SimpleDataSource");
        Context initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env");
        com.ibm.db2.jcc.DB2SimpleDataSource ds = (com.ibm.db2.jcc.DB2SimpleDataSource) envContext.lookup("jdbc/db2xx");
        ds.setClientProgramName("MyApplication");
        conn = ds.getConnection();
    }

But I get error :

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp2.BasicDataSource cannot be cast to com.ibm.db2.jcc.DB2SimpleDataSource

Do I have to change

org.apache.tomcat.dbcp.dbcp2.BasicDataSource

where ? The main point is

ds.setClientProgramName("MyApplication");

because I would like to see

MyApplication in APPLICATION_NAME

when I run

SELECT APPLICATION_NAME FROM TABLE(MON_GET_CONNECTION(CAST(NULL AS BIGINT),-2))

or is there any other way ?

If I am using javax.sql.DataSource program works, but I can not use setClientProgramName.

In maven pom :

    <dependency>
        <groupId>com.ibm.db2</groupId>
        <artifactId>jcc</artifactId>
        <version>11.5.7.0</version>
    </dependency>

Solution

  • Client info properties support by the IBM Data Server Driver for JDBC and SQLJ.

    ApplicationName
    ClientAccountingInformation
    ClientHostname
    ClientUser
    

    These properties mentioned at the link above can't be set with URL.
    They can be set with the JDBC 4 method (use db2jcc4.jar, not db2jcc.jar which is JDBC 3 implementation) setClientInfo only like below:

    conn = ds.getConnection();
    conn.setClientInfo("ApplicationName", "MyApplication");