-
Notifications
You must be signed in to change notification settings - Fork 830
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
Code sample - 23c JDBC Reactive Extensions #317
Open
juarezjuniorgithub
wants to merge
4
commits into
oracle-samples:main
Choose a base branch
from
juarezjuniorgithub:jdbc-reactive-extensions-intro
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
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 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,79 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.oracle.jdbc.reactive</groupId> | ||
<artifactId>jdbc-reactive-extensions-intro</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<name>jdbc-reactive-extensions-intro</name> | ||
<description>Introduction to JDBC Reactive Extensions with the Oracle Database 23c Free — Developer Release</description> | ||
<url>https://juarezjunior.medium.com/introduction-to-jdbc-reactive-extensions-with-the-oracle-database-23c-free-developer-release-b5a64ac5d194/</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.oracle.database.jdbc</groupId> | ||
<artifactId>ojdbc11-production</artifactId> | ||
<version>23.2.0.0</version> | ||
<type>pom</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-clean-plugin</artifactId> | ||
<version>3.1.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-site-plugin</artifactId> | ||
<version>3.7.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-project-info-reports-plugin</artifactId> | ||
<version>3.0.0</version> | ||
</plugin> | ||
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> | ||
<plugin> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-install-plugin</artifactId> | ||
<version>2.5.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>2.8.2</version> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
|
||
</project> |
126 changes: 126 additions & 0 deletions
126
...nsions-intro/src/main/java/com/oracle/jdbc/reactive/SQLStatementWithAsynchronousJDBC.java
This file contains 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,126 @@ | ||
/* | ||
Copyright (c) 2024, Oracle and/or its affiliates. | ||
|
||
This software is dual-licensed to you under the Universal Permissive License | ||
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License | ||
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose | ||
either license. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
https://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.oracle.jdbc.reactive; | ||
|
||
import java.sql.SQLException; | ||
import java.util.concurrent.Flow; | ||
|
||
import oracle.jdbc.OracleConnection; | ||
import oracle.jdbc.OraclePreparedStatement; | ||
import oracle.jdbc.pool.OracleDataSource; | ||
|
||
public class SQLStatementWithAsynchronousJDBC { | ||
|
||
private OracleDataSource ods = null; | ||
private OracleConnection conn = null; | ||
|
||
public SQLStatementWithAsynchronousJDBC() { | ||
try { | ||
ods = new OracleDataSource(); | ||
// jdbc:oracle:thin@[hostname]:[port]/[DB service/name] | ||
ods.setURL("jdbc:oracle:thin@[hostname]:[port]/[DB service/name"); | ||
ods.setUser("[Username]"); | ||
ods.setPassword("[Password]"); | ||
|
||
conn = (OracleConnection) ods.getConnection(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
SQLStatementWithAsynchronousJDBC asyncSQL = new SQLStatementWithAsynchronousJDBC(); | ||
try { | ||
// Execute a SQL DDL statement to create a database table | ||
// asynchronously | ||
asyncSQL.createTable(asyncSQL.getConn()); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Asynchronously creates a new table by executing a DDL SQL statement | ||
* | ||
* @param connection Connection to a database where the table is created | ||
* @return A Publisher that emits the result of executing DDL SQL | ||
* @throws SQLException If a database access error occurs before the DDL SQL can | ||
* be executed | ||
*/ | ||
private Flow.Publisher<Boolean> createTable(OracleConnection connection) throws SQLException { | ||
|
||
OraclePreparedStatement createTableStatement = (OraclePreparedStatement) connection | ||
.prepareStatement("CREATE TABLE employee_names (" + "id NUMBER PRIMARY KEY, " | ||
+ "first_name VARCHAR(50), " + "last_name VARCHAR2(50))"); | ||
|
||
Flow.Publisher<Boolean> createTablePublisher = createTableStatement.unwrap(OraclePreparedStatement.class) | ||
.executeAsyncOracle(); | ||
|
||
createTablePublisher.subscribe( | ||
|
||
// This subscriber will close the PreparedStatement | ||
new Flow.Subscriber<Boolean>() { | ||
public void onSubscribe(Flow.Subscription subscription) { | ||
subscription.request(1L); | ||
} | ||
|
||
public void onNext(Boolean item) { | ||
} | ||
|
||
public void onError(Throwable throwable) { | ||
closeStatement(); | ||
} | ||
|
||
public void onComplete() { | ||
closeStatement(); | ||
} | ||
|
||
void closeStatement() { | ||
try { | ||
createTableStatement.close(); | ||
} catch (SQLException closeException) { | ||
closeException.printStackTrace(); | ||
; | ||
} | ||
} | ||
}); | ||
|
||
return createTablePublisher; | ||
} | ||
|
||
public OracleDataSource getOds() { | ||
return ods; | ||
} | ||
|
||
public void setOds(OracleDataSource ods) { | ||
this.ods = ods; | ||
} | ||
|
||
public OracleConnection getConn() { | ||
return conn; | ||
} | ||
|
||
public void setConn(OracleConnection conn) { | ||
this.conn = conn; | ||
} | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the async APIs for a DDL wouldn't it be common. A better code sample should show a SELECT query and a DML.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed, I don't disagree with you on that, but at the end of the day, the goal is to present the use of our async API and the extensions offering, and it can be used with any SQL statement (DDL, DQL, DML, DCL and TCL) as it will be issued in an async way regardless of the semantics of the SQL statement, so the purpose of it is not defeated at all.
I proposed to create part 2 of this blog post (which just wanted to serve as an intro as no blog post existed about such API) as a more complete segway and then explore CRUD scenarios in it, so I added it as a TODO item for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please note that the official documentation also uses DDL as an example - https://docs.oracle.com/en/database/oracle/oracle-database/21/jjdbc/jdbc-reactive-extensions.html#GUID-1C40C43B-3823-4848-8B5A-D2F97A82F79B