Friday, September 7, 2012

Modifying the targets of JDBC datasources through WLST script in weblogic server

Modifying the targets of JDBC datasources through WLST script in weblogic server

This post will explain how to Modify the targets of JDBC datasources through WLST script in weblogic server


We had a scenario to remove the existing targets and add new targets to multiple JDBC databases in a Single and Clustered environments.

Thought of sharing the same because it may help someone looking for the same

JDBCProperties.properties


domain1.total.DS=2

domain1.prefix.1=om
domain1.om.datasource.name.1=CRM6EAIReference
domain1.om.datasource.target.1=Clusters/SOACluster,Servers/AdminServer

domain1.prefix.2=soa
domain1.soa.datasource.name.2=CRM6EAISOAMetadataSource
domain1.soa.datasource.target.2=Clusters/SOACluster

Before executing the script change the properties accordingly.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.

UntargetTargetJDBCResources.py


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)))
   
   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)]
print target
if datasourceTarget.startswith('Cluster'):
set('Targets',jarray.array([ObjectName('com.bea:Name='+target+',Type=Cluster')], ObjectName))
elif datasourceTarget.startswith('Server'):
set('Targets',jarray.array([ObjectName('com.bea:Name='+target+',Type=Server')], ObjectName))
 
print 'DataSource: ',dsName,',Target has been updated Successfully !!!'
print '========================================='   
   i = i + 1

    print '========================================='
    save()
    activate() 


 
def main():
        adminURL='t3://localhost:7201'
        adminUserName='weblogic'
        adminPassword='Test1234'
        connect(adminUserName, adminPassword, adminURL)
        UntargetTargetJDBCResources();
        print 'Successfully Modified the JDBC resources'

        disconnect()
main()

Execute the script - wlst.sh UntargetTargetJDBCResources.py

DownloadUntargetTargetJDBCResources.py
  


4 comments:

  1. Hi
    How to get the cluster name in the weblogic domain using WLST scripts?
    the requirement is to target the datasource on the available cluster in the domain.

    Thanks in avance

    ReplyDelete
  2. How to target a Datasource to Multiple cluster using WLST

    ReplyDelete
    Replies
    1. The same script can be used - Add the cluster names as a comma separated value to the required datasource target e.g
      domain1.om.datasource.target.1=Clusters/SOACluster,Clusters/OSBCluster

      Delete
  3. Hi,

    Thanks for the wonderful script.
    I am using this script but i am facing an issue where i can only target to one managed server or a cluster but not two things. Can you please help me on this ?

    Thanks,
    Kumar

    ReplyDelete