当前位置:web 服务器 > Tomcat >>

Tomcat配置多数据源

测试的tomcat为apache-tomcat-6.0.18 ,数据库为mysql和oracle。
配置步骤如下:
1、把数据库的JDBC驱动放入D:\apache-tomcat-6.0.18\lib目录下
2、在D:\apache-tomcat-6.0.18\conf\web.xml文件中,将下面代码加入到web.xml中:
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/mysql</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/oracle</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
3、在D:\apache-tomcat-6.0.18\conf\server.xml文件中,在Host节点下添加Context子节点,配置如下
        <Context path="/ljqtest" docBase="ljqtest" debug="5" reloadable="true" crossContext="true">
        <Resource name="jdbc/mysql"
            type="javax.sql.DataSource"
            username="root"
            password="mysql"
            driverClassName="org.gjt.mm.mysql.Driver"
            url="jdbc:mysql://localhost:3306/shop"
            maxIdle="2"
            maxWait="50"
            maxActive="4">
            <parameter>
                <name>removeAbandoned</name>
                <value>true</value>
            </parameter>
        </Resource>
        <Resource name="jdbc/oracle"
            type="javax.sql.DataSource"
            username="test"
            password="test"
            driverClassName="oracle.jdbc.driver.OracleDriver"
            url="jdbc:oracle:thin:@localhost:1521:ORCL"
            maxIdle="2"
            maxWait="50"
            maxActive="4">
            <parameter>
                <name>removeAbandoned</name>
                <value>true</value>
            </parameter>
        </Resource>
        </Context>
      </Host>
或者
        <Context path="/uimcardprj" docBase="uimcardprj" debug="5" reloadable="true" crossContext="true">     
             <Resource name="jdbc/ycxkDB"            
                 type="javax.sql.DataSource"           
                 username="ycxk"           
                 password="xmzh"           
                 driverClassName="oracle.jdbc.driver.OracleDriver"           
                 url="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 134.128.48.250)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = orcl)))"           
                 maxIdle="2"           
                 maxWait="50"           
                 maxActive="4">              
             </Resource>       
          </Context>
      </Host>
注意:path为D:\apache-tomcat-6.0.18\webapps目录下的工程名称
4、把web工程项目部署在D:\apache-tomcat-6.0.18\webapps目录下
MysqlConn类:获取Mysql数据源
package com.ljq.test;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public final class MysqlConn {
    // 懒汉式单例(使用时才new)
    private static MysqlConn instance = null;

    MysqlConn() {
    }

    // 延迟初始化(用到的时候才加载)(推荐)
    // public static synchronized JdbcConn
    // getInstance(){}->这样不好,因为每调用一次就同步,效率非常低
    public static MysqlConn getInstance() {补充:Web开发 , 其他 ,

Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
访问www.zzzyk.com 试试
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,