Last active
February 25, 2021 07:41
-
-
Save aalmiray/972815834e712eb0a205dc3fa0b187c2 to your computer and use it in GitHub Desktop.
SpringBoot + Oracle JDBC
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
dependencies { | |
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc' | |
implementation 'com.oracle.database.jdbc:ojdbc8:19.6.0.0' | |
implementation 'com.oracle.database.jdbc:ucp:19.6.0.0' | |
} |
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
package com.acme.ucp; | |
import oracle.ucp.jdbc.PoolDataSource; | |
import oracle.ucp.jdbc.PoolDataSourceFactory; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Primary; | |
import javax.sql.DataSource; | |
import java.sql.SQLException; | |
@Configuration | |
public class DataSourceConfig { | |
@Value("${spring.datasource.url}") | |
private String url; | |
@Value("${spring.datasource.username}") | |
private String username; | |
@Value("${spring.datasource.password}") | |
private String password; | |
@Value("${oracle.ucp.minPoolSize}") | |
private String minPoolSize; | |
@Value("${oracle.ucp.maxPoolSize}") | |
private String maxPoolSize; | |
@Value("${spring.datasource.driver-class-name:oracle.jdbc.pool.OracleDataSource}") | |
private String driverClassName; | |
@Bean(name = "OracleUniversalConnectionPool") | |
@Primary | |
public DataSource getDataSource() { | |
PoolDataSource pds = null; | |
try { | |
pds = PoolDataSourceFactory.getPoolDataSource(); | |
pds.setConnectionFactoryClassName(driverClassName); | |
pds.setURL(url); | |
pds.setUser(username); | |
pds.setPassword(password); | |
pds.setMinPoolSize(Integer.valueOf(minPoolSize)); | |
pds.setInitialPoolSize(10); | |
pds.setMaxPoolSize(Integer.valueOf(maxPoolSize)); | |
} catch (SQLException ea) { | |
System.err.println("Error connecting to the database: " + ea.getMessage()); | |
} | |
return pds; | |
} | |
} |
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
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-jdbc</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.oracle.database.jdbc</groupId> | |
<artifactId>ojdbc8</artifactId> | |
<version>19.6.0.0</version> | |
</dependency> | |
<dependency> | |
<groupId>com.oracle.database.jdbc</groupId> | |
<artifactId>ucp</artifactId> | |
<version>19.6.0.0</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment