Wednesday, February 3, 2010

Configuring a Generic Datasource

Configuring the datasource is one time activity for production environment but where as for testing environment there is always need for change the datasource as per the demand. These changes makes interrupt majorly the development process. To improve this process we can have a Generic JDBC Data source configuring script.

Creating the data source using a python script makes reusable. Here my perception is that if we don’t hard code the JDBC parameters it will be easy to use for all environments as well as for any kinds of drivers also.

Initially lets go with single Data source creation with targeting to the user input Server it can be Managed Server or AdminServer (basic domain), later we can go on for improve further to target on Cluster.

This scripting we can write in two ways offline and in online. In the offile mode we need to navigate the Mbean and create, configure parameter and finally do assign() them to a Server or Cluster of a domain. For the online script we need to set the target using setTarget() command. To do this we must connect to the Admin server and acquire lock on configuration repository by edit() or startEdit() commands.

Oracle WebLogic supports jDriver for various DBMS. WebLogic also supports XA drivers for distributed databases. Supporting Third-Party Drivers also available from DBMS vendors. WebLogic supports Drivers for the following DBMS:

Cloudscape DB2 PostgreSQL
Oracle  Ms SQL Progress
MySQL  PointBase Sybase

Configuring new data source custom properties
Now let us try to configure a new data source with a custom properties for Connection Pool parameters, weblogic console connecting parameters into a same file or you can specify with different properties files. When the properties file is used it must be loaded before first line of processing statement in the script. We have two options to load the properties one is using command line and other one is using loadProperties() method.

###################****##############****########################
# Generic script applicable on any Operating Environments (Unix, Windows)
# ScriptName    : ConfigDS.py
# Properties    : ConfigDS.properties
# Author        : Srikanth Panda
# Updated by    : Pavan Devarakonda
###############     Connecting to Start     #################################
def connectAdmin() :
try:
connect(CONUSR,CONPWD, CONURL)
print('Successfully connected')
except:
print 'Unable to find admin server...'
exit()
################### Configuring Connection Pool #############################
def connPool(DSnam) :
DRVPARM='/JDBCSystemResources/'+DSnam+'/JDBCResource/'+DSnam+'/JDBCDriverParams/'+DSnam
cd(DRVPARM)
set('Url',DBURL)
set('DriverName',DBDRV)
set('Password',DBPASS)
cd(DRVPARM+'/Properties/'+DSnam)
cmo.createProperty('user')
cd(DRVPARM+'/Properties/'+DSnam+'/Properties/user')
set('Value',DBUSR)

############         Creating Data source    ###############################
def createDS() :
print('Naming the datasource')
DSnam = DSName
cmo.createJDBCSystemResource(DSnam)
RESOURCE='/JDBCSystemResources/'+DSnam+'/JDBCResource/'+DSnam
cd(RESOURCE)
set('Name',DSnam)

#Setting JNDI name
cd(RESOURCE+'/JDBCDataSourceParams/'+DSnam)
print RESOURCE+'/JDBCDataSourceParams/'+DSnam
set('JNDINames',jarray.array([String(DSnam)], String))

connPool(DSnam)

#Set Connection Pool specific parameters
cd(RESOURCE+'/JDBCConnectionPoolParams/'+DSnam)
cmo.setTestConnectionsOnReserve(true)
cmo.setTestTableName('SQL SELECT 1 FROM DUAL')
cmo.setConnectionReserveTimeoutSeconds(25)
cmo.setMaxCapacity(15)
cmo.setConnectionReserveTimeoutSeconds(10)
cmo.setTestFrequencySeconds(120)

cd(RESOURCE+'/JDBCDataSourceParams/'+DSnam)
cmo.setGlobalTransactionsProtocol('TwoPhaseCommit')
cd('/JDBCSystemResources/'+DSnam)

# targets the DS to Servers(Cluster or Server)
targetType=raw_input('Target to (C)luster or (S)erver: ')
if targetType in ('C','c') :
clstrNam=raw_input('Cluster Name: ')
set('Targets',jarray.array([ObjectName('com.bea:Name='+clstrNam+',Type=Cluster')], ObjectName))
else:
servr=raw_input('Server Name: ')
set('Targets',jarray.array([ObjectName('com.bea:Name='+servr+',Type=Server')], ObjectName))

###############     Main Script   #####################################
if __name__== "main":
print('This will enable you to create or update a Datasource')
connectAdmin()
edit()
startEdit()
# Create a new JDBC resource)
cd('/')
createDS()
save()
activate()
disconnect()
####################################


The customized properties file "configDS.properties" goes like this:
DBURL=jdbc:oracle:thin:@dbhostname:dbport:dbschema
DBDRV=oracle.jdbc.OracleDriver
DBPASS=dbpasswd
DBUSR=dbuser
DSName=myDs
CONUSR=system
CONPWD=*********
CONURL=hostname:adminport


Now to execute the custom properties datasource script the command will be given as follows:
java weblogic.WLST –loadProperties ConfigDS.properties ConfigDS.py

6 comments:

Anonymous said...

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 "/nfs/usr/appl/ja10/stage/scripts/wls10-20100504_220247/wlst-automation/new/ConfigDS.py", line 9
try:
^
SyntaxError: invalid syntax

Anonymous said...

i am getting below error, while running the script on linux servers. can you help me

Pavan Bhavani Shekhar said...

please follow indentation, after every fresh block you need 4 spaces or a tab space to indicate the block.

Anonymous said...

hi,

I've tried below but it's didn't read data!!

–loadProperties C:\ConfigDS.properties

would you help?

Pavan Bhavani Shekhar said...

can try using double slashes...

other alternative is wls:/mydomain/serverConfig> loadProperties(' C:\ConfigDS.properties')

prabhudatta Mahapatra said...

Hi Srikanta,

how to create for loop in python script becz we have to lot of propperties, everytime it will call to wlst for creating properties but I want to call only once to wlst to create multiple properties file at atime, for this reason I want put a for loop in python script, how to do please guide me , please help me ASAP

Search This Blog

Loading...

My Favorites