Not sure why your script fails from time to time, but I think there are
better ways to detect "old" transactions. I use the following to monitor
old transactions and also get some information about them:
select
x.agent_id,
substr(z.corr_TOKEN,1,12) as ip,
cast(y.stmt_text as varchar(800)) as stmt,
x.locks_held,
x.APPL_IDLE_TIME,
x.UOW_START_TIME
from sysibmadm.SNAPAPPL x
join sysibmadm.snapstmt y
on x.agent_id = y.agent_id
join sysibmadm.SNAPAPPL_INFO z
on x.agent_id = z.agent_id
where UOW_STOP_TIME is null
and TIMESTAMPDIFF(4,CHAR(current_timestamp - UOW_START_TIME)) > 2
order by UOW_START_TIME
This will detect transactions older than 2 minutes (4 is minutes in
timestampdiff, exchange for other value as needed)
If you are only interested in whether there exists transactions or not
you could do something like:
db2 connect to $DBNAME
db2 "select 1 from sysibmadm.SNAPAPPL where UOW_STOP_TIME is null and
TIMESTAMPDIFF(8,CHAR(current_timestamp - UOW_START_TIME)) > 24" > /dev/null
if [ $? -ne 1 ]; then
# rows found, i.e. transactions exist
echo "Idle application $AP has open transaction exceeding 24 hours"
| mail -s "Idle application
s on $DBNAME" -r "$EMFROM" "$EMAILTO"
fi
db2 connect reset
$? from db2 is bit encoded as:
0 success
1 no rows found
2 warning such as creating a primary key reused an existing index
4 db2 error
8 os error
So you might want to handle additional return values such as 3.
Everything >= 4 means that something is really wrong so you might want
to send an error alert for those cases as well
You might also want to have a look at the newer GET_MON_... functions
Just some thoughts that might give you some ideas.
Cheers
/Lennart