Scripting Websphere 6.1 configurations, Part 4

Creating Datasources

In the previous post we created the datasource “Provider” which consists of telling the container where the Jar file containing your databases drivers are located. With Weblogic, you might do something as simple as adding the Jar to your server classpath when you start it up. Websphere makes it a little more complicated, but I must admin it is a little more enterprisey. You can configure the same provider jar to be a XA enabled datasource, and non-XA enabled for example.

Now it’s hopefully not too late for me to mention that you should learn all the J2C* configuration objects. I will put up a references page at the end of the series.

The next step is needed to actually use the datasource and consists of configuring the datasource and giving it a JNDI name. We put all the datasource configuration methods in to a seperate class.

########################################################################################################
# Utility class Configure a DataSource. This class contains all the createDataSourceXXX methods for all
# of our database connections. For this example I am only showing one.
########################################################################################################

class DataSourceConfig:
def __init__(self, wasDef):
self.wasDef = wasDef;
self.j2cf = J2CFactoryConfig(wasDef);

#####################################################################################################
# Method that creates a JDBC DB2 Datasource
# dsprops contains the datasource name (Which is also used to create the JNDI name) along with
# “serverName”, dsprops.server
# “portNumber”, dsprops.port
# “databaseName”, dsprops.database
# “user”, dsprops.username
# “password”, dsprops.password
#####################################################################################################
def createDataSourceDB2(self, dsprops):
log = Log(“DataSourceConfig.createDataSourceDB2”);

prov = self.getJDBCProviderDB2();
if prov == None:
raise AssertionError;

dstemplates = ListAsString(AdminConfig.listTemplates(WASConfigType.DATASOURCE, WASConfigName.DATASOURCE_DB2_UNIVERSAL)).asList();
if dstemplates == None or len(dstemplates) != 1:
raise Exception, “Too many DB2 DataSource Templates to choose from, expected only 1”;

params = WASConfigParams();
params.add(“name”, dsprops.dataSourceName);
params.add(“jndiName”, “jdbc/” + dsprops.dataSourceName);
params.add(“description”, “DB2 Universal Driver Datasource”);
params.add(“providerType”, “DB2 Universal JDBC Driver Provider”);
params.add(“authMechanismPreference”, “BASIC_PASSWORD”);
params.add(“authDataAlias”, “”);
params.add(“manageCachedHandles”, “false”);
params.add(“logMissingTransactionContext”, “true”);
params.add(“xaRecoveryAuthAlias”, “(none)”);
params.add(“diagnoseConnectionUsage”, “false”);
params.add(“statementCacheSize”, “10”);
params.add(“datasourceHelperClassname”, “com.ibm.websphere.rsadapter.DB2UniversalDataStoreHelper”);
ds = AdminConfig.createUsingTemplate(WASConfigType.DATASOURCE, prov, params.asList(), dstemplates[0]);
log.val(“DataSource Created”, ds);

self.setDataSourceProperty(ds, “serverName”, dsprops.server);
self.setDataSourceProperty(ds, “portNumber”, dsprops.port, “java.lang.Integer”);
self.setDataSourceProperty(ds, “databaseName”, dsprops.database);
self.setDataSourceProperty(ds, “user”, dsprops.username);
self.setDataSourceProperty(ds, “password”, dsprops.password);
if dsprops.schema != None:
self.setDataSourceProperty(ds, “currentSchema”, dsprops.schema);
if dsprops.driverType != None:
self.setDataSourceProperty(ds, “driverType”, dsprops.driverType);

return ds;

#returns the configId of the specified property of the specified datasource or None.
def getDataSourceProperty(self, ds, propName):
return self.j2cf.getConnectionFactoryProperty(ds, propName);

def setDataSourceProperty(self, ds, propName, propValue, propJavaType=”java.lang.String”):
return self.j2cf.setConnectionFactoryProperty(ds, “DataSource”, propName, propValue, propJavaType);

def getJDBCProviderDB2(self):
return self.wasDef.findConfigId(WASConfigType.JDBC_PROVIDER, WASConfigName.JDBC_PROVIDER_DB2_UNIVERSAL);

########################################################################################################
# Utility class to configure J2C Connection Factories of Resource Adapters.
# A Datasource is essentially like a J2CConnectionFactory and share many features but not always exact.
########################################################################################################

class J2CFactoryConfig:

def __init__(self, wasDef):
self.wasDef = wasDef;

#name: NOT JNDI name, JNDI name is composed as resource/name.
#scope: defaults to SERVER.
def getDataSource(self, name, scope=WASConfigScope.SERVER):
return self.wasDef.findConfigId(WASConfigType.DATASOURCE, name);

#name: NOT JNDI name, JNDI name is composed as resource/name.
#scope: defaults to NODE.
def getConnectionFactory(self, name, scope=WASConfigScope.NODE):
return self.wasDef.findConfigId(WASConfigType.J2C_CONNECTION_FACTORY, name, scope);

#returns the configId of the specified property of the specified Datasource or ConnectionFactory.
def getConnectionFactoryProperty(self, cf, propName):
propset = AdminConfig.showAttribute(cf, “propertySet”);
props = ListAsString(AdminConfig.list(“J2EEResourceProperty”, propset)).asList();
for prop in props:
# !!! very slow
#configPropName = AdminConfig.showAttribute(prop, “name”);
#if configPropName == propName:
# return prop;

prop = prop.lstrip();
if prop.find(propName + “(“) >=0 :
return prop;

return None;

#sets the specified property of the specified Datasource or ConnectionFactory.
#cf : configId of ConnectionFactory or DataSource.
#cfType : String identifying the type of cf – e.g. RARConnectionFactory or DataSource.
#propJavaType: All J2CConnectionFactory/Datasource properties are Java properties and their type must be given.
def setConnectionFactoryProperty(self, cf, cfType, propName, propValue, propJavaType=”java.lang.String”):
log = Log(“set” + cfType + “Property”, propName, propValue);

origProp = self.getConnectionFactoryProperty(cf, propName);
if origProp != None:
AdminConfig.remove(origProp);

propset = AdminConfig.showAttribute(cf, “propertySet”);
params = WASConfigParams();
params.add(“name”, propName);
params.add(“value”, str(propValue));
params.add(“type”, propJavaType);
params.add(“required”, “true”);
p = AdminConfig.create(“J2EEResourceProperty”, propset, params.asList());

log.done();

########################################################################################################
# Utility class to hold the internal definitions of objects.
# Note: This class is greatly expanded in real life, for this example I removed all the messaging and security stuff
########################################################################################################

class WASConfigType:
CELL = “Cell”;
NODE = “Node”
SERVER = “Server”;
CONN_POOL = “ConnectionPool”;
JDBC_PROVIDER = “JDBCProvider”;
DATASOURCE = “DataSource”;
WEB_CONTAINER = “WebContainer”;

2 thoughts on “Scripting Websphere 6.1 configurations, Part 4

Comments are closed.