[转帖]WebLogic配置连接池的解决方案_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3872 | 回复: 0   主题: [转帖]WebLogic配置连接池的解决方案        下一篇 
white
注册用户
等级:少校
经验:1327
发帖:305
精华:0
注册:2011-7-21
状态:离线
发送短消息息给white 加好友    发送短消息息给white 发消息
发表于: IP:您无权察看 2011-8-16 9:18:59 | [全部帖] [楼主帖] 楼主

一般配置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的操作(上面的代码有体现),这样可以大大提高永久性配置的可能性。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论