Wednesday 27 January 2010

Test connection on each node for cell scope datasource

I am sure pretty much every WAS administrator has used the "test connection" button in the WAS console to prove a JDBC datasource has been set up correctly.

Although not a problem, something that always got me and didn't seem to be as good as it could be, is the fact that if you work in a large scale enironment you may well end up setting the datasource at a cell level as this would cut down on the time it takes to set up a datasource on each node or appserver and also redude the likelihood of something being mis typed. Hoever, if you do this and then run test connection, the connection is just from the dmgr as you can see by lookiig in the dmgr logs.

So what happens if you have 10, 20 or more nodes and you want to make sure that all of them can connect correctly to the database. You could telnet from each box to the DB server on the correct port, but that just shows network connectivity rather than a full databsee connection.

I assumed this could be done in a jython script but it took me a day or 2 to figure this out. If I connected to the nodeagent in wsadmin, I couldn't get the config details of the datasources as these are accessed from a wsadmin session connected to the dmgr. But running a test connection when in a wsadmin session connected to the dmgr just does the same as the "test connection" through the admin console.

In the end, I managed to write a simple unix script, which does the following:

1. Open a wsadmin session to the dmgr to get the datasource ids and write these to a file, passing in the name of the cell

2. open a wsadmin session to each nodeagent, read the datasource id's from the file, then run a test connection.


This is the basic unix script:

###########################################################################
CELL=epwsdr21Cell
NODES="epwsdr21 epwsdr22 epwsdr23 epwsdr24 epbtdr21"
PORT=8878

echo "Running connection from each node to the datasources in WAS"

echo "Connecting to dmgr through wsadmin....."


# Connect to the dmgr through wsadmin - pass in the name of the cell - and run scropt dsconnect.py

/usr/was6/WebSphere/AppServer/bin/wsadmin.sh -lang jython -f ./jython/dsconnect.py $CELL


echo "Connecting to each nodeagent to run test connections....."



for node in $NODES;do

echo "Connecting to ${node} on port ${PORT} through wsadmin"

# Connect to each nodeagent in my list of nodes above - and run script dsconnect2.py

/usr/was6/WebSphere/AppServer/bin/wsadmin.sh -lang jython -conntype SOAP -host $node -port $PORT -f ./jython/dsconnect2.py $node

done

echo 'Complete '


#############################################################################


And here is what is in the first jython script - dsconnect.py

# Jython script to get the datasource id's once connected to the dmgr through wsadmin


import sys
print ' '
print "Getting datasources for cell " + sys.argv[0]

# First build the cell name we are interested in fromm the cell name passed from the main script
constructcell ="/Cell:" + sys.argv[0] + "/"

# get the cell id

cellid = AdminConfig.getid( constructcell )

# Get the datasource id's

print 'Datasources found are listed below:'

# In this instance I am after v4 datasources for v5 datasources use "dsid = AdminConfig.list("DataSource", cellid).splitlines()"

dsid = AdminConfig.list("WAS40DataSource", cellid).splitlines()


print dsid

# Now open a tmp file and write the list of dsid's to tfe file - this will be a string rather than a jython list

f=open('/tmp/dsconnect.out','w')

s=str(dsid)

f.write(s)

f.close()

##############################################################################

So the list of ids is written to /tmp/dsconnect.out, now the second script is called for each nodeagent I want to connect to and then run a test connection. The list that has been put into a file will be seen as a jython string rather than a list which is why I use the eval statment so it goes back into string format


# Jython script to run a test connection on a list of datasources

import sys

#

# Open temp file to get string of datasource ids and assign to a jython list


f=open('/tmp/dsconnect.out')
test=f.read()
dsids=eval(test)
f.close()


for ds in dsids:

print ' '

print 'Testing connection from ' + sys.argv[0] + ' to ' + ds

try:

outp=AdminControl.testConnection(ds)

except:

print 'Error connecting to datasource'

print outp
else:

print outp

##############################################################################


The jython script doesn't show up as well as I would have thought on here. If you are after the scripts then drop me a mail info@janglestrategies.co.uk and I'll email them over

3 comments:

  1. Hi;

    When the nodeagent is closed and when we try to test connection then the dmgr says there is failure.

    Also when some nodes have database connection problems because of IP address limitations than we can see that on some nodes the connection fails when we do test connection on dmgr.

    So is the information above wrong or I 'm missing something?

    Thanks

    ReplyDelete
  2. Hi Ibrahim

    It depends what scope your datasources are defined. If they are set at node level then "test connection" will try to connect to each node agent and run a test from each node.

    The reason I wrote this script is that at the site I was working, the datasources were at cell level and in that case a test connection only connects from the dmgr and not from each node.

    Also, if you had several servers on each node but the datasource was at node level, then a test connection would be done from the node agents rather than each individual server in that case you may wish to amend this script to test from each app server.

    John

    ReplyDelete
  3. Hi John;

    Thanks for the explanation.

    I got the details.

    My test is on a datasource which is described in a cluster level,but your test is on a cell level.

    So you are right.

    ReplyDelete