Modifying the targets of JDBC datasources through WLST script in weblogic server
This tutorial explains the details on WLST script to change or add the targets for JDBC data sources in weblogic server.
The targeting can be enabled through weblogic admin console but the script helps us to automate the server configurations and reduce the manual effort if multiple data-sources need to be configured.

The script will target the server MS3 and the Cluser1 to the data source with name “CRM6EAIReference”
Define JDBCProperties.properties with required configurations(sample script to show the configuration for multiple data-sources)
domain1.total.DS=2
domain1.prefix.1=om domain1.om.datasource.name.1=CRM6EAIReference domain1.om.datasource.target.1=Clusters/Cluster1,Servers/MS3
domain1.prefix.2=soa domain1.soa.datasource.name.2=CRM6EAISOAMetadataSource domain1.soa.datasource.target.2=Clusters/SOACluster
Before executing the script change the properties as required.If the target is a Server then it should be added like Servers/ServerName and if it is a cluster then it should be defined as Clusters/ClusterName. Configure domain1.total.DS with total number of data sources need to be configured.
Let us now define a WLST script file to change the targets using the property file defined above.
import re from java.io import FileInputStream def UntargetTargetJDBCResources(): edit() startEdit() propInputStream = FileInputStream('JDBCProperties.properties') configProps = Properties() configProps.load(propInputStream) totalDataSource_to_untargetTarget=configProps.get("domain1.total.DS") server='AdminServer' cd("Servers/"+server) i=1 while (i <= int(totalDataSource_to_untargetTarget)) : prefix = configProps.get("domain1.prefix."+ str(i)) dsName = configProps.get("domain1."+prefix+".datasource.name."+ str(i)) datasourceTargets = re.split(",",configProps.get("domain1."+prefix+".datasource.target."+ str(i))) targetArray=[] for datasourceTarget in datasourceTargets: print 'DataSourceTargets',datasourceTargets print 'DataSourceTarget',datasourceTarget if datasourceTarget=='': print '' else: cd ('/JDBCSystemResources/'+dsName) set('Targets',jarray.array([], ObjectName)) target=datasourceTarget[datasourceTarget.index("/")+1:len(datasourceTarget)] if datasourceTarget.startswith('Cluster'): targetArray.append(ObjectName('com.bea:Name='+target+',Type=Cluster')) elif datasourceTarget.startswith('Server'): targetArray.append(ObjectName('com.bea:Name='+target+',Type=Server')) print 'Targets: ',targetArray set('Targets',jarray.array(targetArray, ObjectName)) print 'DataSource: ',dsName,',Target has been updated Successfully !!!' print '=========================================' i = i + 1 print '=========================================' save() activate()
def main(): adminURL='t3://localhost:7001' adminUserName='weblogic' adminPassword='weblogic1' connect(adminUserName, adminPassword, adminURL) UntargetTargetJDBCResources(); print 'Successfully Modified the JDBC resources' disconnect() main()
Script -
The above script update the JDBC data source targets based on the configurations enabled in the property file.
Execute the script — <<Oracle_Home>>\oracle_common\common\bin\wlst.cmd ModifyJDBCResourcesTargets.py

The JDBC data source targets changed now with the enabled configurations,the WLST script can be used to automate the server configurations and reduce the manual effort need for configurations.