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.数据库实现读写分离

    ..........

  • 相关阅读:
    【题解】Candle
    【题解】购物
    【题解】论逼格
    浅谈前缀和
    浅谈 LCA
    浅谈Meet in the middle——MITM
    拓扑排序
    时间复杂度符号
    浅谈排序算法[动图]
    404 页面不存在
  • 原文地址:https://www.cnblogs.com/shapeOfMyHeart/p/6528412.html
Copyright © 2011-2022 走看看