Creating Data source and DB adapter outbound connection pool using WLST

In this post we will see how to create a data source , DB adapter outbound connection pool and redeployment using wlst scripts.

Lets look at the script which creates both DS and DB adapter connection pool and redeployment.

import time

# admin server url
url = ‘t3://localhost:7001’
# username to connect to the admin server
username = ‘weblogic’
# password to connect to the admin server
password = ‘welcome1’
# the name for the EIS
eisName = ‘eis/db/myDS’
# the admin or managed server to target where the DbAdapter is deployed
serverName = ‘AdminServer’
#nmServerName = ‘soa_server1’
# the name for the data source
dsName = ‘myDS’
# the JNDI name for the data source
jndiName = ‘jbdc/myDS’
# the database url for the data source
dbUrl = ‘jdbc:oracle:thin:@localhost:1522:SOAORA’
# the database user
dbUser = ‘soa12c_soainfra’
# the database password
dbPassword = ‘welcome_1’
# the database driver to use
dbDriver = ‘oracle.jdbc.xa.client.OracleXADataSource’
# the host where node manager is running
#nmHost = ‘localhost’
# the port to connect to node manager (5556 is default for plain mode)
#nmPort = ‘5556’
# the user to connect to node manager
#nmUser = ‘weblogic’
# the password to connection to node manager
#nmPassword = ‘welcome1’
# the name of the weblogic domain
domain = ‘soa12c_domain’

# don’t change the below ones
uniqueString = ”
appName = ‘DbAdapter’
moduleOverrideName = appName+’.rar’
moduleDescriptorName = ‘META-INF/weblogic-ra.xml’

#
# method definitions
#

def makeDeploymentPlanVariable(wlstPlan, name, value, xpath, origin=’planbased’):

try:
variableAssignment = wlstPlan.createVariableAssignment(name, moduleOverrideName, moduleDescriptorName)
variableAssignment.setXpath(xpath)
variableAssignment.setOrigin(origin)
wlstPlan.createVariable(name, value)

except:
print(‘–> was not able to create deployment plan variables successfully’)

def main():

uniqueString = str(int(time.time()))

#
# Create a JDBC Data Source.
#

try:
print(‘–> about to connect to weblogic’)
connect(username, password, url)
print(‘–> about to create a data source ‘ + dsName)
edit()
startEdit()
cmo.createJDBCSystemResource(dsName)
cd(‘/JDBCSystemResources/’ + dsName + ‘/JDBCResource/’ + dsName)
cmo.setName(dsName)
cd(‘/JDBCSystemResources/’ + dsName + ‘/JDBCResource/’ + dsName + ‘/JDBCDataSourceParams/’ + dsName)
set(‘JNDINames’,jarray.array([String(jndiName)], String))
cd(‘/JDBCSystemResources/’ + dsName + ‘/JDBCResource/’ + dsName + ‘/JDBCDriverParams/’ + dsName)
cmo.setUrl(dbUrl)
cmo.setDriverName(dbDriver)
cmo.setPassword(dbPassword)
cd(‘/JDBCSystemResources/’ + dsName + ‘/JDBCResource/’ + dsName + ‘/JDBCConnectionPoolParams/’ + dsName)
cmo.setTestTableName(‘DUAL’)
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(dbUser)
cd(‘/JDBCSystemResources/’ + dsName + ‘/JDBCResource/’ + dsName + ‘/JDBCDataSourceParams/’ + dsName)
cmo.setGlobalTransactionsProtocol(‘TwoPhaseCommit’)
cd(‘/JDBCSystemResources/’ + dsName)
set(‘Targets’,jarray.array([ObjectName(‘com.bea:Name=’ + serverName + ‘,Type=Server’)], ObjectName))
save()
print(‘–> activating changes’)
activate()
print(‘–> done’)

#
# update the deployment plan
#
print(‘–> about to update the deployment plan for the DbAdapter’)
startEdit()
planPath = get(‘/AppDeployments/DbAdapter/PlanPath’)
appPath = get(‘/AppDeployments/DbAdapter/SourcePath’)
print(‘–> Using plan ‘ + planPath)
plan = loadApplication(appPath, planPath)
print(‘–> adding variables to plan’)
makeDeploymentPlanVariable(plan, ‘ConnectionInstance_eis/DB/’ + dsName + ‘_JNDIName_’ + uniqueString, eisName, ‘/weblogic-connector/outbound-resource-adapter/connection-definition-group/[connection-factory-interface=”javax.resource.cci.ConnectionFactory”]/connection-instance/[jndi-name=”‘ + eisName + ‘”]/jndi-name’)
makeDeploymentPlanVariable(plan, ‘ConfigProperty_xADataSourceName_Value_’ + uniqueString, eisName, ‘/weblogic-connector/outbound-resource-adapter/connection-definition-group/[connection-factory-interface=”javax.resource.cci.ConnectionFactory”]/connection-instance/[jndi-name=”‘ + eisName + ‘”]/connection-properties/properties/property/[name=”xADataSourceName”]/value’)
print(‘–> saving plan’)
plan.save();
save();
print(‘–> activating changes’)
activate(block=’true’);
cd(‘/AppDeployments/DbAdapter/Targets’);
print(‘–> redeploying the DbAdapter’)
redeploy(appName, planPath, targets=cmo.getTargets());
print(‘–> done’)

except:
print(‘–> something went wrong, bailing out’)
stopEdit(‘y’)
raise SystemExit

#
# disconnect from the admin server
#

print(‘–> disconnecting from admin server now’)
disconnect()

#
# this is the main entry point
#

main()

To use this script, we need to edit the parameters at the top of the program.

save it somewhere in the server location(cd /your/fmwhome/wlserver/server/bin/) with name like “createMyDS.py”

Set the domain environment by running the below command.

cd /your/fmwhome/wlserver/server/bin/> setDomainEnv.cmd

Now run the python script with below command.

/your/fmwhome/wlserver/server/bin/>  java weblogic.WLST createMyDS.py

Now if you open your weblogic console you will find both DS and DB adapter configured with above values.

wlstdswlstdb1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s