How to Configure WebLogic DataSource using WLST?
Step 1. Create working directory and Server properties file
Create a directory at the machine that has the Weblogic Server installed on.
Example: (Linux OS) create directory called /home/create_DS
place a properties file like below in this directory with name "server.properties"
domain.name=7001_Domain
admin.url=t3://localhost:7001
admin.userName=weblogic
admin.password=weblogicPassword
datasource.name=jdbc/DS_Name
datasource.database.name=demo
datasource.targets_server=AdminServer
datasource.targets_cluster=cluster_server_1,cluster_server_2
datasource.filename=jdbc_DS_Name.xml
datasource.jndiname=DS_Name
datasource.driver.class=oracle.jdbc.OracleDriver
datasource.url=jdbc:oracle:thin:@192.168.1.1:1521/DB_NAME
datasource.username=DB_Username
datasource.password=DB_Password
datasource.test.query=SQL SELECT * FROM DUAL
Step 2. Create createDataSource.py
Now in the same directory write the following WLST Script “createDataSource.py” like following:
from java.io import FileInputStream
propInputStream = FileInputStream("server.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")
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")
try:
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(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 )
targets=PyList()
datasource_targets_server=configProps.get("datasource.targets_server").split(",")
datasource_targets_cluster=configProps.get("datasource.targets_cluster").split(",")
for datasourceTarget in datasource_targets_server:
targetStr='com.bea:Name='+datasourceTarget+',Type=Server'
targets.append(ObjectName(targetStr))
for datasourceTarget in datasource_targets_cluster:
targetStr='com.bea:Name='+datasourceTarget+',Type=Cluster'
targets.append(ObjectName(targetStr))
set('Targets',jarray.array(targets,ObjectName))
save()
activate()
except Exception, e:
print 'ERROR:'
print e
print 'Operation was canceled.'
undo('true','y')
cancelEdit('y')
exit()
Step 3. Running setWLSEnv.sh script
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
/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.
Step 4. Running the createDataSource.py script
Run the Above WLST Script like following:
java weblogic.WLST createDataSource.py
OR
java -cp /<WL_HOME>/wlserver/server/lib/weblogic.jar weblogic.WLST createDataSource.py
Make sure that the Database is running while you are creating the DataSource.