Skip to content

Commit 4e50178

Browse files
meedbekgvenzl
andauthored
Add spring sharding demo (#308)
* Add spring sharding demo * Update database setup instructions * Replace 2023 with 2024 in copyright header --------- Co-authored-by: Gerald Venzl <gerald.venzl@oracle.com>
1 parent 4002bb5 commit 4e50178

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+5280
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### STS ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
15+
### IntelliJ IDEA ###
16+
.idea
17+
*.iws
18+
*.iml
19+
*.ipr
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/
27+
build/
28+
!**/src/main/**/build/
29+
!**/src/test/**/build/
30+
31+
### VS Code ###
32+
.vscode/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Copyright (c) 2023 Oracle and/or its affiliates.
2+
3+
The Universal Permissive License (UPL), Version 1.0
4+
5+
Subject to the condition set forth below, permission is hereby granted to any
6+
person obtaining a copy of this software, associated documentation and/or data
7+
(collectively the "Software"), free of charge and under any and all copyright
8+
rights in the Software, and any and all patent rights owned or freely
9+
licensable by each licensor hereunder covering either (i) the unmodified
10+
Software as contributed to or provided by such licensor, or (ii) the Larger
11+
Works (as defined below), to deal in both
12+
13+
(a) the Software, and
14+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
one is included with the Software (each a "Larger Work" to which the Software
16+
is contributed by such licensors),
17+
18+
without restriction, including without limitation the rights to copy, create
19+
derivative works of, display, perform, and distribute the Software and make,
20+
use, sell, offer for sale, import, export, have made, and have sold the
21+
Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
either these or other terms.
23+
24+
This license is subject to the following condition:
25+
The above copyright notice and either this complete permission notice or at
26+
a minimum a reference to the UPL must be included in all copies or
27+
substantial portions of the Software.
28+
29+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35+
SOFTWARE.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
## Overview
2+
3+
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.
4+
The feature is about supporting direct routing to sharded databases.
5+
6+
This version uses Spring Data JDBC (JdbcTemplate), for data access.
7+
8+
You can use the datasource configurations provided in this project as a template for setting up the sharding feature in your own projects.
9+
10+
## Configuration
11+
12+
### Database
13+
14+
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)
15+
to learn how to set up and deploy an Oracle sharded database.
16+
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.
17+
18+
After your sharded database is set, connect to the shard catalog as sysdba and create the demo application schema user.
19+
20+
~~~SQL
21+
ALTER SESSION ENABLE SHARD DDL;
22+
23+
-- Create demo schema user
24+
CREATE USER demo_user IDENTIFIED BY demo_user;
25+
GRANT CONNECT, RESOURCE TO demo_user;
26+
GRANT CREATE TABLE TO demo_user;
27+
GRANT UNLIMITED TABLESPACE TO demo_user;
28+
29+
-- Create tablespace
30+
CREATE TABLESPACE SET TS1 USING TEMPLATE (
31+
DATAFILE SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
32+
EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO);
33+
~~~
34+
35+
On the shard catalog connect as demo_user and run the following SQL script to create your tables.
36+
37+
~~~SQL
38+
ALTER SESSION ENABLE SHARD DDL;
39+
40+
CREATE SHARDED TABLE users (
41+
user_id NUMBER PRIMARY KEY,
42+
name VARCHAR2(100),
43+
password VARCHAR2(255),
44+
role VARCHAR2(5),
45+
CONSTRAINT roleCheck CHECK (role IN ('USER', 'ADMIN')))
46+
TABLESPACE SET TS1 PARTITION BY CONSISTENT HASH (user_id);
47+
48+
CREATE SHARDED TABLE notes (
49+
note_id NUMBER NOT NULL,
50+
user_id NUMBER NOT NULL,
51+
title VARCHAR2(255),
52+
content CLOB,
53+
CONSTRAINT notePK PRIMARY KEY (note_id, user_id),
54+
CONSTRAINT userFK FOREIGN KEY (user_id) REFERENCES users(user_id))
55+
PARTITION BY REFERENCE (UserFK);
56+
57+
CREATE SEQUENCE note_sequence INCREMENT BY 1 START WITH 1 MAXVALUE 2E9 SHARD;
58+
~~~
59+
60+
Make sure to insert a user or two in the database before testing the application.
61+
62+
~~~SQL
63+
INSERT INTO users VALUES (0, 'user1', LOWER(STANDARD_HASH('user1', 'SHA256')), 'USER');
64+
INSERT INTO users VALUES (1, 'admin', LOWER(STANDARD_HASH('admin', 'SHA256')), 'ADMIN');
65+
COMMIT;
66+
~~~
67+
68+
To uninstall and clean up the preceding setup, you can connect as sysdba and run the following SQL script.
69+
70+
~~~SQL
71+
ALTER SESSION ENABLE SHARD DDL;
72+
73+
DROP USER demo_user CASCADE;
74+
DROP TABLESPACE SET TS1;
75+
~~~
76+
77+
## Building the application
78+
To build the application run:
79+
80+
~~~
81+
mvn install
82+
~~~
83+
84+
## Running the application
85+
86+
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.
87+
88+
~~~shell
89+
export CATALOG_URL="the catalog url"
90+
export CATALOG_USER="demo_user"
91+
export CATALOG_PASS="demo_user"
92+
export SHARD_DIRECTOR_URL="the shard director url"
93+
export SHARD_DIRECTOR_USER="demo_user"
94+
export SHARD_DIRECTOR_PASS="demo_user"
95+
~~~
96+
97+
Then you can run the application using:
98+
99+
~~~shell
100+
mvn spring-boot:run
101+
~~~

0 commit comments

Comments
 (0)