WebLogic配置连接池(通过mbean)

本文介绍了如何通过编程方式配置WebLogic的连接池,避免通过WebLogic Console界面配置时可能出现的异常。通过JDBCConnectionPoolMBean和JDBCDataSourceMBean进行配置,提供了一种适用于WebLogic 6, 7及8.1版本的方法。文中还提到了配置后的连接测试,以确保配置生效。" 133659305,19671472,构建Android UI控件数据模型,"['Android开发', 'UI设计', '数据结构', 'Android应用']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WebLogic配置连接池(通过mbean)

有关WebLogic配置连接池比较好的解决方案

一般配置weblgoic连接池(或DataSource)都是通过weblogic console界面来配置,通常情况下,因为很多原因,比如类的加载,driver的错误,url的错误,以及其他属性不正确等等原因,而造成配置抛出mbean的异常信息。

这类问题,在本论坛上出现了很多次,而精华区也有介绍。但是依然不断的出现。一方面是这个问题确实很容易出现,一方面就应该从我们自身找找原因了。

配置weblogic connection pool or datasource未必只可以通过console界面。
接下来,给大家一个比较可行的方式。这种方式在weblogic6,7系列内都是非常好用的,对于weblogic8.1,我想也应该可以。
方法:就是手工通过coding操作,weblogic.management.configuration.JDBCConnectionPoolMBean或者 weblogic.management.configuration.JDBCDataSourceMBeanl类。

可能有些人会说这样写代码不是更复杂,麻烦吗?
那么,可以让这个麻烦只出现一次,也是值得的。
我就用Swing写了一个GUI界面,整合了一些配置信息(结构有些复杂,但是比较容易操作,比直接通过console要方便。主要当时为公司项目写的,便于以后项目中操作)。
仅针对connection pool配置这一块。主要代码大致如下:

public void deployServerDataSource(){

try {
ctx = getInitialContext();
if(ctx!=null){
this.txtareaDeployStatus.append(/r/n+_INFO : 连接服务器,并登陆成功);
this.txtareaDeployStatus.append(/r/n+_INFO : 获取管理对象);
//getting the Administration MBeanHome
mbeanHome = (MBeanHome)ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
this.txtareaDeployStatus.append(/r/n+_INFO : 获取Admin Server);
serverMBean = (ServerMBean)mbeanHome.getAdminMBean(serverName, Server);
this.txtareaDeployStatus.append(/r/n+_INFO : 获取domain);
domainName = mbeanHome.getDomainName();
}else{
this.txtareaDeployStatus.append(/r/n+_INFO : 连接服务器失败,请服务器正确启动,并且登陆正确);
return;
}
}
catch (Exception ex) {
this.txtareaDeployStatus.append(/r/n+_ERROR: 初始化管理服务器信息失败,配置终止);
return;
}

try{
this.txtareaDeployStatus.append(/r/n+_INFO : 开始配置WebLogic DataSource of RiseNet);
config();
this.txtareaDeployStatus.append(/r/n+_INFO : 配置WebLogic DataSource of RiseNet成功 );
JOptionPane.showMessageDialog(this, 配置WebLogic DataSource of RiseNet成功, null, JOptionPane.INFORMATION_MESSAGE);
}catch(Exception ex){
this.txtareaDeployStatus.append(/r/n+_ERROR: 配置WebLogic DataSource of RiseNet失败 );
JOptionPane.showMessageDialog(this, 配置WebLogic DataSource of RiseNet失败, 错误, JOptionPane.ERROR_MESSAGE);
}

//需要在配置完以后,进行一次连接测试。
try{
javax.sql.DataSource ds = null;
ds = (javax.sql.DataSource)ctx.lookup(cpDataSourceJNDIName);
java.sql.Connection c = ds.getConnection();
c.close();
}catch(Exception ex){
this.txtareaDeployStatus.append(/r/n+_ERROR: 测试失败 );
this.txtareaDeployStatus.append(/r/n+_ERROR: 请手动进行Apply一次,否则配置DataSource在服务器重新启动后失效 );
JOptionPane.showMessageDialog(this, 测试失败, 错误, JOptionPane.ERROR_MESSAGE);
}

}

public void config() throws Exception{

cpPoolName = this.txtPoolName.getText();
cpDataSourceName = this.txtDataSourceName.getText();
cpDataSourceJNDIName = this.txtDataSourceJNDIName.getText();

//将原有的连接DataSource信息删除
deleteDataSource();

try {
createDataSource();
}
catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}

public void createDataSource() throws Exception {
/**
* WebLogicObjectName的构造起的参数如下:
* java.lang.String name 名称
* java.lang.String type 类型
* java.lang.String domain 域
* java.lang.String location
* WebLogicObjectName parent 父节点
*/
/*
WebLogicObjectName pname = new WebLogicObjectName(server1, ServerRuntime, domainName,server1);
WebLogicObjectName oname = new WebLogicObjectName(cpName, JDBCConnectionPoolRuntime, domainName,server1, pname);
JDBCConnectionPoolRuntimeMBean cprmb = (JDBCConnectionPoolRuntimeMBean)mbeanHome.getMBean(oname);
*/
Vector vct = RiseNet.getDefaultDataSource();
String jdbc_user = (String)vct.get(RiseContent.HASHMAP_JDBC_USERNAME);
String jdbc_url = (String)vct.get(RiseContent.HASHMAP_JDBC_URL);
String jdbc_pass = (String)vct.get(RiseContent.HASHMAP_JDBC_PASSWROD);
String jdbc_driver = (String)vct.get(RiseContent.HASHMAP_JDBC_DRIVER_NAME);
String t_Properties = (String)vct.get(RiseContent.HASHMAP_JDBC_PROPERTIES);
t_Properties = ConfigUtil.convertEnter(t_Properties);
Properties pros = new Properties();
pros.put(user, jdbc_user);

String[] t_PropertiesArr = null;
if(!(t_Properties==null || t_Properties.equals())){
t_PropertiesArr = StrUtil.separateDateStr(t_Properties,;);
}
if(t_PropertiesArr!=null && t_PropertiesArr.length>0){
for(int i=0;i<t_PropertiesArr.length;i++){
String[] tmpArr = StrUtil.separateDateStr(t_PropertiesArr[i],=);
pros.put(tmpArr[0],tmpArr[1]);
}
}

// Create ConnectionPool MBean
JDBCConnectionPoolMBean cpMBean = (JDBCConnectionPoolMBean)mbeanHome.createAdminMBean(cpPoolName, JDBCConnectionPool, domainName);

cpMBean.setProperties(pros);
cpMBean.setURL(jdbc_url);
cpMBean.setDriverName(jdbc_driver);
cpMBean.setPassword(jdbc_pass);
cpMBean.setLoginDelaySeconds(1);
cpMBean.setInitialCapacity(0);
cpMBean.setMaxCapacity(5);
cpMBean.setCapacityIncrement(5);
cpMBean.setShrinkingEnabled(true);
cpMBean.setShrinkPeriodMinutes(10);
//cpMBean.setRefreshMinutes(10); //空闲10分钟后,即测试连接
//cpMBean.setTestTableName(dual);
//cpMBean.setACLName(dynapool);
cpMBean.addTarget(serverMBean);
cpMBean.setPersistenceEnabled(true);

JDBCDataSourceMBean dsMBeans = (JDBCDataSourceMBean)mbeanHome.createAdminMBean(cpDataSourceName, JDBCDataSource, domainName);
dsMBeans.setJNDIName(cpDataSourceJNDIName);
dsMBeans.setPoolName(cpPoolName);
dsMBeans.addTarget(serverMBean);
dsMBeans.setPersistenceEnabled(true);

}

请注意,直接通过程序修改,不可能很快的响应到config.xml中。当前的配置信息在weblogic console已经可以显示,但是如果不对其操作(apply),或者等待一段时间。那么所有的修改将在下一次server启动的时候不再存在。
为解决上面的情况,我在配置完后,硬性的采取了创建connection的操作(上面的代码有体现),这样可以大大提高永久性配置的可能性。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值