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() {
if (instance == null ) {
synchronized (MysqlConn. class ) { // 可能会产生并发的问题,我们对他进行同步
if (instance == null ) {
instance
= new MysqlConn();
}
}
}
return instance;
}

private DataSource getDataSource() {
DataSource ds
= null ;
try {
Context ctx
= new InitialContext();
ds
= (DataSource) ctx.lookup( " java:comp/env/jdbc/mysql " );
}
catch (Exception e) {
System.out.println(
" 数据源获取失败 " );
e.printStackTrace();
}
return ds;
}

public Connection getConn() {
Connection conn
= null ;
try {
conn
= getDataSource().getConnection();
}
catch (SQLException e) {
System.out.println(
" 数据库连接失败 " );
e.printStackTrace();
}
return conn;
}

}
复制代码

OraclelConn类:获取Oracle数据源

复制代码
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 OracleConn {
// 懒汉式单例(使用时才new)
private static OracleConn instance = null ;

OracleConn() {
}

// 延迟初始化(用到的时候才加载)(推荐)
// public static synchronized JdbcConn
// getInstance(){}->这样不好,因为每调用一次就同步,效率非常低
public static OracleConn getInstance() {
if (instance == null ) {
synchronized (OracleConn. class ) { // 可能会产生并发的问题,我们对他进行同步
if (instance == null ) {
instance
= new OracleConn();
}
}
}
return instance;
}

private DataSource getDataSource() {
DataSource ds
= null ;
try {
Context ctx
= new InitialContext();
ds
= (DataSource) ctx.lookup( " java:comp/env/jdbc/mysql " );
}
catch (Exception e) {
System.out.println(
" 数据源获取失败 " );
e.printStackTrace();
}
return ds;
}

public Connection getConn() {
Connection conn
= null ;
try {
conn
= getDataSource().getConnection();
}
catch (SQLException e) {
System.out.println(
" 数据库连接失败 " );
e.printStackTrace();
}
return conn;
}

}
复制代码

页面index.jsp:打印数据库连接对象

< body >
mysql连接对象为:
<% Connection conn = MysqlConn.getInstance().getConn(); %><%= conn %><% conn.close(); %>< br />
oracle连接对象为:
<% Connection conn2 = MysqlConn.getInstance().getConn(); %><%= conn2 %><% conn2.close(); %>< br />
</ body >


5、启动tomcat,在浏览器中输入:http://localhost:8083/ljqtest/,输出如下:

猜你喜欢

转载自javatea.iteye.com/blog/2084299