Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:oracle 9i 如何在tomcat中配置连接池? [Re:mazalet]
felexs





发贴: 80
积分: 55
于 2003-08-14 13:38 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
思路参考了wrox 的《Professional Java Servlets 2.3 》
中文版为:《Java servlet 2.3编程指南》

我没法在这个贴子里加附件,代码:
package persistence;

import java.io.InputStream;
import javax.servlet.ServletContext;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;

public abstract class Config {

protected Element root;

protected void init(ServletContext sctx, String xmlFile) throws Exception {
InputStream is = null;

// Obtain the root element.
try {
is = sctx.getResourceAsStream(xmlFile);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(is));
root = doc.getDocumentElement();
} finally {
if (is != null) {
is.close();
}
}
}

protected void cleanup() {
root = null;
}

protected String getElementText(Element parent, String name) {
NodeList nodeList = parent.getElementsByTagName(name);
if (nodeList.getLength() == 0) {
return null;
}

Element element = (Element) nodeList.item(0);
StringBuffer sb = new StringBuffer();
for (Node child = element.getFirstChild();
child != null; child = child.getNextSibling()) {

if (child.getNodeType() == Node.TEXT_NODE) {
sb.append(child.getNodeValue());
}
}
return sb.toString().trim();
}

}

package persistence.database;

import persistence.Config;
import javax.servlet.ServletContext;
import javax.sql.DataSource;

public abstract class DataSourceConfig extends Config {

private static final String DATABASE_USER = "DatabaseUser";
private static final String DATABASE_PASSWORD = "DatabasePassword";
private static final String SERVER_NAME = "ServerName";
private static final String DATABASE_NAME = "DatabaseName";
private static final String SERVER_PORT = "ServerPort";

protected DataSource ds;
protected String databaseUser;
protected String databasePassword;
protected String serverName;
protected String portNumber;
protected String databaseName;

public void init(ServletContext sctx, String xmlFile) throws Exception {

super.init(sctx, xmlFile);

// Read URI properties.
databaseUser = getElementText(root, DATABASE_USER);
databasePassword = getElementText(root, DATABASE_PASSWORD);
databaseName = getElementText(root, DATABASE_NAME);
serverName = getElementText(root, SERVER_NAME);
portNumber = getElementText(root, SERVER_PORT);
}

public DataSource getDataSource() {
return ds;
}
}

package persistence.database.oracle;

import persistence.database.DataSourceConfig;
import javax.servlet.ServletContext;
//import javax.sql.DataSource;
import oracle.jdbc.pool.OracleConnectionCacheImpl;

public class OracleConfig extends DataSourceConfig {
  private static final String MAX_CONNECTIONS = "MaxConnections";
  
  public void init(ServletContext ctx, String xmlFile) throws Exception {
    
    super.init(ctx, xmlFile);
    
    String databaseURL = "jdbc:oracle:thin@" + serverName + ":" +
              portNumber + ":" + databaseName;
    ds = new OracleConnectionCacheImpl();
    ((OracleConnectionCacheImpl)ds).setURL(databaseURL);
    ((OracleConnectionCacheImpl)ds).setUser(databaseUser);
    ((OracleConnectionCacheImpl)ds).setPassword(databasePassword);
    
    try{
      int maxConnections = Integer.parseInt(
                getElementText(root, MAX_CONNECTIONS));
      ((OracleConnectionCacheImpl)ds).setMaxStatements(maxConnections);
    } catch(NumberFormatException n) {
      //
    }
    
    cleanup();
  }
}

datasource-config.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>

<DataSource>
<DatabaseUser>sms</DatabaseUser>
<DatabasePassword>sms</DatabasePassword>
<DatabaseName>ittest</DatabaseName>
<ServerName>10.38.8.20</ServerName>
<ServerPort>1521</ServerPort>
<MaxConnections>10</MaxConnections>
</DataSource>




Eclipse使用技巧

话题树型展开
人气 标题 作者 字数 发贴时间
9967 oracle 9i 如何在tomcat中配置连接池? mazalet 39 2003-08-13 13:58
10110 Re:oracle 9i 如何在tomcat中配置连接池? dissip 96 2003-08-13 16:04
9541 Re:oracle 9i 如何在tomcat中配置连接池2? dissip 96 2003-08-13 16:06
8872 Re:oracle 9i 如何在tomcat中配置连接池? 256456 757 2003-08-13 20:35
9430 更灵活的方法//Re:oracle 9i 如何在tomcat中配置连接池? felexs 320 2003-08-14 08:59
8924 Re:更灵活的方法//Re:oracle 9i 如何在tomcat中配置连接池? mazalet 395 2003-08-14 10:12
9421 Re:oracle 9i 如何在tomcat中配置连接池? felexs 4176 2003-08-14 13:38
9355 Re:oracle 9i 如何在tomcat中配置连接池? felexs 19 2003-08-29 09:44

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923