-->

虫虫的技术博客 技术 生活

Thursday, May 31, 2012

Torque实现读写分离双数据库配置

由于有读写分离的需求,看了一下公司的代码是怎么处理的,才发现用的是很原始的方法,在做数据库操作的时候,直接用的不同的数据库连接。
现在用的torque,并且代码也写了一些,不想再在业务代码级别上做太多的改动,所以就想到torque来管理读写分离的工作。
了解了一下发现torque只有一个库的配置,没有考虑到读写分离的情况(也读是提供了我没找到。。。)
于是自己稍微看了一下源代码,加上了读写分离数据库的配置,让torque在执行doSelect(Criteria criteria)方法的时候会去找配置的只读库
改动比较少
进入正题:
基于TORQUE-runtime-3.3版本
http://svn.apache.org/repos/asf/db/torque/runtime/tags/TORQUE_3_3
可以从以上地址下载到torque的3.3版源代码
首先是org.apache.torque.util.BasePeer类
直接进入doSelect方法
原代码:
con = Transaction.beginOptional( criteria.getDbName(), criteria.isUseTransaction());
改为:
con = Transaction.beginOptional( Torque.getReadableDB(), criteria.isUseTransaction());
这里先把方法写出来,后面再去实现.
继续org.apache.torque.Torque类
增加属性声明
/**
     * The key used to configure the name of the readable database.
     * jiangchi@youku.com add
     */
    public static final String READABLE_KEY = "readable";
增加方法
/**
     * Returns the name of the readable database.
     *
     * @return name of the readable DB, or null if Torque is not initialized yet
     */
    public static String getReadableDB()
    {
        return getInstance().getReadableDB();
    }
一些注释和代码的写法是为了代码符合原来的规范样式等
然后是org.apache.torque.TorqueInstance类
/** The db name that is specified as the readable in the property file */
    private String readableDBName = null;

initDefaultDbName(conf);
代码的后面加上
initReadableDbName(conf);
实现方法
/**
     * Initializes the name of the readable database and
     * associates the database with the name DEFAULT_NAME * to the default database. * * By the way the name will be used to do select operate * * @param conf the configuration representing the torque section. * of the properties file. * @throws TorqueException if the appropriate key is not set. * * jiangchi@youku.com add */ private void initReadableDbName(Configuration conf) throws TorqueException { readableDBName = conf.getString( Torque.DATABASE_KEY + "." + Torque.READABLE_KEY); if (readableDBName == null) { String debuginfo = "Invalid configuration: Key " + Torque.TORQUE_KEY + "." + Torque.DATABASE_KEY + "." + Torque.READABLE_KEY + " not set"; log.debug(debuginfo); // throw new TorqueException(error); if (defaultDBName == null) { String error = "Invalid configuration: Key " + Torque.TORQUE_KEY + "." + Torque.DATABASE_KEY + "." + Torque.DEFAULT_KEY + " not set"; log.error(error); throw new TorqueException(error); } else { readableDBName = defaultDBName; log.debug("use default database"); } } }
/**
       * Returns the name of the readable database.
       *
     * @return name of the readable DB, or null if Torque is not initialized yet
     *
       * @author JiangChi
       * jiangchi@youku.com add
       */
       public String getReadableDB() {
             return readableDBName;
      }
因为代码是前些天改的,今天才测试跑了一遍,所以之前的看代码的顺序思路这里也就没有了,直接上改后的代码
最后部署打包
用maven clean package
报错,test通不过,改了一会还是有问题,一直不太熟junit,所以就索性将test代码注释掉
打包通过放到公司的maven仓库上(注意打包的时候别跟原来的名称冲突,我改了artifactId)
使用的时候加一个readable配置就OK了
example:
#原来的配置
torque.database.default=epg
torque.database.epg.adapter=mysql
torque.dsfactory.epg.factory=org.apache.torque.dsfactory.SharedPoolDataSourceFactory
torque.dsfactory.epg.pool.maxIdle=8
torque.dsfactory.epg.pool.maxActive=10
torque.dsfactory.epg.pool.testOnBorrow=true
torque.dsfactory.epg.pool.validationQuery=SELECT 1
torque.dsfactory.epg.connection.driver = org.gjt.mm.mysql.Driver
datasourcewrite.user = jctest
datasourcewrite.password = abcd
datasourcewrite.url = jdbc:mysql://localhost:33061/epg
torque.dsfactory.epg.connection.url = ${datasourcewrite.url}?useUnicode=true&characterEncoding=UTF-8
torque.dsfactory.epg.connection.user = ${datasourcewrite.user}
torque.dsfactory.epg.connection.password = ${datasourcewrite.password}
#再加如下配置
torque.database.readable=epgreadable
torque.database.epgreadable.adapter=mysql
torque.dsfactory.epgreadable.factory=org.apache.torque.dsfactory.SharedPoolDataSourceFactory
torque.dsfactory.epgreadable.pool.maxIdle=8
torque.dsfactory.epgreadable.pool.maxActive=10
torque.dsfactory.epgreadable.pool.testOnBorrow=true
torque.dsfactory.epgreadable.pool.validationQuery=SELECT 1
torque.dsfactory.epgreadable.connection.driver = org.gjt.mm.mysql.Driver
datasourceread.user = jctest
datasourceread.password = abcd
datasourceread.url = jdbc:mysql://localhost:33061/epg
torque.dsfactory.epgreadable.connection.url = ${datasourceread.url}?useUnicode=true&characterEncoding=UTF-8
torque.dsfactory.epgreadable.connection.user = ${datasourceread.user}
torque.dsfactory.epgreadable.connection.password = ${datasourceread.password}
由于目的是为了实现具体的需求,肯定会有很多地方想得不周到的地方,希望以后发现再完善吧。

0 comments:

Post a Comment

Popular Posts

Copyright © 虫虫的成长历程 | Powered by Blogger Design by PWT | Blogger Theme by NewBloggerThemes.com