Wednesday, April 23, 2014

Creating the Gridlink data source through WLST script

Creating the Gridlink data source through WLST script

The below WLST script will help as to create a GridLink datasource in weblogic server.

GridLinkDataSource.properties

dbURL=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=dbhost1)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=dbhost2)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=SERVICENAME)))
connectionpool.test.query=SQL SELECT * FROM DUAL
connectionpool.driver.class=oracle.jdbc.OracleDriver
connectionpool.username=SOA_EAIOWNER
connectionpool.password=orasoa11g
connectionpool.initCapacity=10
connectionpool.maxCapacity=60
datasource.name=EAISOAMetadataSource
datasource.jndi.name=eai/ds/EAISOAMetadataSource
datasource.target=Servers/AdminServer

domain.AdminIP=localhost
domain.AdminPort=7201

domain.AdminPasswd=Albin123!

GridLinkDatasourceCreation.py

from java.io import FileInputStream

def createGridLinkJDBCResources(configProps):
   edit()
   startEdit()
       
   server='AdminServer'
   cd("Servers/"+server)
   target=cmo
   cd("../..")
   print '========================================='
   print 'Creating GridLink DataSource....'
   print '========================================='
   dsTestQuery=configProps.get("connectionpool.test.query")
   dsDriverName=configProps.get("connectionpool.driver.class")
 
           
   cd('/')
   dbURL= configProps.get("dbURL")         
   dsUserName = configProps.get("connectionpool.username")
   dsPassword = configProps.get("connectionpool.password")
   initCapacity = configProps.get("connectionpool.initCapacity")
   maxCapacity = configProps.get("connectionpool.maxCapacity")
   dsName = configProps.get("datasource.name")
   jndiname = configProps.get("datasource.jndi.name")
   datasourceTargets = configProps.get("datasource.target").split(",")

   print 'dsUserName',dsUserName
   print 'dsPassword',dsPassword
   print 'initCapacity',initCapacity
   print 'maxCapacity',maxCapacity
   print 'dsName',dsName
   print 'jndiname',jndiname
   print 'datasourceTargets',datasourceTargets

   print 'Creating DataSource: ',dsName,' ....'
 
   myResourceName = dsName 
   jdbcSystemResource = create(myResourceName,"JDBCSystemResource")   
   myFile = jdbcSystemResource.getDescriptorFileName()   
   jdbcResource = jdbcSystemResource.getJDBCResource()   
   jdbcResource.setName(myResourceName) 
 
 

   # Create the DataSource Params   
   dpBean = jdbcResource.getJDBCDataSourceParams()   
   myName=jndiname
   dpBean.setJNDINames([myName])
   dpBean.setGlobalTransactionsProtocol('TwoPhaseCommit')
   
   # Create the Driver Params   
   drBean = jdbcResource.getJDBCDriverParams()   
   drBean.setPassword(dsPassword)   
   drBean.setUrl(dbURL)   
   drBean.setDriverName(dsDriverName)

   #Create the Oracle params
   orapr=jdbcResource.getJDBCOracleParams()
   orapr.setFanEnabled(true)
   orapr.setOnsNodeList('node1:6200,node2:6200')
   propBean = drBean.getProperties()
   driverProps = Properties()   
   driverProps.setProperty("user",dsUserName)
   e = driverProps.propertyNames()
   while e.hasMoreElements() :           
propName = e.nextElement()           
myBean = propBean.createProperty(propName)         
myBean.setValue(driverProps.getProperty(propName)) 
 
# Create the ConnectionPool Params   
ppBean = jdbcResource.getJDBCConnectionPoolParams()   
ppBean.setInitialCapacity(int(initCapacity))   
ppBean.setMaxCapacity(int(maxCapacity))   
ppBean.setTestConnectionsOnReserve(true)   
ppBean.setTestTableName('SQL SELECT 1 FROM DUAL')

xaParams = jdbcResource.getJDBCXAParams()   
xaParams.setKeepXaConnTillTxComplete(1) 

   # Add Target
   for datasourceTarget in datasourceTargets:
print 'DataSourceTargets',datasourceTargets
print 'DataSourceTarget',datasourceTarget
if datasourceTarget=='':
print ''
else:
jdbcSystemResource.addTarget(getMBean(datasourceTarget))   
 

   print 'DataSource: ',dsName,', has been created Successfully !!!'
   print '=========================================' 
       
 
   save()
   activate() 


 
def main():
    propInputStream1 = FileInputStream("GridLinkDataSource.properties")
    configProps = util.Properties()
    configProps.load(propInputStream1)
 
    adminURL='t3://'+configProps.get('domain.AdminIP')+':'+configProps.get('domain.AdminPort')
    adminUserName='weblogic'
    adminPassword=configProps.get("domain.AdminPasswd")
    connect(adminUserName, adminPassword, adminURL)
 
    createGridLinkJDBCResources(configProps);
 
    print 'Successfully created JDBC resources for SOACoreDomain'

    disconnect()

     
main()


Change the values accordingly in the property file and execute the  $WLSHOME/common/bin/wlst.sh GridLinkDatasourceCreation.py


After the successful execution of the script login to console and verify the Gridlink datasource created.


Download GridLinkDatasourceCreation.py


10 comments:

  1. Hi Albin;
    Nice blogs and very good contents. I have never used silent configuration using wlst. first time i saw this and its really nice. I tried this script but getting issue on line number 83/84-

    [oracle@retdb ~]$ java weblogic.WLST GridLinkDatasourceCreation.py GridLinkDataSource.properties

    Initializing WebLogic Scripting Tool (WLST) ...

    Welcome to WebLogic Server Administration Scripting Shell

    Type help() for help on available commands

    Problem invoking WLST - Traceback (innermost last):
    (no code object) at line 0
    File "/home/oracle/GridLinkDatasourceCreation.py", line 83
    print 'DataSourceTargets',datasourceTargets
    ^
    SyntaxError: invalid syntax

    ***********************************************************

    81 # Add Target
    82 for datasourceTarget in datasourceTargets:
    83 print 'DataSourceTargets',datasourceTargets
    84 print 'DataSourceTarget',datasourceTarget
    85 if datasourceTarget=='':
    86 print ''
    87 else:
    88 jdbcSystemResource.addTarget(getMBean(datasourceTarget))
    89
    90
    91 print 'DataSource: ',dsName,', has been created Successfully !!!'
    92 print '========================================='
    93


    *****************************************************************************

    I am very new and really dont understand more in wlst scripting. need your help to teat the same.

    Please do the needful.



    ReplyDelete
    Replies
    1. it is working when you edit GridLinkDatasourceCreation.py at the below place


      # Add Target
      for datasourceTarget in datasourceTargets:
      print 'DataSourceTargets',datasourceTargets
      print 'DataSourceTarget',datasourceTarget
      if datasourceTarget=='':
      print ''
      else:
      jdbcSystemResource.addTarget(getMBean(datasourceTarget))


      FYI Its just a few spaces that was causing the run time error.

      Delete
  2. Now I can create the Gridlink data source through WLST script thank you very much for precious experience. Find for more in here writing a white paper
    thanks a lot!

    ReplyDelete
  3. i need to pass dynamic values for intial capacity and maximun capacity

    ReplyDelete
  4. Type help() for help on available commands

    Problem invoking WLST - Traceback (innermost last):
    (no code object) at line 0
    File "/opt/soa/TA/SCRIPTS/Grid/GridLinkDatasourceCreation.py", line 82
    print 'DataSourceTargets',datasourceTargets
    ^
    SyntaxError: invalid syntax

    Getting above Error .can anyone suggest here please below is code
    # Add Target
    for datasourceTarget in datasourceTargets:
    print 'DataSourceTargets',datasourceTargets
    print 'DataSourceTarget',datasourceTarget
    if datasourceTarget=='':
    print ''
    else:
    jdbcSystemResource.addTarget(getMBean(datasourceTarget))

    ReplyDelete
    Replies
    1. Attached the working GridLinkDatasourceCreation.py script

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. how we can parametried it for multiple grid dataosurces ..
    Please help me on this ..

    ReplyDelete
    Replies
    1. You can implement the approach followed here - https://www.albinsblog.com/2012/09/modifying-targets-of-jdbc-datasources.html

      Delete
  7. Hi,

    Mai i know what value in the script is deciding it should be generic or grid

    Thanks
    Vipin Alakkadan

    ReplyDelete