DataSource

Target and Untargeting of Multiple DataSources using WLST

Ravish Mody

Yesterday one of our subscribers Ravi Shankar Kumar had asked us using comment how to target and untarget multiple datasources in the same domian using a single generic WLST script, as they have total 5 clusters and want to target each cluster to different datasources and as per his requirement we created this simple WLST script which can do the same things in this single script.

As usual we have used a properties file which has all the required details in it like the datasources names, targets name and number of datasoures which has to be updated. This way you would not have to modify anything in your actual WLST script.

Target and Untargeting of Multiple DataSources using WLST

Step1). Create a Directory somewhere in your file system like : “C:WLSTDataSource

Step2). Write a Properties fileDsDetails.properties inside “C:WLSTDataSource” like following:

userName=weblogic
password=weblogic
admin.Url=t3://localhost:7001

####### DataSources names ######
ds.Name.1 = MyJDBC_Data_Source-1
ds.Name.2 = MyJDBC_Data_Source-2

##### DataSources Target names #####
target.1= Cluster
target.2= Cluster-0

##### Total Numbers of DataSources has to be modified  #####
total.Ds.Count=2

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

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

from java.io import FileInputStream

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

userName = configProps.get("userName")
password = configProps.get("password")
adminUrl = configProps.get("admin.Url")
totalDsCount = configProps.get("total.Ds.Count")

connect(userName,password,adminUrl)
edit()
startEdit()
print ''
print '======================================================================'
print 'UnTargeting and Targeting of the DataSources has started.....'
print '======================================================================'

dsCount=1
while (dsCount <= int(totalDsCount)) :
	dsName = configProps.get("ds.Name."+ str(dsCount))
	tgName = configProps.get("target."+ str(dsCount))
	cd ('/JDBCSystemResources/'+ dsName)
	set('Targets',jarray.array([], ObjectName))
	print ''
	print 'DataSource = ', dsName ,', has been UnTargeted'
	set('Targets',jarray.array([ObjectName('com.bea:Name='+tgName+',Type=Cluster')], ObjectName))
	print 'Congrats!!! DataSource = ', dsName ,', now has been Targeted to "',tgName,'"'
	print ''
	dsCount = dsCount + 1

print '======================================================================'
print 'UnTrageting and Targeting of the DataSources has been completed !!!'
print '======================================================================'
print ''

activate()
exit()

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 Tragate_UnTragate_DS.py

Following would be the Output


java weblogic.WLST Tragate_UnTragate_DS.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

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

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.

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.

======================================================================
UnTargeting and Targeting of the DataSources has started .....
======================================================================

DataSource =  MyJDBC_Data_Source-1 , has been UnTargeted
Congrats!!! DataSource =  MyJDBC_Data_Source-1 , now has been Targeted to " Cluster "

DataSource =  MyJDBC_Data_Source-2 , has been UnTargeted
Congrats!!! DataSource =  MyJDBC_Data_Source-2 , now has been Targeted to " Cluster-0 "

======================================================================
UnTargeting and Targeting of the DataSources has been completed !!!
======================================================================

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

Exiting WebLogic Scripting Tool.

Note:

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

Regards,

Ravish Mody


Creating Multiple DataSource Using WLST with Properties File

Ravish Mody

Today, few hours back one of our subscriber  Kaushik had asked us that as per their requirement they need a WLST script which should get its details from a property file and create multiple datasource instead of only one, hence while watching the Quarter Final Cricket match between India Vs Australia I have created this script which would fulfil Kaushik’s and others subscribers requirements also. So make the best out of this WLST script and have fun with the cricket fever 😉

The best thing here is the administrator need to just edit the properties file with the datasource  details and the rest of the things will be taken care by the WLST Script.

Steps to Create Multiple DataSource Using WLST

Step1). Create a Directory somewhere in your file system like : “C:WLST_DS”

Step2). Write a Properties filedetails.properties inside “C:WLST_DS” like following:

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

total.DS=3

datasource.name.1=DS_1
datasource.database.name.1=demo
datasource.target.1=AdminServer
datasource.filename.1=DS_1.xml
datasource.jndiname.1=DS_1_JNDI
datasource.driver.class.1=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url.1=jdbc:pointbase:server://localhost:9092/demo
datasource.username.1=PBPUBLIC
datasource.password.1=PBPUBLIC
datasource.test.query.1=SQL SELECT * FROM DUAL

datasource.name.2=DS_2
datasource.database.name.2=demo
datasource.target.2=AdminServer
datasource.filename.2=DS_2.xml
datasource.jndiname.2=DS_2_JNDI
datasource.driver.class.2=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url.2=jdbc:pointbase:server://localhost:9092/demo
datasource.username.2=PBPUBLIC
datasource.password.2=PBPUBLIC
datasource.test.query.2=SQL SELECT * FROM DUAL

datasource.name.3=DS_3
datasource.database.name.3=demo
datasource.target.3=AdminServer
datasource.filename.3=DS_3.xml
datasource.jndiname.3=DS_3_JNDI
datasource.driver.class.3=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url.3=jdbc:pointbase:server://localhost:9092/demo
datasource.username.3=PBPUBLIC
datasource.password.3=PBPUBLIC
datasource.test.query.3=SQL SELECT * FROM DUAL

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

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

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")

totalDataSource_to_Create=configProps.get("total.DS")

connect(adminUserName, adminPassword, adminURL)
edit()
startEdit()
print '========================================='
print 'Creating DataSource....'
print '========================================='
i=1
while (i <= int(totalDataSource_to_Create)) :

	try:
		cd('/')
		dsName=configProps.get("datasource.name."+ str(i))
		dsFileName=configProps.get("datasource.filename."+ str(i))
		dsDatabaseName=configProps.get("datasource.database.name."+ str(i))
		datasourceTarget=configProps.get("datasource.target."+ str(i))
		dsJNDIName=configProps.get("datasource.jndiname."+ str(i))
		dsDriverName=configProps.get("datasource.driver.class."+ str(i))
		dsURL=configProps.get("datasource.url."+ str(i))
		dsUserName=configProps.get("datasource.username."+ str(i))
		dsPassword=configProps.get("datasource.password."+ str(i))
		dsTestQuery=configProps.get("datasource.test.query."+ str(i))

		print ''
		print 'Creating DataSource: ',dsName,' ....'
		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))

		print 'DataSource: ',dsName,', has been created Successfully !!!'
		print ''

	except:
		print '***** CANNOT CREATE DATASOURCE !!! Check If the DataSource With the Name : ' , dsName ,' Alreday exists or NOT...'
		print ''
	i = i + 1
print '========================================='
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 createMultipleDataSource.py

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

Following would be the Output

java weblogic.WLST createMultipleDataSource.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

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

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.

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)
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.
=========================================
Creating DataSource....
=========================================

Creating DataSource:  DS_1  ....
DataSource:  DS_1 , has been created Successfully !!!

Creating DataSource:  DS_2  ....
DataSource:  DS_2 , has been created Successfully !!!

Creating DataSource:  DS_3  ....
DataSource:  DS_3 , has been created Successfully !!!

=========================================
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

Regards,
Ravish Mody


JDBC Tips

Hi,

Jay SenSharma

Jay SenSharma

Are you facing JDBC related issues in your WebLogic Environment the Please Keep the following tips always in mind.

Point-1). Set the Maximum Capacity of the connection pool at least equal to the Execute Thread Count.

Point-2). Configure Inactive Connection Timeout to specify how long (in seconds) a connection can be inactive before it is reclaimed to the pool.

Point-3). The connection leak profiling option shows leaked connections from the connection pool. BEA recommends that you do not use this option in production; it uses extra resources and typically slows connectionpool operations.

Point-4). If possible, size database connection pools so that they never increase the number of connections: set Initial Capacity equal to Maximum Capacity.

Point-5). Use Test Reserved Connections only if you can afford the overhead of testing connections as part of normal request processing.

Point-6). Avoid using a production table for “Test Table Name”, use a dummy table (ex. dual).

Point-7). Use the statement cache to improve performance of prepared and callable statements.

Point-8). Select the least-recently-used (LRU) algorithm for the cache; this removes rarely-used statements from the cache.

Point-9). Connection Creation Retry Frequency can be used to retry for establishing connections to the database, if database is not reachable when creating connection pool or starting WebLogic Server.

Point-10). If database is restarted when WebLogic Server is running, Test Frequency can be increased from 0, so that all the connections are closed and reopened to re-establish valid physical connection. After all the connections are recreated changing it back to 0 will disable the testing.

Point-11). When using DataSource objects for a connection pool, use the Honors Global Transaction option to create a TxDataSource.

Point-12). The only time you should use a non-Tx Data Source is when you want to do some work on the database that you do not want to include in the current transaction.

Point-13). When configuring a connection pool to use with WebLogic JMS JDBC Store, use non-XA database drivers.

Point-14). Always close the ResultSet, Statement and Connection objects insode the finally{} Block in the following order:

try {
         //...
        // YOUR JDBC Code here
        
    } finally {
            try { 
                  if(resultSet != null) 
                      resultSet.close(); 
            } catch(Exception e) {
                   e.printStackTrace();
            }

            try { 
                  if(statement != null) 
                      statement.close(); 
            } catch(Exception e) {
                   e.printStackTrace();
            }

            try { 
                  if(connection != null) 
                      connection.close(); 
            } catch(Exception e) {
                   e.printStackTrace();
            }                      
    }

——————————

Use the following command to make sure that you are able to connect to the DataBase Box from the WLS Server Box…

Step1). Add JDBC Driver also in the Classpath or Better run “setWLSEnv.sh”

Step2). Use WLS DB Ping utility:
Syntax:

java -classpath /bea103/wl_server103/server/lib/weblogic.jar utils.dbping ORACLE_THIN &lt;dbUserName&gt; &lt;dbPasswoes&gt; &lt;dbURL&gt;

Example:

java -classpath /bea103/wl_server103/server/lib/weblogic.jar utils.dbping ORACLE_THIN scott tiger databaseHostName:1521:P15215h

While using WLST/JMX to get Connection from DatSource if you get the Following Exception:

java.lang.UnsupportedOperationException: java.lang.UnsupportedOperationException: Remote JDBC disabled

Then Please Add the Following JAVA_OPTION in your Servers StartScript:

“-Dweblogic.jdbc.remoteEnabled=true”

Example WLST Script to Get Connection from WLS DataSource:

uname = "weblogic"
pwd = "weblogic1"
url = "t3://localhost:7101"

#======================================================
def diagnoseWebConfig(object) :

from java.sql import *
from javax.sql import *
from javax.naming import *
from java.util import *

connect(uname, pwd, url)
JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory"
serverUrl ="t3://localhost:7001"

env = Hashtable()
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY)
env.put(Context.PROVIDER_URL, serverUrl)
ic = InitialContext(env)

ds=ic.lookup("jdbc/TestDS")
con=ds.getConnection()
print "nt Got Connection From TestDS : "+con
con.close();

#=========================================================
if __name__== "main":
diagnoseWebConfig('app')

Thanks
Jay SenSharma


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