hibernate shards分库的实现
2016-04-25 22:20
281 查看
网上资料良莠不齐。终于找到
如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。
如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。
相关文章推荐
- Android 开发新手指南一
- Tab
- oracle用户权限、角色管理详解
- SDOI R1 day2 T1 生成魔咒 后缀自动机
- [Lintcode] Unique Paths I,II
- Cocos2D 初步了解
- mysql-5.6.28源码安装 整个安装过程详细教程,详解
- 有向图的拓扑排序算法JAVA实现
- 415 评论博客
- c查漏补缺
- C++ 类的保护继承与构造函数
- Ubuntu 16.04 LTS 初体验
- 两篇很牛的vim使用技巧
- 关于Base64编码解码
- 生产消费模型demo
- 重要性!important
- 多部话机共享同一个号码的各种情况分析
- HM编码器端提取PU分割模式及CU深度信息
- 战略、业务流程和知识管理
- hdu 1102 Constructing Roads