Tomcat配置多数据源

来源:互联网 发布:顾家床垫 知乎 编辑:程序博客网 时间:2024/05/16 09:22

测试的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 finalclass MysqlConn {
// 懒汉式单例(使用时才new)
privatestatic MysqlConn instance = null ;

MysqlConn() {
}

// 延迟初始化(用到的时候才加载)(推荐)
// public static synchronized JdbcConn
// getInstance(){}->这样不好,因为每调用一次就同步,效率非常低
publicstatic 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 finalclass OracleConn { // 懒汉式单例(使用时才new) privatestatic OracleConn instance = null ; OracleConn() { }// 延迟初始化(用到的时候才加载)(推荐)// public static synchronized JdbcConn// getInstance(){}->这样不好,因为每调用一次就同步,效率非常低publicstatic OracleConn getInstance() { if (instance == null ) { synchronized (OracleConn.class ) { // 可能会产生并发的问题,我们对他进行同步 if (instance == null ) { instance= newOracleConn(); } } } returninstance; } privateDataSource getDataSource() { DataSource ds =null ; try { Context ctx= newInitialContext(); ds =(DataSource) ctx.lookup( " java:comp/env/jdbc/mysql " ); } catch(Exception e) { System.out.println( " 数据源获取失败 "); e.printStackTrace(); } returnds; } publicConnection getConn() { Connection conn =null ; try { conn= getDataSource().getConnection(); }catch (SQLException e) { System.out.println(" 数据库连接失败 " ); e.printStackTrace(); } returnconn; } }

页面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