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
##############################################################################
No comments:
Post a Comment