Wednesday 21 November 2012

Using wsadmin to script the creation of a JDBC data source - using input parameters


In this article, I explain how I was able to create a wsadmin script, using the Jython language, to automate the creation of a JDBC data source, to allow WebSphere Application Server 8 to connect to DB2 UDB 9.7. I also highlight an exception that I kept on seeing ( for hours and hours and hours ) and how I resolved it ....

The requirement was to create a script that would be called from a Bash shell script, with input parameters, and would then create a data source, and the associated J2C alias and mapping.

In essence, it looked like this: -

...
cellname=sys.argv[0]
databaseName=sys.argv[1]
driverType=sys.argv[2]
serverName=sys.argv[3]
portNumber=sys.argv[4]

provider_id=AdminConfig.getid('/Cell:E1DSCell/JDBCProvider:DB2 XA provider/')

dsid = AdminTask.createDatasource('provider_id, '[-name [Team Server Datasource] -jndiName jdbc/ilogDataSource -dataStoreHelperClassName com.ibm.websphere.rsadapter.DB2DataStoreHelper -componentManagedAuthenticationAlias RES_db2conn -containerManagedPersistence false -xaRecoveryAuthAlias RES_db2conn -configureResourceProperties [[databaseName java.lang.String ' + str(databaseName) + '] [driverType java.lang.Integer ' + str(driverType) + '] [serverName java.lang.String ' + str(serverName) + '] [portNumber java.lang.Integer ' + portNumber + ']]') 

AdminConfig.create('MappingModule', dsid , '[[authDataAlias RES_db2conn [mappingConfigAlias ""]]')

AdminConfig.save()
...

However, when I executed it, I was seeing: -

$ /opt/IBM/WebSphere/AppServer80/profiles/E1DSDMNODE/bin/wsadmin.sh -lang jython -user wasadmin -password passw0rd -f ~/foobar.py E1DSCELL RESDB 4 rhel6.uk.ibm.com 50000

WASX7209I: Connected to process "dmgr" on node E1DSDMNODENode using SOAP connector;  The type of process is: DeploymentManager
WASX7303I: The following options are passed to the scripting environment and are available as arguments that are stored in the argv variable: "[E1DSCELL, RESDB, 4, rhel6.uk.ibm.com, 50000]"
WASX7017E: Exception received while running file "/home/wasadmin/foobar.py"; exception information: java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: WASX7122E: Expected "]"  not found. 
[-name [Team Server Datasource] -jndiName jdbc/ilogDataSource -dataStoreHelperClassName com.ibm.websphere.rsadapter.DB2DataStoreHelper -componentManagedAuthenticationAlias RES_db2conn -containerManagedPersistence false -xaRecoveryAuthAlias RES_db2conn -configureResourceProperties [[databaseName java.lang.String RESDB] [driverType java.lang.Integer 4] [serverName java.lang.String rhel6.uk.ibm.com] [portNumber java.lang.Integer 50000]]
                           ^

WASX7341W: No "save" was performed before the interactive scripting session exited; configuration changes will not be saved.

It took me AGES to realise that the exception was actually coming from the AdminConfig.create() command: -

AdminConfig.create('MappingModule', dsid , '[[authDataAlias RES_db2conn [mappingConfigAlias ""]]')

rather than the the AdminTask.createDatasource() command, and that it was simply a syntax error on my part - I should have written: -

AdminConfig.create('MappingModule', dsid , '[[authDataAlias RES_db2conn] [mappingConfigAlias ""]]')

Obviously, I should've broken the script down into separate commands, but I got fixated on the AdminTask command, and spent hours hacking around with that :-)

For the record, I didn't actually NEED to use the str() command, as the input parameters were already strings - I've since changed the script to this: -

dsid = AdminTask.createDatasource(provider_id, '[-name [Team Server Datasource] -jndiName jdbc/ilogDataSource -dataStoreHelperClassName com.ibm.websphere.rsadapter.DB2DataStoreHelper -componentManagedAuthenticationAlias RES_db2conn -containerManagedPersistence false -xaRecoveryAuthAlias RES_db2conn -configureResourceProperties [[databaseName java.lang.String ' + databaseName + '] [driverType java.lang.Integer ' + driverType + '] [serverName java.lang.String ' + serverName + '] [portNumber java.lang.Integer ' + portNumber + ']]') 

The moral of the story ? If you get exceptions from your Jython script, in the words of MC Hammer, BREAK IT DOWN :-)

Ah well, it's life up here on the learning curve, every day is a school day.

1 comment:

Ashrith said...
This comment has been removed by the author.

Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...