博文

目前显示的是 五月, 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 ini...