I came across the need for below requirement when i observed that developers are spending lot of time and making mistakes while setting up datasources on weblogic domain. I decided to simplify it by making it a one click approach.
You could use it in your project to simplify developers work while configuring datasources.

While creating data sources following steps are to be done.

1) Connect to Domain- We need to connect to domain to configure datasources.

connect('weblogic','weblogic1','t3://localhost:7001',adminServerName='AdminServer'); 

2) Enable Edit Mode

cd('/')
edit()
startEdit()

3) Delete existing datasource with same name. (This step is not needed if we are setting up first time).

try:
	cd('/SystemResources/jdbc/csJndiName')
	set('Targets',jarray.array([], ObjectName))
except:
	print 'Unset Target failed'

try:
	cd('/JDBCSystemResources')
	dataSources = cmo.getJDBCSystemResources()
	for dataSource in dataSources :
		dsName = dataSource.getName()
		if dsName == iDsName:
			try:
				dataSource.forceShutdown()
			except:
				print 'failed to force shudown'
			try:
				cmo.destroyJDBCSystemResource(dataSource)				
			except:
				print 'failed to destroy'
			print 'DataSource '+ iDsName + 'is deleted'				
except:
	print 'Unable to delete the existing data source. '

If you see the above code, We are doing below steps during datasource delete.

  • cd('/SystemResources/jdbc/' + codeSimplifyJndiName) – Access the data source
  • set('Targets',jarray.array([], ObjectName)) – Removes current target/server set to the data source
  • dataSource.forceShutdown() – Shutdown the data source
  • cmo.destroyJDBCSystemResource(dataSource) – Destroy the data source

3) Save and activate the changes after datasource delete.

save()
activate()
startEdit()
cd('/')

4) Create datasource and Attach it to domain

cmo.createJDBCSystemResource('jdbc/csJndiName')

cd('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName')
cmo.setName('jdbc/csJndiName')

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

cd('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName/JDBCDriverParams/jdbc/csJndiName')
cmo.setUrl('jdbc:oracle:thin:@codeSimplify.db:1521:'+ dbName)
cmo.setDriverName('oracle.jdbc.xa.client.OracleXADataSource')
cmo.setPassword(passWd)
print 'pass word set'
cd('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName/JDBCConnectionPoolParams/jdbc/csJndiName')
cmo.setTestTableName('SQL SELECT 1 FROM DUAL\r\n\r\n')

cd('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName/JDBCDriverParams/jdbc/csJndiName/Properties/jdbc/csJndiName')
cmo.createProperty('user')

cd('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName/JDBCDriverParams/jdbc/csJndiName/Properties/jdbc/csJndiName/Properties/user')
cmo.setValue(userId)

cd('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName/JDBCDataSourceParams/jdbc/csJndiName')
cmo.setGlobalTransactionsProtocol('TwoPhaseCommit')

cd('/SystemResources/jdbc/csJndiName')
# attaching the datasource to server/domain
set('Targets',jarray.array([ObjectName('com.bea:Name=AdminServer,Type=Server')], ObjectName))

save()
activate()
print 'Datasource jdbc/'+ codeSimplifyJndiName + ' is activated'
startEdit()
cd('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName/JDBCDriverParams/jdbc/csJndiName/Properties/jdbc/csJndiName')
cmo.destroyProperty(getMBean('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName/JDBCDriverParams/jdbc/csJndiName/Properties/jdbc/csJndiName/Properties/user'))
cmo.createProperty('user')

cd('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName/JDBCDriverParams/jdbc/csJndiName/Properties/jdbc/csJndiName/Properties/user')
cmo.setValue(userId)

cd('/JDBCSystemResources/jdbc/csJndiName/JDBCResource/jdbc/csJndiName/JDBCConnectionPoolParams/jdbc/csJndiName')
cmo.setTestConnectionsOnReserve(true)
cmo.setTestTableName('SQL SELECT 1 FROM DUAL\r\n')
save()
activate()

5) Start the data source

iDsName = 'jdbc/csJndiName'
startEdit()
cd('/JDBCSystemResources')
try:
	dataSources = cmo.getJDBCSystemResources()
	for dataSource in dataSources :
		dsName = dataSource.getName()
		if dsName == iDsName:
			dataSource.start()				
			print 'DataSource '+ iDsName + 'is deleted'
except:
	print 'Datasource might have been started already'
save()
activate()

If the above code is put together in the order mentioned, we could make a one click utility that creates the given data source and attach to running server.

How to Create Weblogic Datasource with one click

Leave a Reply

Your email address will not be published. Required fields are marked *