有个JAVA WEB工程,准备使用 Struts,Spring,hibernate ,【不是网站】,已经使用了Memcached缓存经常访问的页面了,但是还想用MYsql主从库减轻 数据库访问压力,方案如下:------------------------
只有一台主服务器,只负责更新数据的操作,不处理查询操作。
 一台或多台从服务器,只负责处理查询,不负责更新数据。
------------------------意思就是:【主SERVER更新数据,从SERVER查询数据,是分开的】。那么现在【问题】是:1.在SSH框架下,在代码方面,在配置文件方面,怎么实现 insert或者update 这些更新都放到主Server,而 select这些都放到从Server那去呢?hibernate 一般都只有一个连接啊。2.工程能不能只建立一个连接,连到主Server,由mysql自己分配,更新操作到主Server,查询操作到从Server呢?mysql有这机制吗?关于问题1,问过一些人,说是主Server和从Server都要负责查询的,但主Server只负责更新,跟我们的要求不太一样。在代码方面,说是建立两个或多个连接(主的至少一个,N个从的),注入到DAO里,然后具体insert还是select时再使用不同的连接,这样可行吗?各位帮我瞧瞧,谢谢哈~

解决方案 »

  1.   

    try mysql proxy
    http://dev.mysql.com/doc/refman/5.1/en/mysql-proxy.html估计您需要自己修改lua读写分离脚本(默认脚本包括master的读,而您不要读)
      

  2.   

    hibernate是可以建立多个连接的,你工程没必要建立两个阿,在处理的时候可以根据更新或是查询来请求不同的hibernate连接。写两个hibernate.properties,例如连接oraA的叫做hibernateA.properties,连接oraB的叫做hibernateB.properties。   hibernateA.properties内容如下:   引用:   
    hibernate.dialect net.sf.hibernate.dialect.OracleDialect   
    hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver   
    hibernate.connection.username username   
    hibernate.connection.password password   
    hibernate.connection.url jdbc:oracle:thin:@local:1521:oraA     hibernateB.properties内容如下:   引用:   
    hibernate.dialect net.sf.hibernate.dialect.OracleDialect   
    hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver   
    hibernate.connection.username usernamet   
    hibernate.connection.password passwordt   
    hibernate.connection.url jdbc:oracle:thin:@local:1521:oraB     程序里面:   代码:   
    java代码:     Configuration conf_oraA = new Configuration(”/hibernateA.properties”).addClass()…….;   Configuration conf_oraB = new Configuration(”/hibernateB.properties”).addClass()…….;   SessionFactory sf_oraA = conf_oraA.buildSessionFactory();   
    SessionFactory sf_oraB = conf_oraB.buildSessionFactory();   Session s_oraA = sf_oraA.openSession();   
    Session s_oraB = sf_oraB.openSession();