Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spring sharding demo #308

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions java/jdbc/SpringSharding/JDBCTemplate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
35 changes: 35 additions & 0 deletions java/jdbc/SpringSharding/JDBCTemplate/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Copyright (c) 2023 Oracle and/or its affiliates.
meedbek marked this conversation as resolved.
Show resolved Hide resolved

The Universal Permissive License (UPL), Version 1.0

Subject to the condition set forth below, permission is hereby granted to any
person obtaining a copy of this software, associated documentation and/or data
(collectively the "Software"), free of charge and under any and all copyright
rights in the Software, and any and all patent rights owned or freely
licensable by each licensor hereunder covering either (i) the unmodified
Software as contributed to or provided by such licensor, or (ii) the Larger
Works (as defined below), to deal in both

(a) the Software, and
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
one is included with the Software (each a "Larger Work" to which the Software
is contributed by such licensors),

without restriction, including without limitation the rights to copy, create
derivative works of, display, perform, and distribute the Software and make,
use, sell, offer for sale, import, export, have made, and have sold the
Software and the Larger Work(s), and to sublicense the foregoing rights on
either these or other terms.

This license is subject to the following condition:
The above copyright notice and either this complete permission notice or at
a minimum a reference to the UPL must be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions java/jdbc/SpringSharding/JDBCTemplate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
## Overview

This project demonstrates the use of the latest sharding [feature](https://github.com/spring-projects/spring-framework/pull/31506) in Spring Framework with the Oracle Database.
The feature is about supporting direct routing to sharded databases.

This version uses Spring Data JDBC (JdbcTemplate), for data access.

You can use the datasource configurations provided in this project as a template for setting up the sharding feature in your own projects.

## Configuration

### Database

You can refer to the [Oracle Docs](https://docs.oracle.com/en/database/oracle/oracle-database/21/shard/sharding-deployment.html#GUID-F99B8742-4089-4E77-87D4-4691EA932207)
to learn how to set up and deploy an Oracle sharded database.
You can also refer to [Oracle Database Operator](https://github.com/oracle/oracle-database-operator) that makes deploying a sharded database on a Kubernetes Cluster an easy process.

After your sharded database is set, run the following SQL script on the shard catalog to create your tables.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As what database user are you supposed to run this script? There is no indication of that. Is it SYSTEM or the shard director user, or the catalog user, or are these all the same the user? It would probably help the user to be more specific on how to execute these DDLs and what user(s) to use in the properties.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After your sharded database is set, connect to the shard catalog as sysdba and create the demo application schema user.


~~~SQL
ALTER SESSION ENABLE SHARD DDL;

CREATE TABLESPACE SET TS1 USING TEMPLATE (
DATAFILE SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
meedbek marked this conversation as resolved.
Show resolved Hide resolved
EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO);

CREATE SHARDED TABLE users (
user_id NUMBER PRIMARY KEY,
name VARCHAR2(100),
password VARCHAR2(255),
role VARCHAR2(5),
CONSTRAINT roleCheck CHECK (role IN ('USER', 'ADMIN')))
TABLESPACE SET TS1 PARTITION BY CONSISTENT HASH (user_id);

CREATE SHARDED TABLE notes (
note_id NUMBER NOT NULL,
user_id NUMBER NOT NULL,
title VARCHAR2(255),
content CLOB,
CONSTRAINT notePK PRIMARY KEY (note_id, user_id),
CONSTRAINT userFK FOREIGN KEY (user_id) REFERENCES users(user_id))
PARTITION BY REFERENCE (UserFK);

CREATE SEQUENCE note_sequence INCREMENT BY 1 START WITH 1 MAXVALUE 2E9 SHARD;
~~~

Make sure to insert a user or two in the database before testing the application.

~~~SQL
INSERT INTO users VALUES (0, 'user1', standard_hash('user1', 'SHA256'), 'USER');
INSERT INTO users VALUES (1, 'admin', standard_hash('admin', 'SHA256'), 'ADMIN');
~~~

## Building the application
To build the application run:

~~~
mvn install
~~~

## Running the application

Before running the application set the following environment variables or update [application.properties](src/main/resources/application.properties). These configure the URL and credentials for the catalog database and shard director (GSM) used by the application.

~~~shell
export CATALOG_URL="the catalog url"
export CATALOG_USER="username"
export CATALOG_PASS="password"
export SHARD_DIRECTOR_URL="the shard director url"
export SHARD_DIRECTOR_USER="username"
export SHARD_DIRECTOR_PASS="password"
~~~

Then you can run the application using:

~~~shell
mvn spring-boot:run
~~~
Loading