zoukankan      html  css  js  c++  java
  • 数据库读写分离

    1.应用层操作(默认有2个数据库,一个写数据库,一个读数据库)

      应用层操作,select操作就通过 读数据 操作,增删改操作通过 写数据库 操作。

    如果是用的Spring jdbc

    简单的实现就是new 2个JdbcTemplate。  在dao层引用这2个JdbcTemplate。分别做读写操作。

    但是我们要写的漂亮点,扩展点。

    这里有个org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource

    这个类有个属性Map<Object, Object> targetDataSources,可以把读写数据库设置这个map进去。

     当我们做操作的时候可以通过key去查找对应的数据库。key是我们做数据操作前时设置进去的(可以通过注解)。

    这个类有个抽象方法,

        /**
         * Determine the current lookup key. This will typically be
         * implemented to check a thread-bound transaction context.
         * <p>Allows for arbitrary keys. The returned key needs
         * to match the stored lookup key type, as resolved by the
         * {@link #resolveSpecifiedLookupKey} method.
         */
        protected abstract Object determineCurrentLookupKey();

    我们只需要实现这个方法就可以了。

    这篇文章写的很详细,一看就明白了。

    http://linhongyu.blog.51cto.com/6373370/1615895

    2.数据库实现读写分离

    ..........

  • 相关阅读:
    Ant构建原理及build.xml文档描述
    Selenium WebDriver的工作原理
    appium工作原理
    jmeter获取mysql数据并作为请求参数使用
    linux sar的使用
    hdu 1520 Anniversary party
    hdu 1331 Function Run Fun
    hdu 1208 Pascal's Travels
    hdu 1159 Common Subsequence
    poj 1129 Channel Allocation
  • 原文地址:https://www.cnblogs.com/shapeOfMyHeart/p/6528412.html
Copyright © 2011-2022 走看看