Pages

Tuesday, April 13, 2010

Resetting Weblogic datasources with ANT

When you are working with Weblogic JDBC Datasources for example in a Web Application, EJB or in an AQ or Database resource adapter then there is a possibility that the database sessions are in an invalid state when you change something like a package or object type in the database. This can be solved by restarting all the managed servers, reset all the Datasources in the Weblogic console or use this ANT or WLST script. This script is an ANT file where I use the WLST ANT task to fire some wlst and phyton commandos.
This ANT target works fast and can be easily integrated in your deployed script.

the reset.xml ANT build file.
<?xml version="1.0" encoding="iso-8859-1"?>
<project name="resetAllDatasources" default="resetJDBC">

  <target name="resetJDBC">
   <property name="admin.User" value="weblogic"/>
   <property name="admin.Password" value="weblogic1"/>
   <property name="admin.Url" value="localhost"/>
   <property name="admin.Port" value="7101"/>
   <property name="admin.ServerTarget" value="DefaultServer"/>

   <property name="datasources" value="hrDS,scottDS"/>

    <wlResetDatasource adminUser="${admin.User}" 
            adminPassword="${admin.Password}" 
            adminUrl="${admin.Url}" 
            adminPort="${admin.Port}" 
            serverTarget="${admin.ServerTarget}" 
            datasourceNames="${datasources}"/>

  </target>

 <macrodef name="wlResetDatasource">
  <attribute name="adminUser"/>
  <attribute name="adminPassword"/>
  <attribute name="adminUrl"/>
  <attribute name="adminPort"/>
  <attribute name="serverTarget"/>
  <attribute name="datasourceNames"/>
  <sequential>
    <wlst failonerror="true" debug="true" arguments="@{adminUser} @{adminPassword} @{adminUrl} @{adminPort} @{serverTarget} @{datasourceNames}">
      <script>
          adminUser=sys.argv[0]
          adminPassword=sys.argv[1]
          adminUrl="t3://"+sys.argv[2]+":"+sys.argv[3]
          serverTarget=sys.argv[4]
          datasourceNames=String(sys.argv[5]).split(",")
          connect(adminUser,adminPassword,adminUrl)
          print 'all datasource: '+sys.argv[5]
          domainRuntime()
          for datasourceName in datasourceNames:
           print 'resetting datasource: '+datasourceName
           cd('/')
           cd('ServerRuntimes/'+serverTarget+'/JDBCServiceRuntime/'+serverTarget+'/JDBCDataSourceRuntimeMBeans/'+datasourceName)
           objs = jarray.array([], java.lang.Object)
           strs = jarray.array([], java.lang.String)
           invoke('reset', objs, strs)
      </script>
    </wlst>
  </sequential>
 </macrodef>

</project>
And the windows bat file to startup the ANT target. I only need a ANT home and the weblogic.jar
set ORACLE_HOME=C:\oracle\MiddlewareJdev11gR1PS1
set ANT_HOME=%ORACLE_HOME%\jdeveloper\ant
set PATH=%ANT_HOME%\bin;%PATH%
set JAVA_HOME=%ORACLE_HOME%\jdk160_14_R27.6.5-32
set ANT_OPTS=%ANT_OPTS% -XX:MaxPermSize=128m

set CLASSPATH=%CLASSPATH%;%ORACLE_HOME%\wlserver_10.3\server\lib\weblogic.jar

ant -f reset.xml
I also made a WLST script which automatically finds the managed servers in the connected domains and resets the user created datasources ( I skipped the Soa datasource )
domainRuntime()

drs = ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean"); 
domainconfig =  mbs.getAttribute(drs, "DomainConfiguration");
servers = mbs.getAttribute(domainconfig, "Servers"); 
for server in servers:
  serverName = mbs.getAttribute(server,'Name')
  print 'server: '+serverName
  if serverName == "AdminServer":
    print 'server skipped'
  else:
    dsBean = ObjectName('com.bea:ServerRuntime='+serverName+',Name='+serverName+',Location='+serverName+',Type=JDBCServiceRuntime')
    if dsBean is None:
      print 'not found'
    else:
      datasourceObjects = mbs.getAttribute(dsBean, 'JDBCDataSourceRuntimeMBeans')  
      for datasourceObject in datasourceObjects:
        if datasourceObject is None:
          print 'datasource not found'
        else:
          Name = mbs.getAttribute(datasourceObject,'Name')
          x = Name.find("SRA",0,3 )
          if ( Name.find("SOA",0,3 ) == -1 and Name.find("mds",0,3 ) == -1 and Name.find("EDN",0,3 ) == -1 and Name.find("BAM",0,3 ) == -1 and Name.find("Ora",0,3 ) == -1):  
            mbs.invoke(datasourceObject, 'reset',None,None)  
            print 'reset: '+Name

17 comments:

  1. Edwin, thank for your posts which are always very interesting, unfortunately there must be something on your blog (graphics? scripts?) which make the page painfully slow to scroll.... can you see if removing something makes it faster?
    thanks

    ReplyDelete
  2. Hi Pierre

    thanks, is it now better , I removed 4 things.

    Edwin

    ReplyDelete
  3. hi Edwin,
    we use the MDS to hold some endpoints but is there a way to tell weblogic that the MDS cache needs updating? I had a quick look at MDS Bean properties but there was no isDirty setting or anything like that. Is there an easy way to refresh MDS cache of is it a case of restarting the server?

    Regards
    Jason

    ReplyDelete
  4. Hi Jason,

    can you explain in detail what you have done. I know MDS DB supports versioning and I think this mbean notify's everything

    thanks

    ReplyDelete
  5. we have WSDLS in our MDS using your ANT script. These WSDLs contain the runtime endpoints to externals services/composites. Now when we would like to permanately change the endpoint, we do this in MDS by "reloading" the WSDL back into MDS using the ANT script. But we sometimes get a problem in that Weblogic has cached the old endpoint and of course this is wrong. So back to the original question, is there an easy way to refresh it or is it done automatically

    ReplyDelete
  6. Hi Jason,

    you can try to deploy a new version of the wsdl in MDS , without using undeploy. This will make a new version. Hope this will work else you can try to promote this version in MDS.

    thanks

    ReplyDelete
  7. Edwin, thankU for WLST script. It worked perfectly, can u guide me to some material where i can learn WLST.

    ReplyDelete
  8. Edwin,

    Can you suggest or describe please what may cause a DB object to become invalid within a weblogic server (using SOA/bpel 11g)?

    Thanks

    ReplyDelete
    Replies
    1. Hi,

      the datasource has some open connections and these session stores some state information about the database. this is how the Oracle database works, probably for performance.

      thanks

      Delete
    2. Thanks.
      There are no other parameters in the datasource to config in order to ovoid that?

      Delete
    3. Nop.

      it is a datasource with a pool of active connections. this is for performance and bound to a max count of connections. Resetting datasource should be part of your plsql deployment.

      thanks

      Delete
    4. Does the active connection keeps also the DB parameters ?
      For example if one of the DB parameters was changes(like SDU etc..), will I also have to reset the datasource?

      Thanks

      Delete
  9. Thank Edwin.
    Last question - what do you mean by Resetting datasource? How can I do it? Delete and create or update the deploymnet?

    ReplyDelete
    Replies
    1. Hi,

      When you go to the datasources in the wls console , then to the control tab , select the datasource and do reset or shutdown.

      thanks

      Delete
  10. Very useful, tyvm. Running this script from Maven. I just had to add a taskdef element with a classpathref and added a classpathref attribute to the wlst element.

    ReplyDelete
  11. Hi,

    I am getting below error while running the scripts

    resetJDBC:

    BUILD FAILED
    /home/appldso1/monitoring/restartDS/reset_SOA1.xml:18: The following error occurred while executing this line:
    /home/appldso1/monitoring/restartDS/reset_SOA1.xml:30: Problem: failed to create task or type wlst
    Cause: The name is undefined.
    Action: Check the spelling.
    Action: Check that any custom tasks/types have been declared.
    Action: Check that any / declarations have taken place.


    Please advice.

    Thanks

    ReplyDelete
  12. Hi,

    I am getting below error while running the scripts, can you please advice?


    ./restartDS.sh
    *****************************************************
    ** Setting up SOA specific environment...
    *****************************************************
    EXTRA_JAVA_PROPERTIES= -da:org.apache.xmlbeans...
    .
    LD_LIBRARY_PATH=:/us1001/fmw/12c/soa/lib:/us1001/fmw/12c/wlserver/server/native/linux/x86_64:/us1001/fmw/12c/wlserver/server/native/linux/x86_64/oci920_8
    .
    .
    *****************************************************
    ** End SOA specific environment setup
    *****************************************************
    Buildfile: /home/appldso1/monitoring/restartDS/reset_SOA1.xml

    resetJDBC:

    BUILD FAILED
    /home/appldso1/monitoring/restartDS/reset_SOA1.xml:18: The following error occurred while executing this line:
    /home/appldso1/monitoring/restartDS/reset_SOA1.xml:30: Problem: failed to create task or type wlst
    Cause: The name is undefined.
    Action: Check the spelling.
    Action: Check that any custom tasks/types have been declared.
    Action: Check that any / declarations have taken place.


    Total time: 0 seconds


    Thanks,

    ReplyDelete