wlst创建GridLink数据源

config.ini

#普通数据源
admin_url=t3://localhost:7001
admin_username=weblogic
admin_password=weblogic1
db_username=demo
db_password=demo
db_url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=devrac-cluster-scan)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=DEVDB)))
#初始连接数
db_init_capacity=1
#最大连接数
db_max_capacity=50
datasource_name=DemoDS
jndi=jdbc/DemoDS
#数据源target多个用逗号,隔开
target=AdminServer

#RAC连接信息
ons=10.1.11.76:6200,10.1.11.77:6200

wlst_datasource.py

from java.io import FileInputStream
def admin_connect(config):
	username=config.get("admin_username")
	password=config.get("admin_password")
	url=config.get("admin_url")
	connect(username,password,url)
	
#创建GrikLink数据源(RAC数据源)		
def create_gridlink_datasource(config):
	try:
		admin_connect(config)
		
		dsName=config.get("datasource_name")
		jndi=config.get("jndi")
		
		dbUsername=config.get('db_username')
		dbPassword=config.get('db_password')
		dbURL=config.get('db_url')
		dbInitCapacity=config.get('db_init_capacity')
		dbMaxCapacity=config.get('db_max_capacity')
		targets=config.get('target').split(',')
		ons=config.get('ons');
		
		edit()
		cd("Servers/AdminServer")
		cd("../../")
		startEdit()
		log('正在创建数据源'+dsName)
		
		#创建数据源
		jdbc=create(dsName,"JDBCSystemResource")
		jdbcResource=jdbc.getJDBCResource()
		jdbcResource.setName(dsName)
		
		#设置数据源参数
		datasourceParams=jdbcResource.getJDBCDataSourceParams()
		datasourceParams.setJNDINames([jndi])
		datasourceParams.setGlobalTransactionsProtocol('OnePhaseCommit')
		
		#设置jdbc驱动信息
		driverParams=jdbcResource.getJDBCDriverParams()
		driverParams.setPassword(dbPassword)
		driverParams.setDriverName('oracle.jdbc.OracleDriver')
		driverParams.setUrl(dbURL)
		driverConfig=driverParams.getProperties()
		driverUser=driverConfig.createProperty('user')
		driverUser.setValue(dbUsername)
		
		#设置连接信息
		connectionParams=jdbcResource.getJDBCConnectionPoolParams()
		connectionParams.setInitialCapacity(int(dbInitCapacity))
		connectionParams.setMaxCapacity(int(dbMaxCapacity))
		connectionParams.setTestConnectionsOnReserve(true)
		connectionParams.setTestTableName('SQL SELECT 1 FROM DUAL')
		
		#设置RAC信息
		oracleParams=jdbcResource.getJDBCOracleParams();
		oracleParams.setFanEnabled(true);
		oracleParams.setOnsNodeList(ons)
		
		
		#设置目标target
		for target in targets:
			print 'Add To Target',target
			jdbc.addTarget(getMBean('Servers/'+target))
		save()
		activate(block="true")
		log('GridLink数据源创建成功'+dsName)
	except:
		log('GridLink数据源创建失败,请登录console释放配置(Release Configuration)后继续操作')
		print dumpStack()
		activate(block="true")
		
		
def delete_data_source(config,ds):
	admin_connect(config)
	log('删除数据源'+ds)
	edit()
	cd("Servers/AdminServer")
	cd("../../")
	startEdit()
	delete(ds,'JDBCSystemResource')
	save()
	stopEdit('true')
	activate(block="true")
	log('删除数据源成功'+ds)
	
def log(msg):
	print '*************************************'
	print '**    '+msg+'    **'
	print '*************************************'
		
def main():
	config = FileInputStream("config.ini")
	properties = util.Properties()
	properties.load(config)
	create_gridlink_datasource(properties);
	#create_generic_data_source(properties)
	#delete_data_source(properties,'TestDS3')
	disconnect('true')
	
main()
	

create_gridlink_datasource.cmd

cd /d %~dp0
wlst.cmd wlst_datasource.py

将wlst.cmd所在的目录($ORACLE_HOME/common/bin)加到环境变量中,在控制台执行运行脚本create_gridlink_datasource.cmd

如果要创建Generic类型数据源去掉JDBCOracleParams修改数据源连接字符串即可。

你可能感兴趣的:(weblogic)