如何?分散式資料庫,實現分散式資料庫

來源:互聯網
上載者:User

如何?分散式資料庫,實現分散式資料庫

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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.