hibernate shards分库的实现

来源:互联网 发布:乐高机器人的所有编程 编辑:程序博客网 时间:2024/05/21 22:53

网上资料良莠不齐。终于找到   

如http://docs.jboss.org/hibernate/shards/3.0/reference/en/html_single/中介绍的。


public static SessionFactory createSessionFactory() {

   Configuration prototypeConfig = new Configuration().configure("hibernate1.xml");
   prototypeConfig.addResource("com/ha/entity/Student.hbm.xml"); //mapping映射
   prototypeConfig.addResource("com/ha/entity/Teacher.hbm.xml"); //mapping映射
   List<ShardConfiguration> shardConfigs = new ArrayList<ShardConfiguration>();
   shardConfigs.add(buildShardConfig("hibernate2.xml"));  //添加数据库的信息
   ShardStrategyFactory shardStrategyFactory = buildShardStrategyFactory();
   ShardedConfiguration shardedConfig = new ShardedConfiguration(
       prototypeConfig,
       shardConfigs,
       shardStrategyFactory);
   return shardedConfig.buildShardedSessionFactory();
}


static ShardStrategyFactory buildShardStrategyFactory() {
   ShardStrategyFactory shardStrategyFactory = new ShardStrategyFactory() {
       public ShardStrategy newShardStrategy(List<ShardId> shardIds) {
           RoundRobinShardLoadBalancer loadBalancer = new RoundRobinShardLoadBalancer(shardIds);
           ShardSelectionStrategy pss = new RoundRobinShardSelectionStrategy(loadBalancer);
           ShardResolutionStrategy prs = new AllShardsShardResolutionStrategy(shardIds);
           ShardAccessStrategy pas = new SequentialShardAccessStrategy();
           return new ShardStrategyImpl(pss, prs, pas);
       }
   };
   return shardStrategyFactory;
}


static ShardConfiguration buildShardConfig(String configFile) {
   Configuration config = new Configuration().configure(configFile);
   return new ConfigurationToShardConfigurationAdapter(config);

}

  简单的注意一下hibernate1.xml、hibernate2.xml只有数据库连接的信息,没有<mapping resource="com/ha/entity/Student.hbm.xml" />这种要注释,因为自动根据shardConfigs去找。

   hibernate1.xml:

<hibernate-configuration>


<session-factory   name="HibernateSessionFactory1" >
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/new
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hbm2ddl.auto">validate</property>
<property name="show_sql">true</property>

<property name="hibernate.connection.shard_id">0</property>
    <property name="hibernate.shard.enable_cross_shard_relationship_checks">
      true
    </property>


<!-- <mapping resource="com/ha/entity/Student.hbm.xml" /> -->
    
</session-factory>

</hibernate-configuration>


hibernate2.xml:

<hibernate-configuration>
<session-factory name="HibernateSessionFactory" >
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3307/dome
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hbm2ddl.auto">validate</property>
<property name="show_sql">true</property>

<property name="hibernate.connection.shard_id">6</property>
    <property name="hibernate.shard.enable_cross_shard_relationship_checks">
      true
    </property>   
    
   <!--  <mapping resource="com/ha/entity/Teacher.hbm.xml" /> -->

</session-factory>
</hibernate-configuration>


搞定了。哈哈。

测试:

   SessionFactory sf=createSessionFactory();
       Session session= sf.openSession();
       List<Student> list=session.createQuery("from Student s").list();
       System.out.println("size="+list.size());


有结果了。over。

0 0
原创粉丝点击