-
Notifications
You must be signed in to change notification settings - Fork 857
Add spring sharding demo #308
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| Copyright (c) 2023 Oracle and/or its affiliates. | ||
|
|
||
| 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| ## 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, connect to the shard catalog as sysdba and create the demo application schema user. | ||
|
|
||
| ~~~SQL | ||
| ALTER SESSION ENABLE SHARD DDL; | ||
|
|
||
| -- Create demo schema user | ||
| CREATE USER demo_user IDENTIFIED BY demo_user; | ||
| GRANT CONNECT, RESOURCE TO demo_user; | ||
| GRANT CREATE TABLE TO demo_user; | ||
| GRANT UNLIMITED TABLESPACE TO demo_user; | ||
|
|
||
| -- Create tablespace | ||
| CREATE TABLESPACE SET TS1 USING TEMPLATE ( | ||
| DATAFILE SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED | ||
| EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO); | ||
| ~~~ | ||
|
|
||
| On the shard catalog connect as demo_user and run the following SQL script to create your tables. | ||
|
|
||
| ~~~SQL | ||
| ALTER SESSION ENABLE SHARD DDL; | ||
|
|
||
| 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', LOWER(STANDARD_HASH('user1', 'SHA256')), 'USER'); | ||
| INSERT INTO users VALUES (1, 'admin', LOWER(STANDARD_HASH('admin', 'SHA256')), 'ADMIN'); | ||
| COMMIT; | ||
| ~~~ | ||
|
|
||
| To uninstall and clean up the preceding setup, you can connect as sysdba and run the following SQL script. | ||
|
|
||
| ~~~SQL | ||
| ALTER SESSION ENABLE SHARD DDL; | ||
|
|
||
| DROP USER demo_user CASCADE; | ||
| DROP TABLESPACE SET TS1; | ||
| ~~~ | ||
|
|
||
| ## 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="demo_user" | ||
| export CATALOG_PASS="demo_user" | ||
| export SHARD_DIRECTOR_URL="the shard director url" | ||
| export SHARD_DIRECTOR_USER="demo_user" | ||
| export SHARD_DIRECTOR_PASS="demo_user" | ||
| ~~~ | ||
|
|
||
| Then you can run the application using: | ||
|
|
||
| ~~~shell | ||
| mvn spring-boot:run | ||
| ~~~ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.