Tag: DataSource

Changing DataSource Details Using WLST (Multiple Domains)

Hi,

Jay SenSharma

Jay SenSharma

Thanks to Magic User “Mr. Baji ”  http://middlewaremagic.com/weblogic/?p=4261#comment-2360 for asking the a very useful query to the magic team….

In Production Environments it is very frequent that for Maintenance/Security reasons we frequently change or Update the DataSource Username and Passwords. But it becomes a problem for WebLogic Administrator when there are multiple DataSources present as part of a WebLogic Domain.

But using the following script it will be very easy for a WebLogic Administrator to change all the DataSource UserName and Passwords Just by Editing a Properties file.  The WLST Script is going to recursively read the Property file and then it will change the DataSource Properties like UserName and Password one by one from Old to the new one.

The Below Mentioned WLST Script can also be enhanced to even change the DataSource URL it means if we want to change the Database to some other failover Database we can do it easily using the following kind of script.

Step1). Create a Directory somewhere in your filesystem like  “C:WLST_MultiDomain_DS”

Step2). Now create a properties file inside “C:WLST_MultiDomain_DS” with some name like “domainsDetails.properties” and write the following kind of entries inside it:

ds.name.1=TestDS_One
ds.username.1=PBPUBLIC
ds.username.new.1=PBPUBLIC_ONE
ds.password.1=PBPUBLIC
ds.password.new.1=PBPUBLIC_ONEPASS

ds.name.2=TestDS_Two
ds.username.2=jack
ds.username.new.2=mack
ds.password.2=password
ds.password.new.2=mackPassword

Step2). Now  write a WLST script inside “C:WLST_MultiDomain_DS” with some name like “Main.py” and write the following kind of entries inside it:

from java.io import FileInputStream

propInputStream = FileInputStream("domainsDetails.properties")
configProps = Properties()
configProps.load(propInputStream)

for i in 1,2:
	mainDSName=configProps.get("ds.name."+ str(i))
	oldDSUserName = configProps.get("ds.username."+ str(i))
	newDSUserName = configProps.get("ds.username.new."+ str(i))
	oldDSPassword = configProps.get("ds.password."+ str(i))
	newDSPassword = configProps.get("ds.password.new."+ str(i))
	i = i + 1

	print("*** Trying to Connect.... *****")
	connect('weblogic','weblogic','t3://localhost:7001')
	print("*** Connected *****")
	cd('/Servers/AdminServer')
	edit()
	startEdit()
	cd('JDBCSystemResources')
	pwd()
	ls()
	allDS=cmo.getJDBCSystemResources()
	for tmpDS in allDS:
		       dsName=tmpDS.getName();
		       print 'DataSource Name: ', dsName
		       print ' '
		       print ' '
		       if  mainDSName == dsName:
		       		print 'Changing Password & UserName for DataSource ', dsName
		       		cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDriverParams/'+dsName)
		       		print('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDriverParams/'+dsName)
		       		set('PasswordEncrypted', newDSPassword)
		       		cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDriverParams/'+dsName+'/Properties/'+dsName+'/Properties/user')
		       		set('Value',newDSUserName)
		       		print("*** CONGRATES !!! Username & Password has been Changed for DataSource: ", dsName)
		       		print ('')
		       		print ('')

save()
activate()

Step3). Run the “setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the <BEA_HOME>/wlserver_10.3/server/bin) then run the following command….
.  ./setWLSEnv.sh
Note: the first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step4). Now run the WLS Script like following:

java        weblogic.WLST        Main.py

You wil get the following kind of results:


C:WLST_MultiDomain_DS>java weblogic.WLST Main.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

*** Trying to Connect.... *****
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain '7001_Domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

*** Connected *****
Location changed to edit tree. This is a writable tree with
DomainMBean as the root. To make changes you will need to start
an edit session via startEdit().

For more help, use help(edit)

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
drw-   TestDS_One
drw-   TestDS_Two

DataSource Name:  TestDS_One

Changing Password & UserName for DataSource  TestDS_One
/JDBCSystemResources/TestDS_One/JDBCResource/TestDS_One/JDBCDriverParams/TestDS_One
('*** CONGRATES !!! Username & Password has been Changed for DataSource: ', 'TestDS_One')

DataSource Name:  TestDS_Two

*** Trying to Connect.... *****
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain '7001_Domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

*** Connected *****
You already have an edit session in progress and hence WLST will
continue with your edit session.

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
drw-   TestDS_One
drw-   TestDS_Two

DataSource Name:  TestDS_One

DataSource Name:  TestDS_Two

Changing Password & UserName for DataSource  TestDS_Two
/JDBCSystemResources/TestDS_Two/JDBCResource/TestDS_Two/JDBCDriverParams/TestDS_Two
('*** CONGRATES !!! Username & Password has been Changed for DataSource: ', 'TestDS_Two')

Saving all your changes ...
Saved all your changes successfully.
Activating all your changes, this may take a while ...
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed

C:WLST_MultiDomain_DS>

.

.

Thanks

Jay SenSharma


WebLogic DataSource Monitoring Using WLST

Hi,

Jay SenSharma

Jay SenSharma

Many times we want to monitor the different modules available as part of our WebLogic Domain. WebLogic Scripting Tool (WLST) is one of the best utility which is based on Jython (The Java Version of Python Scripting) Option.

WLST not only allows to configure different resources in WebLogic Server/Domain as well as it allows a very easy way to monitor different resources available as part of WebLogic Server/Domain. We can monitor JMS/ DataSources/JDBC Parameters…etc using WLST.

Here we are going to see a very simple demonstration of using WLST to monitor DataSources and the JDBC Properties which it holds, this part of monitoring includes both the Configuration Informations as well as the Runtime informations of the dataSource.

Step1). Create a WLST Script somewhere in your file system with some name like “Monitor_Jdbc.py”   inside “C:WLST_Jdbc”  contents will be something like following:

#############################################################################
#
# @author Copyright (c) 2010 - 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################

connect('weblogic','weblogic','t3://localhost:7001')
allServers=domainRuntimeService.getServerRuntimes();
if (len(allServers) > 0):
  for tempServer in allServers:
    jdbcServiceRT = tempServer.getJDBCServiceRuntime();
    dataSources = jdbcServiceRT.getJDBCDataSourceRuntimeMBeans();
    if (len(dataSources) > 0):
		for dataSource in dataSources:
			print 'ActiveConnectionsAverageCount      '  ,  dataSource.getActiveConnectionsAverageCount()
			print 'ActiveConnectionsCurrentCount      '  ,  dataSource.getActiveConnectionsCurrentCount()
			print 'ActiveConnectionsHighCount         '  ,  dataSource.getActiveConnectionsHighCount()
			print 'ConnectionDelayTime                '  ,  dataSource.getConnectionDelayTime()
			print 'ConnectionsTotalCount              '  ,  dataSource.getConnectionsTotalCount()
			print 'CurrCapacity                       '  ,  dataSource.getCurrCapacity()
			print 'CurrCapacityHighCount              '  ,  dataSource.getCurrCapacityHighCount()
			print 'DeploymentState                    '  ,  dataSource.getDeploymentState()
			print 'FailedReserveRequestCount          '  ,  dataSource.getFailedReserveRequestCount()
			print 'FailuresToReconnectCount           '  ,  dataSource.getFailuresToReconnectCount()
			print 'HighestNumAvailable                '  ,  dataSource.getHighestNumAvailable()
			print 'HighestNumUnavailable              '  ,  dataSource.getHighestNumUnavailable()
			print 'LeakedConnectionCount              '  ,  dataSource.getLeakedConnectionCount()
			print 'ModuleId                           '  ,  dataSource.getModuleId()
			print 'Name                               '  ,  dataSource.getName()
			print 'NumAvailable                       '  ,  dataSource.getNumAvailable()
			print 'NumUnavailable                     '  ,  dataSource.getNumUnavailable()
			print 'Parent                             '  ,  dataSource.getParent()
			print 'PrepStmtCacheAccessCount           '  ,  dataSource.getPrepStmtCacheAccessCount()
			print 'PrepStmtCacheAddCount              '  ,  dataSource.getPrepStmtCacheAddCount()
			print 'PrepStmtCacheCurrentSize           '  ,  dataSource.getPrepStmtCacheCurrentSize()
			print 'PrepStmtCacheDeleteCount           '  ,  dataSource.getPrepStmtCacheDeleteCount()
			print 'PrepStmtCacheHitCount              '  ,  dataSource.getPrepStmtCacheHitCount()
			print 'PrepStmtCacheMissCount             '  ,  dataSource.getPrepStmtCacheMissCount()
			print 'Properties                         '  ,  dataSource.getProperties()
			print 'ReserveRequestCount                '  ,  dataSource.getReserveRequestCount()
			print 'State                              '  ,  dataSource.getState()
			print 'Type                               '  ,  dataSource.getType()
			print 'VersionJDBCDriver                  '  ,  dataSource.getVersionJDBCDriver()
			print 'WaitingForConnectionCurrentCount   '  ,  dataSource.getWaitingForConnectionCurrentCount()
			print 'WaitingForConnectionFailureTotal   '  ,  dataSource.getWaitingForConnectionFailureTotal()
			print 'WaitingForConnectionHighCount      '  ,  dataSource.getWaitingForConnectionHighCount()
			print 'WaitingForConnectionSuccessTotal   '  ,  dataSource.getWaitingForConnectionSuccessTotal()
			print 'WaitingForConnectionTotal          '  ,  dataSource.getWaitingForConnectionTotal()
			print 'WaitSecondsHighCount               '  ,  dataSource.getWaitSecondsHighCount()

Step2). Start your WebLogic Server and Databases (Make sure that you have configured some dataSources in the Server to monitor).

Step3). Open a command prompt and then run the “setWLSEnv.cmd” or “setWLSEnv.sh”  to set the CLASSPATH and PATH variables. Better you do echo %CLASSPATH%    or echo $CLASSPATH to see whether the CLASSPATH is set properly or not. If you see an Empty Classpath even after running the “setWLSEnv.sh” then please refer to the Note mentioned at Step3) in the Following post:   http://middlewaremagic.com/weblogic/?page_id=1492

Step4). Now run the WLST Script in the same command prompt like following:

java       weblogic.WLST        C:WLST_JdbcMonitor_Jdbc.py
You will see the following kind of results in the command prompt

ActiveConnectionsAverageCount       0
ActiveConnectionsCurrentCount       0
ActiveConnectionsHighCount          0
ConnectionDelayTime                 25
ConnectionsTotalCount               10
CurrCapacity                        10
CurrCapacityHighCount               1
DeploymentState                     2
FailedReserveRequestCount           0
FailuresToReconnectCount            0
HighestNumAvailable                 10
HighestNumUnavailable               0
LeakedConnectionCount               0
ModuleId                            TestDS
Name                                TestDS
NumAvailable                        10
NumUnavailable                      0
Parent                              [MBeanServerInvocationHandler]com.bea:Name=AdminServer,Location=AdminServer,Type=ServerRuntime
PrepStmtCacheAccessCount            0
PrepStmtCacheAddCount               0
PrepStmtCacheCurrentSize            0
PrepStmtCacheDeleteCount            0
PrepStmtCacheHitCount               0
PrepStmtCacheMissCount              0
Properties                          {databaseName=jdbc:pointbase:server://localhost:9092/demo, user=PBPUBLIC}
ReserveRequestCount                 0
State                               Running
Type                                JDBCDataSourceRuntime
VersionJDBCDriver                   com.pointbase.jdbc.jdbcUniversalDriver
WaitingForConnectionCurrentCount    0
WaitingForConnectionFailureTotal    0
WaitingForConnectionHighCount       0
WaitingForConnectionSuccessTotal    0
WaitingForConnectionTotal           0
WaitSecondsHighCount                0
Jdbc_Monitoring_Using_WLST

Jdbc_Monitoring_Using_WLST

.

.

Thanks

Jay SenSharma


Creating WebLogic DataSource Using WLST and Properties File

Hi All,

Jay SenSharma

Jay SenSharma

We have seen many demonstrations of reading a properties file inside a WLST script to make is fully automated and easily managable without Hardcoding anything inside it. We will see many demos of creating and configuring various resources in WebLogic Server using the WLST where we used properties file data as an input to the resource configuration. Here is an example in which we will see How to Configure WebLogic DataSource using WLST?

In this sample we will use a properties file to  avoid hard coding. This script will be same for various DataSource configuration just we need to change the Properties files entry to make a new DataSource.

Step1). Create a Directory somewhere in your file system like:  “C:WLST_DS_Creation” and place a properties file like below in this directory with name “details.properties”

domain.name=7001_Domain
admin.url=t3://localhost:7001
admin.userName=weblogic
admin.password=weblogic

datasource.name=DS_One
datasource.database.name=demo
datasource.target=AdminServer
datasource.filename=DS_One.xml
datasource.jndiname=DS_One_JNDI
datasource.driver.class=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url=jdbc:pointbase:server://localhost:9092/demo
datasource.username=PBPUBLIC
datasource.password=PBPUBLIC

datasource.test.query=SQL SELECT * FROM DUAL

Step2). Now in the same directory write the following WLST Script “createDataSource.py” like following:

from java.io import FileInputStream

propInputStream = FileInputStream("details.properties")
configProps = Properties()
configProps.load(propInputStream)

domainName=configProps.get("domain.name")
adminURL=configProps.get("admin.url")
adminUserName=configProps.get("admin.userName")
adminPassword=configProps.get("admin.password")

dsName=configProps.get("datasource.name")
dsFileName=configProps.get("datasource.filename")
dsDatabaseName=configProps.get("datasource.database.name")
datasourceTarget=configProps.get("datasource.target")
dsJNDIName=configProps.get("datasource.jndiname")
dsDriverName=configProps.get("datasource.driver.class")
dsURL=configProps.get("datasource.url")
dsUserName=configProps.get("datasource.username")
dsPassword=configProps.get("datasource.password")
dsTestQuery=configProps.get("datasource.test.query")

connect(adminUserName, adminPassword, adminURL)
edit()
startEdit()
cd('/')
cmo.createJDBCSystemResource(dsName)
cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName)
cmo.setName(dsName)

cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
set('JNDINames',jarray.array([String('jdbc/' + dsName )], String))

cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName )
cmo.setUrl(dsURL)
cmo.setDriverName( dsDriverName )
cmo.setPassword(dsPassword)

cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCConnectionPoolParams/' + dsName )
cmo.setTestTableName(dsTestQuery)
cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
cmo.createProperty('user')

cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/user')
cmo.setValue(dsUserName)

cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
cmo.createProperty('databaseName')

cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/databaseName')
cmo.setValue(dsDatabaseName)

cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
cmo.setGlobalTransactionsProtocol('OnePhaseCommit')

cd('/SystemResources/' + dsName )
set('Targets',jarray.array([ObjectName('com.bea:Name=' + datasourceTarget + ',Type=Server')], ObjectName))

save()
activate()

Step3). Now Open a Command/Shell Prompt and then run the “setWLSEnv.sh” script to set the CLASSPATH and PATH environment variables. Run the “. ./setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the <BEA_HOME>/wlserver_10.3/server/bin) then run the following command….
.  ./setWLSEnv.sh


Note: Here The first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step4). Run the Above WLST Script like following:

java weblogic.WLST createDataSource.py

Make sure that the Database is running while you are creating the DataSource.

.

.

Thanks

Jay SenSharma


Copyright © 2010-2012 Middleware Magic. All rights reserved. |