`
wangcheng
  • 浏览: 1454634 次
  • 性别: Icon_minigender_1
  • 来自: 青岛人在北京
社区版块
存档分类
最新评论

Tomcat 配置多数据源

    博客分类:
  • java
阅读更多

前几天有朋友问Tomcat 中配置多数据源,做了个小例子。

数据源: 本机MySQL的2个数据库 mydb1, mydb2
先在Eclipse中建立一个Web项目, 我的叫MyWebApp

在apache-tomcat-5.5.17\conf\Catalina\localhost中,建立名为MyWebApp.xml的文件,在其中指明项目路径和数据源配置,内容如下:

xml 代码
  1. <Context path="/" docBase="D:/workspaceWTP1.5/MyWebApp/web" debug="99" reloadable="true">  
  2.     <Logger className="org.apache.catalina.logger.FileLogger" prefix="dvs." suffix=".txt" timestamp="true"/>  
  3.     <Resource  
  4.     name="jdbc/mydb1"  
  5.     type="javax.sql.DataSource"  
  6.     driverClassName="com.mysql.jdbc.Driver"  
  7.     maxIdle="30"  
  8.     maxWait="10000"  
  9.     username=""  
  10.     password=""  
  11.     url="jdbc:mysql://localhost/mydb1?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8"  
  12.     maxActive="100"/>  
  13.        
  14.     <Resource  
  15.     name="jdbc/mydb2"  
  16.     type="javax.sql.DataSource"  
  17.     driverClassName="com.mysql.jdbc.Driver"  
  18.     maxIdle="30"  
  19.     maxWait="10000"  
  20.     username=""  
  21.     password=""  
  22.     url="jdbc:mysql://localhost/mydb2?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8"  
  23.     maxActive="100"/>  
  24. </Context>  

然后在MyWebApp项目的WEB-INF/web.xml 中引用这两个数据源

xml 代码
  1. <resource-env-ref>  
  2.     <description>myDB1 Connection</description>  
  3.     <resource-env-ref-name>jdbc/mydb1</resource-env-ref-name>  
  4.     <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>  
  5. </resource-env-ref>  
  6.   
  7. <resource-env-ref>  
  8.     <description>myDB2 Connection</description>  
  9.     <resource-env-ref-name>jdbc/mydb2</resource-env-ref-name>  
  10.     <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>  
  11. </resource-env-ref>  

这样就可以在web应用中使用这两个数据源了,可以写个简单的servlet测试

java 代码
  1. public static final String namespace = "java:comp/env";   
  2.   
  3. public DataSource getDataSource(String resourceName) throws NamingException {   
  4.     InitialContext init = new InitialContext();   
  5.     Context ctx = (Context)init.lookup(namespace);   
  6.     return (DataSource)ctx.lookup(resourceName);   
  7. }   
  8.   
  9. private void doService(HttpServletRequest request, HttpServletResponse response) throws IOException, NamingException, SQLException {   
  10.     DataSource ds1 = getDataSource("jdbc/mydb1");   
  11.     DataSource ds2 = getDataSource("jdbc/mydb2");   
  12.        
  13.     response.getWriter().println("DataSource ds1 is : " + ds1);   
  14.     response.getWriter().println("DataSource ds2 is : " + ds2);   
  15.        
  16.     Connection conn1 = ds1.getConnection();   
  17.     String sql1 = "select email from users where id = 15";   
  18.     response.getWriter().println("email is : " + executeQuery(conn1, sql1));   
  19.   
  20.     Connection conn2 = ds2.getConnection();   
  21.     String sql2 = "select product_name from products where id = 1";   
  22.     response.getWriter().println("Product Name is : " + executeQuery(conn2, sql2));   
  23. }  

其中executeQuery方法就是简单的数据库查询,这里就不详细写了.

如果使用Hibernate也很简单,只要在两个不同的hibernate.cfg.xml文件中分别指定不同的hibernate.connection.datasource就可以了.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics