hibernate最新版本(3.3)是否支持如下需求:
后台有多个物理数据库, master database 和 slave database1, slave database2, .....
数据库服务器自身负责完成master database和slave databases的数据同步。
我的问题是, 能否定义多个数据源, 通过配置或简单编码hibernate实现将所有的insert, update, delete等改变数据的操作都在master database中完成, 所有select操作分配在slave databases中完成。
[b]问题补充:[/b]
我想将单数据库系统升级为Master-Slave模式(Master写,Slave读)。
原系统简介:使用Struts+Hibernate架构;数据库连接池。
计划使用ReplicationDriver,考虑了一下。大致需要做以下三处改动。
1 用新的驱动程序替换旧驱动程序
2 修改连接池配置文件如下
3 修改DAO, 增加如下代码
session.connection().setReadOnly(true/false);
不知是否考虑完全,有哪些更好的建议?
[b]问题补充:[/b]
补充问题:
ReplicationDriver对MySQL数据库的版本有要求吗? 我理解应该是没有要求。
ReplicationDriver定义Master-Slave服务器就是通过排列顺序是吗? 排在第一的是Master,从第二个开始是Slave?
[b]问题补充:[/b]
做了简单测试,数据写入Master没有问题, 但读出数据为什么是从Master,而不是Slave。
连接池配置文件见第一次问题补充
CustomerDAO 测试代码如下:
[code="java"]
public class CustomerDAO {
private String DTO_NAME = "CustomerDTO";
public List find() {
List result = null;
try {
final Session session = HibernateUtil.currentSession();
session.connection().setReadOnly(true);
result = session.createCriteria(CustomerDTO.class).add(Restrictions.eq("deleted", false)).add(Restrictions.eq("disabled", false)).list();
} catch (HibernateException e) {
e.printStackTrace();
} catch (SQLException se) {
se.printStackTrace();
}
return result;
}
public void add(final CustomerDTO obj) {
try {
final Session session = HibernateUtil.currentSession();
session.connection().setReadOnly(false);
obj.setCreationDate(new Date());
obj.setLastEditDate(new Date());
session.save(obj);
} catch (HibernateException e) {
e.printStackTrace();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
[/code]
HibernateUtil部分测试代码如下:
[code="java"]
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static final String HIBERNATE_CONF_FILE = "hibernate.cfg.xml";
public static final ThreadLocal session = new ThreadLocal();
public static final ThreadLocal userTran = new ThreadLocal();
public static String dialect;
static {
try {
sessionFactory = getHibernateConfiguration().buildSessionFactory();
} catch (Exception ex) {
}
}
public static Configuration getHibernateConfiguration() {
Configuration config = new Configuration();
try {
String path = null;
path = "/hibernate_mapping/" + HIBERNATE_CONF_FILE;
dialect = "org.hibernate.dialect." + "MySQL" + "Dialect";
final Properties hibProperties = new Properties();
hibProperties.setProperty(Environment.DIALECT, dialect);
config.configure(path).addProperties(hibProperties);
} catch (Exception ex) {
}
return config;
}
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
s.setFlushMode(FlushMode.AUTO);
return s;
}
}
[/code]
请帮忙看一下问题所在.