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。
- hibernate shards分库的实现
- Hibernate Shards
- Hibernate Shards
- 使用 Hibernate Shards 进行切分
- [转]Sharding with Hibernate Shards
- Spring-Hibernate分库事务实现
- hibernate实现分表
- Hibernate实现分表
- hibernate 实现分表
- 【Hibernate】Hibernate分表的实现--老古董留存
- solr分布式shards的合并优化
- Elasticsearch关于unassigned shards的查看
- Elasticsearch关于unassigned shards的查看
- Hibernate Shards - 关系数据库水平分割解决方案框架
- Java 开发 2.0: 使用 Hibernate Shards 进行切分
- 数据库水平分割垂直分割 使用 Hibernate Shards 进行切分
- Java 开发 2.0: 使用 Hibernate Shards 进行切分
- Java 开发 2.0: 使用 Hibernate Shards 进行切分
- 高并发带来的重复提交问题解决方案
- 第5周 经典小程序2题 C语言实现的猜拳游戏(剪子锤子布)
- kafka Java实例
- PHP中的$this用法
- 从零开始变大牛
- hibernate shards分库的实现
- hdu 5665 Lucky(简单题)(bestcoder#80 1000)
- 下载网页上的各种视频只需四步
- dp 16
- mysql数据库导入导出
- 实现带记忆功能的AutoCompleteTextView
- HDU 5672:String【模拟】
- 易语言学习第二十五课----CE查找内存
- HashMap实现原理分析