Posted on March 21, 2016 by James Gilstrap
Recently, I found myself needing to write a script to change JDBC Data Source properties on the fly and restart them since I was constantly changing these properties using the WebLogic Server Admin Console. While it can be done using the Admin Console, that process was very time consuming and prone to error. Enter the WebLogic Scripting Tool (WLST) and the power of Jython! This post will demonstrate the power of WLST and only a fraction of what you can do with the tool. While it is specific to WebLogic Server 11g (10.3.6), the concepts also apply to WebLogic Server 12c as well.
The WebLogic Scripting Tool is a command-line scripting environment that you can use to create, manage and monitor WebLogic domains. It is based on the Java scripting interpreter, Jython. In addition to supporting standard Jython features such as local variables, conditional variables and flow control statements, WLST provides a set of scripting functions, or commands, that are specific to WebLogic Server. You can extend the WebLogic scripting language to suit your needs by following the Jython language syntax.
Applications use naming services to locate objects in data sources, EJBs, JMS, Mail Sessions, and so on in the network. A naming service associates names with objects and finds objects based on their given names.
JNDI provides a common-denominator interface to many existing naming services. These naming services maintain a set of bindings, which relate names to objects and provide the ability to look up objects by name. JNDI allows the components in distributed applications to locate each other.
By switching the JNDI Name of a JDBC Data Source, you can point a Java web application to a different database without having to change any application code. When the JDBC Data Source is restarted, all database connections are reset and pointed to the new database.
The sections that follow explain the script, its purpose and the functions within the script, at the end of this post I will include the script in its entirety:
The first thing you will need to do is connect to the AdminServer and start an edit session:
The original purpose of this script was to update the JNDI Names and switch them from one JNDI name to the other for two JDBC Data Sources. You could modify this script to update any attributes of the JDBC Data Source. This comes in handy if you are switching a data source to point from Test to Production, or switching it from the main database to a hot spare database:
This script needs user input, so we need to prompt the user for the name of the data sources and the JNDI Names that we need for the switch. You could also hard-code the data source names and JNDI names if you are always using the same names:
After updating the JNDI Names (or any other attributes of a data source) you need to restart the data sources to reset them and any database connections. You can always use the Admin Console, but the WLST method is much faster. The function defined below, restartDS(), does the restart for us in a fraction of the time:
Now, we just need a main() function to wrap it up and call everything:
In summary, the WebLogic Scripting Tool is a very powerful tool that can be used both online and offline.
In the online mode you can:
In the offline mode you can:
This is just some of the power! For more on the WebLogic Scripting Tool, see the WebLogic Server 12c (12.2.1) Tutorials, located at: http://www.oracle.com/webfolder/technetwork/tutorials/obe/fmw/wls/12c/12_2_1/01-32-001-UsingWLST/Using_WLST.html
James Gilstrap, Senior Principal Consultant, Mythics Consulting