zoukankan      html  css  js  c++  java
  • 如何实现分布式数据库

    1.前言:

           我们知道随着用户数量的增长,应用软件的规模会逐步增大,同时也使应用的服务压力愈发大。而首当其冲的无非是数据库操作资源...


    2.技术方案:

          这里采用jndi技术实现分库读写(mysql和oracle数据库集成)


    3.具体实现:

        a.配置tomcat的server.xml

       <Context>
           <Resource name="jdbc/mysql" auth="Container" 
                     type="javax.sql.DataSource"                                              
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/test" username="admin" password="admin“ maxActive="100" maxIdle="30" maxWait="10000” /> <Resource name="jdbc/orcl" auth="Container" type="javax.sql.DataSource"
    driverClassName="oracle.jdbc.OracleDriver"
    url="jdbc:oracle:thin:@127.0.0.2:1521:orcl" username="trigger" password="trigger" maxActive="50" maxIdle="10" maxWait="10000"/> </Context>

       b.配置项目的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/orcl</res-ref-name>
                         <res-type>javax.sql.DataSource</res-type>
                        <res-auth>Container</res-auth>
                 </resource-ref>

        *注意: <res-ref-name>jdbc/mysql</res-ref-name>的值必须匹配 <Resource name="jdbc/mysql" auth="Container" ...的name,否则会出现错误

        c.如何初始化资源链接:

           Context initContext = new InitialContext();
           Context envContext  = (Context)initContext.lookup("java:/comp/env");
           DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
           Connection conn = ds.getConnection();

    4.总结:

            配置服务器(tomcat) ==》配置项目的web.xml ==》在项目中引入数据库驱动包 ==》获取数据库连接


      5.分享和交流:

          如有纰漏!望各位看客斧正,谢谢


      6.引用资源:

        JNDI 在 J2EE 中的角色(http://www.ibm.com/developerworks/cn/java/j-jndi/)

       JNDI Datasource HOW-TO(https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#Introduction)

  • 相关阅读:
    React在componentDidMount里面发送请求
    React 术语词汇表
    React里受控与非受控组件
    React和Vue等框架什么时候操作DOM
    【LeetCode】79. Word Search
    【LeetCode】91. Decode Ways
    【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
    【LeetCode】1. Two Sum
    【LeetCode】141. Linked List Cycle (2 solutions)
    【LeetCode】120. Triangle (3 solutions)
  • 原文地址:https://www.cnblogs.com/imaikce/p/6055928.html
Copyright © 2011-2022 走看看