Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4224756

Browse files
juarezjuniorgithubgvenzl
andauthored
Added sample code - JDBC 21c Driver Support for Virtual Threads (#223)
* Added sample code and script - Intro to JDBC 21c Driver Support for Virtual Threads * Added adjustments as requested by Kuassi --------- Co-authored-by: Gerald Venzl <gerald.venzl@oracle.com>
1 parent d82112b commit 4224756

File tree

6 files changed

+372
-0
lines changed

6 files changed

+372
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Java - Oracle Developers on Medium.com
2+
[Introduction to Oracle JDBC 21c Driver Support for Virtual Threads](https://juarezjunior.medium.com/introduction-to-oracle-jdbc-21c-driver-support-for-virtual-threads-189b918c56f4)

‎java/jdbcdriver-virtual-threads/pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<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">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.oracle.dev.jdbc</groupId>
7+
<artifactId>jdbcdriver-virtual-threads</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<name>jdbcdriver-virtual-threads</name>
11+
<description>Introduction to Oracle JDBC 21c Driver Support for Virtual Threads</description>
12+
<url>https://medium.com/oracledevs/introduction-to-oracle-jdbc-21c-driver-support-for-virtual-threads-189b918c56f4</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.source>21</maven.compiler.source>
17+
<maven.compiler.target>21</maven.compiler.target>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>3.8.1</version>
25+
</dependency>
26+
<!-- Oracle JDBC JARs -->
27+
<dependency>
28+
<groupId>com.oracle.database.jdbc</groupId>
29+
<artifactId>ojdbc11-production</artifactId>
30+
<version>21.7.0.0</version>
31+
<type>pom</type>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<pluginManagement>
37+
<plugins>
38+
<plugin>
39+
<artifactId>maven-clean-plugin</artifactId>
40+
<version>3.1.0</version>
41+
</plugin>
42+
<plugin>
43+
<artifactId>maven-site-plugin</artifactId>
44+
<version>3.7.1</version>
45+
</plugin>
46+
<plugin>
47+
<artifactId>maven-project-info-reports-plugin</artifactId>
48+
<version>3.0.0</version>
49+
</plugin>
50+
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
51+
<plugin>
52+
<artifactId>maven-resources-plugin</artifactId>
53+
<version>3.0.2</version>
54+
</plugin>
55+
<plugin>
56+
<artifactId>maven-compiler-plugin</artifactId>
57+
<version>3.8.0</version>
58+
<configuration>
59+
<compilerArgs>
60+
<arg>--enable-preview</arg>
61+
</compilerArgs>
62+
</configuration>
63+
</plugin>
64+
<plugin>
65+
<artifactId>maven-surefire-plugin</artifactId>
66+
<version>2.22.1</version>
67+
</plugin>
68+
<plugin>
69+
<artifactId>maven-jar-plugin</artifactId>
70+
<version>3.0.2</version>
71+
</plugin>
72+
<plugin>
73+
<artifactId>maven-install-plugin</artifactId>
74+
<version>2.5.2</version>
75+
</plugin>
76+
<plugin>
77+
<artifactId>maven-deploy-plugin</artifactId>
78+
<version>2.8.2</version>
79+
</plugin>
80+
</plugins>
81+
</pluginManagement>
82+
</build>
83+
84+
</project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.jdbc;
23+
24+
import java.io.InputStream;
25+
import java.util.Properties;
26+
27+
/**
28+
* <p>
29+
* Configuration for connecting code samples to an Oracle Database instance.
30+
* </p>
31+
*/
32+
public class DatabaseConfig {
33+
34+
private static final Properties CONFIG = new Properties();
35+
36+
static {
37+
38+
try (InputStream input = DatabaseConfig.class.getClassLoader().getResourceAsStream("config.properties")) {
39+
if (input == null) {
40+
System.out.println("Sorry, unable to find config.properties");
41+
System.exit(1);
42+
}
43+
// load a properties file from class path, inside static method
44+
CONFIG.load(input);
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
49+
}
50+
51+
private static final String DB_USER = CONFIG.getProperty("DB_USER");
52+
53+
private static final String DB_URL = CONFIG.getProperty("DB_URL");
54+
55+
private static final String DB_PASSWORD = CONFIG.getProperty("DB_PASSWORD");
56+
57+
public static String getDbUser() {
58+
return DB_USER;
59+
}
60+
61+
public static String getDbUrl() {
62+
return DB_URL;
63+
}
64+
65+
public static String getDbPassword() {
66+
return DB_PASSWORD;
67+
}
68+
69+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.jdbc;
23+
24+
import java.sql.Connection;
25+
import java.sql.DatabaseMetaData;
26+
import java.sql.ResultSet;
27+
import java.sql.SQLException;
28+
import java.sql.Statement;
29+
import java.time.Duration;
30+
import java.time.Instant;
31+
import java.util.Properties;
32+
import java.util.stream.IntStream;
33+
34+
import oracle.jdbc.OracleConnection;
35+
import oracle.ucp.jdbc.PoolDataSource;
36+
import oracle.ucp.jdbc.PoolDataSourceFactory;
37+
38+
public class PlatformThreadsOracleJdbc {
39+
40+
private final static String DB_URL = DatabaseConfig.getDbUrl();
41+
private final static String DB_USER = DatabaseConfig.getDbUser();
42+
private final static String DB_PASSWORD = DatabaseConfig.getDbPassword();
43+
private final static String queryStatement = "SELECT * FROM SH.CUSTOMERS WHERE CUST_ID = 49671";
44+
45+
public static void main(String args[]) throws SQLException {
46+
47+
Properties props = new Properties();
48+
props.put(OracleConnection.CONNECTION_PROPERTY_FAN_ENABLED, "false");
49+
50+
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
51+
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
52+
pds.setURL(DB_URL);
53+
pds.setUser(DB_USER);
54+
pds.setPassword(DB_PASSWORD);
55+
pds.setConnectionPoolName("JDBC_UCP_POOL");
56+
pds.setConnectionProperties(props);
57+
OracleConnection connection = (OracleConnection) pds.getConnection();
58+
59+
DatabaseMetaData dbmd = connection.getMetaData();
60+
System.out.println("Driver Name: " + dbmd.getDriverName());
61+
System.out.println("Driver Version: " + dbmd.getDriverVersion());
62+
System.out.println();
63+
64+
Instant start = Instant.now();
65+
66+
// platform threads
67+
var threads = IntStream.range(0, 1_500).mapToObj(i -> new Thread(() -> {
68+
try {
69+
doSQLWork(connection, queryStatement);
70+
System.out.println("Query #: " + (i));
71+
} catch (SQLException ex) {
72+
ex.printStackTrace();
73+
}
74+
})).toList();
75+
76+
for (var thread : threads) {
77+
thread.start();
78+
}
79+
80+
for (var thread : threads) {
81+
try {
82+
thread.join();
83+
} catch (InterruptedException e) {
84+
e.printStackTrace();
85+
}
86+
}
87+
88+
Instant finish = Instant.now();
89+
long timeElapsed = Duration.between(start, finish).getSeconds();
90+
System.out.println("Elapsed: " + timeElapsed);
91+
92+
}
93+
94+
private static void doSQLWork(Connection conn, String queryStatement) throws SQLException {
95+
conn.setAutoCommit(false);
96+
try (Statement statement = conn.createStatement();
97+
ResultSet resultSet = statement.executeQuery(queryStatement)) {
98+
while (resultSet.next()) {
99+
System.out.println(new StringBuilder(resultSet.getString(1)).append(" ").append(resultSet.getString(2))
100+
.append(" ").append(resultSet.getString(3)).append(" ").append(resultSet.getString(4))
101+
.append(" ").append(resultSet.getInt(5)).toString());
102+
}
103+
statement.close();
104+
resultSet.close();
105+
}
106+
}
107+
108+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.jdbc;
23+
24+
import java.sql.Connection;
25+
import java.sql.DatabaseMetaData;
26+
import java.sql.ResultSet;
27+
import java.sql.SQLException;
28+
import java.sql.Statement;
29+
import java.time.Duration;
30+
import java.time.Instant;
31+
import java.util.Properties;
32+
import java.util.stream.IntStream;
33+
34+
import oracle.jdbc.OracleConnection;
35+
import oracle.ucp.jdbc.PoolDataSource;
36+
import oracle.ucp.jdbc.PoolDataSourceFactory;
37+
38+
public class VirtualThreadsOracleJdbc {
39+
40+
private final static String DB_URL = DatabaseConfig.getDbUrl();
41+
private final static String DB_USER = DatabaseConfig.getDbUser();
42+
private final static String DB_PASSWORD = DatabaseConfig.getDbPassword();
43+
private static String queryStatement = "SELECT * FROM SH.CUSTOMERS WHERE CUST_ID = 49671";
44+
45+
public static void main(String args[]) throws SQLException {
46+
47+
Properties props = new Properties();
48+
props.put(OracleConnection.CONNECTION_PROPERTY_FAN_ENABLED, "false");
49+
50+
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
51+
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
52+
pds.setURL(DB_URL);
53+
pds.setUser(DB_USER);
54+
pds.setPassword(DB_PASSWORD);
55+
pds.setConnectionPoolName("JDBC_UCP_POOL");
56+
pds.setConnectionProperties(props);
57+
OracleConnection connection = (OracleConnection) pds.getConnection();
58+
59+
DatabaseMetaData dbmd = connection.getMetaData();
60+
System.out.println("Driver Name: " + dbmd.getDriverName());
61+
System.out.println("Driver Version: " + dbmd.getDriverVersion());
62+
System.out.println();
63+
64+
Instant start = Instant.now();
65+
66+
// virtual threads
67+
var threads = IntStream.range(0, 1_500).mapToObj(i -> Thread.startVirtualThread(() -> {
68+
try {
69+
doSQLWork(connection, queryStatement);
70+
System.out.println("Query #: " + (i));
71+
} catch (SQLException ex) {
72+
ex.printStackTrace();
73+
}
74+
})).toList();
75+
76+
for (var thread : threads) {
77+
try {
78+
thread.join();
79+
} catch (InterruptedException e) {
80+
e.printStackTrace();
81+
}
82+
}
83+
84+
Instant finish = Instant.now();
85+
long timeElapsed = Duration.between(start, finish).getSeconds();
86+
System.out.println("Elapsed: " + timeElapsed);
87+
88+
}
89+
90+
private static void doSQLWork(Connection conn, String queryStatement) throws SQLException {
91+
conn.setAutoCommit(false);
92+
try (Statement statement = conn.createStatement();
93+
ResultSet resultSet = statement.executeQuery(queryStatement)) {
94+
while (resultSet.next()) {
95+
System.out.println(new StringBuilder(resultSet.getString(1)).append(" ").append(resultSet.getString(2))
96+
.append(" ").append(resultSet.getString(3)).append(" ").append(resultSet.getString(4))
97+
.append(" ").append(resultSet.getInt(5)).toString());
98+
}
99+
statement.close();
100+
resultSet.close();
101+
}
102+
}
103+
104+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# https://docs.oracle.com/en/cloud/paas/autonomous-database/adbsa/connect-jdbc-thin-wallet.html
2+
# jdbc:oracle:thin:@dbname_tpurgent?TNS_ADMIN=<PATH_TO_YOUR_WALLET>
3+
DB_URL=jdbc:oracle:thin:@jdbcvtdb_tpurgent?TNS_ADMIN=<PATH_TO_YOUR_WALLET>
4+
DB_USER=<DB_USER>
5+
DB_PASSWORD=<DB_PASSWORD>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /