• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

DB2 LIST APPLICATIONS and FORCE APPLICATION

Hi all,

I need to perform the following:

1. retrieve LIST APPLICATIONS list
2. loop through it and execute FORCE APPLICATION on all with a certain name(s)

I don't care if the solution is a Wshell script, C/C++ application or in C#,
I just need this done.

Any help is appreciated.
0
Mi-Jack
Asked:
Mi-Jack
  • 5
  • 4
  • 3
1 Solution
 
momi_sabagCommented:
i don't understand the question
it can be done in C
it can be done with a shell script

what exactly do you want?
you want someone to give you the code?
0
 
Mi-JackAuthor Commented:
A code snippet would be nice, yes.
My preference is C/C++
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
Kent OlsenData Warehouse Architect / DBACommented:

Hi Mi-Jack,

You can always use popen() to run the DB2 command and read the output.

...
#include <io.h>
...
  char Buffer[200];
  FILE *AppList = popen ("DB2 -x \"list applications\"");

  while (AppList)
  {
    if (fgets (Buffer, 200, AppList))
    {
    // Parse the line for the application handle (3rd object, approx 25th char position)
    // send the force application command to that handle
    }
    else
    {
      fclose (AppList);
      AppList = NULL;
    }
  }

Will something like that work for you?


Kent


 
0
 
Mi-JackAuthor Commented:
>>momi_sabad
I am lost in IBM's documentation.
The second example will give me the list, but how do I force them to disconnect?

Hi Kent,
This will work fine. Ho w do I force disconnect?

Sorry for being dense, I did research, but couldn't find anything clear enough.
0
 
momi_sabagCommented:
sqlefrce API - Force users and applications off the system
Forces local or remote users or applications off the system to allow for maintenance on a server. Attention: If an operation that cannot be interrupted (a database restore, for example) is forced, the operation must be successfully re-executed before the database becomes available.

Scope
This API affects all database partition servers that are listed in the db2nodes.cfg file.

In a partitioned database environment, this API does not have to be issued from the coordinator partition of the application being forced. This API can be issued from any database partition server in the partitioned database environment.

Authorization
One of the following:
sysadm
sysctrl
sysmaint
Required connection
Instance. To force users off a remote server, it is necessary to first attach to that server. If no attachment exists, this API is executed locally.

API include file
sqlenv.h
API and data structure syntax
SQL_API_RC SQL_API_FN
  sqlefrce (
        sqlint32 NumAgentIds,
        sqluint32 * pAgentIds,
        unsigned short ForceMode,
        struct sqlca * pSqlca);

SQL_API_RC SQL_API_FN
  sqlgfrce (
        struct sqlca * pSqlca,
        unsigned short ForceMode,
        sqluint32 * pAgentIds,
        sqlint32 NumAgentIds);
sqlefrce API parameters
NumAgentIds
Input. An integer representing the total number of users to be terminated. This number should be the same as the number of elements in the array of agent IDs.
If this parameter is set to SQL_ALL_USERS (defined in sqlenv), all applications with either database connections or instance attachments are forced. If it is set to zero, an error is returned.

pAgentIds
Input. Pointer to an array of unsigned long integers. Each entry describes the agent ID of the corresponding database user.
ForceMode
Input. An integer specifying the operating mode of the sqlefrce API. Only the asynchronous mode is supported. This means that the API does not wait until all specified users are terminated before returning. It returns as soon as the API has been issued successfully, or an error occurs. As a result, there may be a short interval between the time the force application call completes and the specified users have been terminated.
This parameter must be set to SQL_ASYNCH (defined in sqlenv).

pSqlca
Output. A pointer to the sqlca structure.
Usage notes
The database manager remains active so that subsequent database manager operations can be handled without the need for db2start.

To preserve database integrity, only users who are idling or executing interruptible database operations can be forced off.

After a force command has been issued, the database will still accept requests to connect. Additional forces may be required to completely force all users off. The database system monitor functions are used to gather the agent IDs of the users to be forced.

When the force mode is set to SQL_ASYNCH (the only value permitted), the API immediately returns to the calling application.

Minimal validation is performed on the array of agent IDs to be forced. The user must ensure that the pointer points to an array containing the total number of elements specified. If NumAgentIds is set to SQL_ALL_USERS, the array is ignored.

When a user is forced off, a unit of work rollback is performed to ensure database consistency.

All users that can be forced will be forced. If one or more specified agent IDs cannot be found, sqlcode in the sqlca structure is set to 1230. An agent ID may not be found, for instance, if the user signs off between the time an agent ID is collected and sqlefrce is called. The user that calls this API is never forced off.

Agent IDs are recycled, and are used to force applications some time after being gathered by the database system monitor. When a user signs off, therefore, another user may sign on and acquire the same agent ID through this recycling process, with the result that the wrong user may be forced.





i would also search the samples folder on your hard drive
they usually have good samples there
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Mi-Jack,

Send the FORCE APPLICATION command.

//  I'm assuming that you've extracted the Application Handle as an integer.  :)

  char DB2Command[200];

  sprintf (DB2Command, "DB2 FORCE APPLICATION %d", AppHandle);
  system (DB2Command);

Before the call to popen() you'll also need to establish a database connection.

  system ("DB2 connect to MYDATABASE");



Kent
0
 
Mi-JackAuthor Commented:
This is the second time I come across this suggestion - use system()
But how do I use it?
db2cmd opens its own window, you can't execute anything with system().
running a db2 command in a command prompt, on the other hand, will return an error that environment not initialized.
So how do you connect the 2 (cmd and db2cmd)?
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Mi,

The DB2 CLI (Command Line Interface) is just a batch program.  If a DB2 command is passed to it on the command line, a window should not be opened.  If there is no DB2 command on the command line then the CLI will open a window and prompt for a command.  You can also pass a file name to the CLI and it will execute every line in the file.

For the CLI to run correctly, the environment must have run the DB2 prolog which will set up the PATH statement so that the command and libraries are available.  The location of the file is dependent upon your installation parameters.

Path=
C:\Program Files\IBM\SQLLIB\BIN\..\db2tss\bin;
C:\PROGRA~1\IBM\SQLLIB\BIN\..\db2tss\bin;
C:\Program Files\IBM\SQLLIB\BIN;
C:\Program Files\IBM\SQLLIB\FUNCTION;

If you've taken the default location for installation, the folders listed above should be the first 4 items on the PATH statement.


Kent
0
 
Mi-JackAuthor Commented:
popen seems to work with some command-line arguments:

db2cmd -c -w -i db2 -tv

I'll take it from here,
Thank you.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi Mi,

The -c option (autocommit) should be the default, as should the -w (show warnings) option.


Kent
0
 
Mi-JackAuthor Commented:
What really does the tric is the -tv
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now