`

Database Sharding

阅读更多

Database sharding is a way of scaling your database horizontally. For a given table or set of tables, you split up the data that is stored and fetched based on a given hash or something like that. Google, Facebook, and Wikipedia all use database sharding.

 

Benefits

  • One database does not get overloaded
  • Smaller queries (since each table has less data now)
  • Better overall throughput under load because all your IO is not going through one database server.

Sharded Portal Instances

At present, sharding exists in Liferay from version 5.2.3+ for handling data across multiple portal instances. Here's what you do to get it up.

  1. After you have a normally working development environment, make sure your hosts file is setup to allow virtual hosting (for our convenience, I will call the hosts abc1.com, abc2.com, abc3.com, etc.).
  2. Use the create-minimal SQL script (from our Downloads section) to create three database schemas: lportal, lportal1, and lportal2. Be careful and use the scripts for your liferay portal version, otherwise, it won't work. By default, the configuration files are setup for three schemas called default, one and two, but you can configure it for more. All this configuration is set up in the file portal-impl/src/META-INF/shard-data-source-spring.xml . As you will see, we will include this file in the property spring.configs in the next step. In case we want to modify it we just should write our own file in the Extension Environment and include it in this property instead of the default one.
  3. In your portal-ext.properties, you will need to set the following:
    1. Enable META-INF/shard-data-source-spring.xml under spring.configs.
    2. Configure the jdbc schema settings for jdbc.default.*, jdbc.one.*, and jdbc.two.*.
    3. Enable the shard names: shard.available.names=default,one,two
  4. Don't forget to set the appropriate username and password for each schema.
  5. Startup the server and create several (2-3) portal instances (e.g., abc1.com, abc2.com, abc3.com).
  6. Using your favorite database browser, do a query on each of your schemas for the User_ table, and you will notice that the data is now distributed across different schemas. That's it!

Additional Settings

  • Remember that default, one, and two are default values defined in portal-impl/src/META-INF/shard-data-source-spring.xml and portal-ext.properties. If you want to change it, make sure to propagate the changes to the spring XML file as well as portal-ext.properties.
  • If you want to manually select the shard (via the GUI when creating portal instances), you need to enable in your portal-ext.properties file: shard.selector=com.liferay.portal.dao.shard.ManualShardSelector. Otherwise, the shard for the data will be chosen using a round robin technique.

======================================================================

Hi,

Multiple tenants are using one portal, but each tenant requires a separate datasource (database). So upon each incoming request we need to perform a context switch each time a Hibernate Session is needed. I have browsed the source fairly extensively and found what I believe is the best place to hook in for this context switch:

com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl#openSession() {
...
if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
session = _sessionFactoryImplementor.getCurrentSession();
}
else {
session = _sessionFactoryImplementor.openSession();
}


I found that SPRING_HIBERNATE_SESSION_DELEGATED maps to the following property:

# Set this property to false to manually open and close sessions
spring.hibernate.session.delegated=true


Per what I read in docs and code I can set this property to false, which I have done:

spring.hibernate.session.delegated=false

Docs say to also change the following property to reference my replacement class:

# Set the bean name for the Liferay session factory.
spring.hibernate.session.factory=com.blah.blah.BlahSessionFactoryImplementor


I have done this in portal-ext.properties in:

tomcat-6.0.18/webapps/ROOT/WEB-INF/classes/portal-ext.properties

I have also placed my class here:

tomcat-6.0.18/webapps/ROOT/WEB-INF/classes/com/blah/blah/BlahSessionFactoryImplementor.class

When I start the portal I see that the ext properties get loaded. But my class is apparently not loaded. I have tried to log output when openSession() is called but I see no logging to the console. I also have added an explicit exception throw from the method in case logging is not setup correctly. I see no indication that an exception is thrown. The portal runs normally. I am sure my class is not being used.

1. Is this the correct way to override the session factory? If so, what could be the problem?

2. If not, what should I do?

Thanks!

Vaughn
分享到:
评论

相关推荐

    sharding-jdbc.zip

    1、创建数据库 首先我们创建相应的数据库 create database sharding_0; create database sharding_1; 这样我们就创建了两个数据库sharding_0和sharding_1; 脚本在项目里面

    Oracle sharding database安装操作手册

    Oracle Sharding Database(分片数据库)是一项数据库水平分割的技术,它能够通过将数据分布在多个数据库服务器中,从而实现大规模并发访问和高效数据管理。Oracle Sharding 是Oracle数据库提供的解决方案,使得应用...

    MySQL分库分表-sharding_demo.zip

    此时,分库(database sharding)和分表(table sharding)成为有效的解决方案。分库是将数据分散到多个数据库中,而分表则是将一个大表拆分成多个小表,通常在相同的数据库内进行。 在"sharding_demo-master"这个...

    MYSQL 数据库水平切分的实现原理解析

    水平切分可以进一步细分为分库(Database Sharding)和分表(Table Sharding)两种主要类型: 1. **分库(Database Sharding)**:将整个数据库中的表按照一定的规则拆分成多个子集,并将这些子集分别存储在不同的...

    simple-sharding, A simple database shard middleware.zip

    《简单分库中间件simple-sharding的探索与实践》 在大数据时代,数据库的扩展性和性能成为了企业系统架构的关键挑战之一。"simple-sharding",正如其名,是一款旨在简化数据库分片操作的中间件,它作为一个轻量级的...

    一种以ID特征为依据的数据分片(Sharding)策略

    假如您有一个应用程序,随着业务越来越有起色,系统所牵涉到的数据量也就越来越大,此时您要涉及到对...由于许多系统的瓶颈都处在数据存储上,因此一种叫做“数据分片(DatabaseSharding)”的数据架构方式应运而生,本

    计算机-软件工程-外文翻译-外文文献-英文文献.doc

    1. 数据库切分: Database Sharding 是一种将大型数据库拆分成多个小的逻辑数据库的技术,以便提高数据库的性能和可扩展性。 2. Hibernate Shards:Hibernate Shards 是一个基于 Java 的开源框架,用于实现数据库...

    对分库分表的一些想法

    这种方法通常分为分库(Database Sharding)和分表(Table Sharding)两种方式: 1. **分库**:即将一个大数据库拆分成多个小数据库,每个数据库负责一部分数据的存储和管理。这可以有效缓解单个数据库的压力,避免...

    MyCat权威指南 v1.6.0

    数据库切分(Database Sharding)作为一种重要的技术手段,被广泛应用于大数据量场景下,其目的是通过将一个大型数据库分割成多个较小的部分来提高系统的可扩展性和性能。 **数据库切分**主要是指将一个大的数据库...

    mysql分库分表-atlantic.zip

    1. **分库(Database Sharding)**:将一个大数据库拆分成多个小数据库,每个数据库负责一部分业务数据。这样可以降低单一数据库的压力,提高读写性能,同时有利于实现高可用性和灾难恢复。 2. **分表(Table ...

    云时代的数据库 - Oracle 的技术演进之路和19c新特性揭秘.pdf

    2. **数据库分片(Database Sharding)** 面对大数据和高并发的挑战,Oracle引入了分片技术,通过将数据分布在多个独立的数据库实例上,实现了水平扩展,确保了大规模业务场景下的高性能和高可用性。 3. **内存...

    一步步搭建基于RedHat 7 Oracle 12c Sharding

    Oracle Sharding是一种架构,它是Oracle Database 12c Release 2(版本12.2)中的一种特性,专门为在线事务处理(OLTP)应用程序设计。在Sharding架构中,数据被水平地分布在多个独立的Oracle数据库中,这些独立的...

    django-master-class

    - **Database Optimization:** Techniques like indexing, query optimization, and database sharding can enhance the scalability of a Django application. #### 9. GIS (Jeremy Dunck) **Introduction:** ...

    多个数据库配置

    - **数据库路由(Database Sharding)**:根据特定规则(如用户ID或地理区域)将数据分散到多个数据库。 - **读写分离**:主数据库负责写操作,从数据库处理读请求,减少主库压力。 - **数据复制**:数据在多个...

    database-sharding:自动从code.google.compdatabase-sharding导出

    "database-sharding"项目是Google Code上一个已迁移的开源项目,专注于提供数据库分片的解决方案。尽管描述中提到这只是测试,但我们可以深入探讨数据库分片这一主题,以及它在Java环境中的实现。 数据库分片是一种...

    Oracle面试题

    - **Database Sharding:** 数据分片,用于水平扩展数据库。 - **JSON Support:** 对JSON数据的支持,便于处理半结构化数据。 以上内容涵盖了Oracle面试中可能遇到的一些重要知识点和技术细节,希望能帮助您更好地...

    数据库+分库分表+sharding-jdbc

    Sharding-JDBC是阿里巴巴开源的一款轻量级的Java框架,用于实现数据库的水平拆分,它是基于JDBC的中间件,可以在不修改业务代码的情况下,透明地实现数据分片和读写分离。 1. 数据库分库分表概念: 分库是指将一个...

    开源项目-go-pg-sharding.zip

    在Go-PG-Sharding项目中,Go语言的并发特性使得处理多个数据库分片变得轻而易举,同时其标准库中的net/http和database/sql等模块为实现高效的数据库操作提供了便利。 PostgreSQL,又名PG,是一款强大的开源对象关系...

    leetcode卡-Interview-Preparation:准备面试的一站式目的地

    Database sharding —— Design a distributed cache framework like Redis —— Design a CDN - Design a service like TinyURL 一般 IT 相关主题 What is Docker and why use it ? VM vs Docker What is Docker ...

Global site tag (gtag.js) - Google Analytics