Hibernate support for horizontal partitioning

Same model need to persist to multiple databases.
For example - SAAS

There are some strategies how to handle SAAS persistency.You can store all customers' data in the same tables, so you have single table for all customers,Or you can create different scheme (or database) for each customer.
Any solution has its own advantages and disadvantages.

A lot of companies chose the second approach (scheme for each customer) and have a problem to integrate it with hibernate.

Session factory is per persistence physical location, so even that this is the same model and the only difference is the connection parameters you need to create new session factory.
Session factory is an expensive object (creation time and size), so create session factory for each customer can kill the application.

This kind of problems Hibernate shard, a new open source of Hibernate come to solve.
Here is the preface:
You can't always put all your relational data in a single relational database. Sometimes you simply have too much data. Sometimes you have a distributed deployment architecture (network latency between California and India might be too high to have a single database). There might even be non-technical reasons (a potential customer simply won't do the deal unless her company's data lives in its own db instance). Whatever your reasons, talking to multiple relational databases inevitably complicates the development of your application. Hibernate Shards is a framework that is designed to encapsulate and minimize this complexity by adding support forhorizontal partitioning on top of Hibernate Core.
And this is how is builds:
1     public SessionFactory createSessionFactory() {
2         Configuration prototypeConfig = new Configuration().configure("shard0.hibernate.cfg.xml");
3         prototypeConfig.addResource("weather.hbm.xml");
4         List shardConfigs = new ArrayList();
5         shardConfigs.add(buildShardConfig("shard0.hibernate.cfg.xml"));
6         shardConfigs.add(buildShardConfig("shard1.hibernate.cfg.xml"));
7         shardConfigs.add(buildShardConfig("shard2.hibernate.cfg.xml"));
8         ShardStrategyFactory shardStrategyFactory = buildShardStrategyFactory();
9         ShardedConfiguration shardedConfig = new ShardedConfiguration(
10            prototypeConfig,
11            shardConfigs,
12            shardStrategyFactory);
13        return shardedConfig.buildShardedSessionFactory();
14    }
16    ShardStrategyFactory buildShardStrategyFactory() {
17        ShardStrategyFactory shardStrategyFactory = new ShardStrategyFactory() {
18            public ShardStrategy newShardStrategy(List shardIds) {
19                RoundRobinShardLoadBalancer loadBalancer = new RoundRobinShardLoadBalancer(shardIds);
20                ShardSelectionStrategy pss = new RoundRobinShardSelectionStrategy(loadBalancer);
21                ShardResolutionStrategy prs = new AllShardsShardResolutionStrategy(shardIds);
22                ShardAccessStrategy pas = new SequentialShardAccessStrategy();
23                return new ShardStrategyImpl(pss, prs, pas);
24            }
25        };
26        return shardStrategyFactory;
27    }
29    ShardConfiguration buildShardConfig(String configFile) {
30        Configuration config = new Configuration().configure(configFile);
31        return new ConfigurationToShardConfigurationAdapter(config);
32    }

As you can see from the above createSessionFactory build from multiple cfgs file, so we have one session factory for all configurations.Hibernate shard come with a lot of power to control the persistency partition strategy. You can save to a partition based on ids or parallel or round robin…

For more info:

אין תגובות:

הוסף רשומת תגובה