Skip to content

Commit

Permalink
Code sample - SpringBoot Hikari to UCP migration
Browse files Browse the repository at this point in the history
  • Loading branch information
juarezjuniorgithub committed May 10, 2024
1 parent 4a5f71f commit 8637e3d
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 0 deletions.
2 changes: 2 additions & 0 deletions java/springboot-hikari-ucp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# springboot-hikari-ucp
[A Guide to Migrating from HikariCP to Oracle UCP (Universal Connection Pool)](https://juarezjunior.medium.com/e2f3fb6db2d5)
103 changes: 103 additions & 0 deletions java/springboot-hikari-ucp/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?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>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<groupId>com.oracle.dev.jdbc</groupId>
<artifactId>springboot-hikari-ucp</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springboot-hikari-ucp</name>
<description>A Guide to Migrating from HikariCP to Oracle UCP (Universal
Connection Pool)</description>
<url>https://juarezjunior.medium.com/e2f3fb6db2d5</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.3.0.23.09</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ucp11</artifactId>
<version>23.3.0.23.09</version>
</dependency>

<!-- Metrics -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${hibernate.version}</version>
<executions>
<execution>
<id>enhance</id>
<goals>
<goal>enhance</goal>
</goals>
<configuration>
<enableLazyInitialization>true</enableLazyInitialization>
<enableDirtyTracking>true</enableDirtyTracking>
<enableAssociationManagement>true</enableAssociationManagement>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>

</compilerArgs>
</configuration>
</plugin>

</plugins>
</build>


</project>
33 changes: 33 additions & 0 deletions java/springboot-hikari-ucp/script/UCPSpringBoot.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CREATE USER UCP_USER IDENTIFIED BY <YOUR_PASSWORD>;
GRANT DWROLE TO UCP_USER;
GRANT CREATE SESSION TO UCP_USER;
GRANT UNLIMITED TABLESPACE TO UCP_USER;
ALTER SESSION SET CURRENT_SCHEMA = UCP_USER;

CREATE TABLE Employee
(id NUMBER(10) CONSTRAINT pk_employee PRIMARY KEY,
name VARCHAR2(20),
job VARCHAR2(20),
salary NUMBER(10),
commission NUMBER(10));

INSERT INTO Employee VALUES(7369,'JOHN','CLERK',7902,NULL);
INSERT INTO Employee VALUES(7499,'PETER','SALESMAN',7698,300);
INSERT INTO Employee VALUES(7521,'JEFF','SALESMAN',7698,500);
INSERT INTO Employee VALUES(7566,'MARK','MANAGER',7839,NULL);
INSERT INTO Employee VALUES(7654,'MARTIN','SALESMAN',7698,1400);
INSERT INTO Employee VALUES(7698,'ADAM','MANAGER',7839,NULL);
INSERT INTO Employee VALUES(7782,'CLARK','MANAGER',7839,NULL);
INSERT INTO Employee VALUES(7788,'SCOTT','ANALYST',7566,NULL);
INSERT INTO Employee VALUES(7839,'KING','PRESIDENT',NULL);
INSERT INTO Employee VALUES(7844,'TURNER','SALESMAN',0);
INSERT INTO Employee VALUES(7876,'ADAMS','CLERK',7788,NULL);
INSERT INTO Employee VALUES(7900,'JAMES','CLERK',7698,NULL);
INSERT INTO Employee VALUES(7902,'FORD','ANALYST',7566,NULL);
INSERT INTO Employee VALUES(7934,'MILLER','CLERK',7782,NULL);

COMMIT;




Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright (c) 2021, 2022, 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.dev.jdbc.ucp.spring;

import java.util.Objects;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "EMPLOYEE", schema = "ADMIN")
//@Table(name = "EMPLOYEE", schema = "MYTODOLIST")
public class Employee {

private @Id @GeneratedValue Long id;
private String name;
private String job;
private Integer salary;
private Integer commission;

public Employee() {
}

public Employee(Long id, String name, String job, Integer salary, Integer commission) {
this.id = id;
this.name = name;
this.job = job;
this.salary = salary;
this.commission = commission;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getJob() {
return job;
}

public void setJob(String job) {
this.job = job;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}

public Integer getCommission() {
return commission;
}

public void setCommission(Integer commission) {
this.commission = commission;
}

@Override
public int hashCode() {
return Objects.hash(commission, id, job, name, salary);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
return Objects.equals(commission, other.commission) && Objects.equals(id, other.id)
&& Objects.equals(job, other.job) && Objects.equals(name, other.name) && Objects.equals(salary, other.salary);
}

public String toString() {
return String.format("%20s %20s %20s %20s %20s", id, name, job, salary, commission);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright (c) 2021, 2022, 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.dev.jdbc.ucp.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EmployeeApplication {

public static void main(String[] args) {
SpringApplication.run(EmployeeApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright (c) 2021, 2022, 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.dev.jdbc.ucp.spring;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
class EmployeeController {

private final EmployeeRepository repository;

EmployeeController(EmployeeRepository repository) {
this.repository = repository;
}

@GetMapping("/employees")
List<Employee> all() {
return repository.findAll();
}

@PostMapping("/employees")
Employee newEmployee(@RequestBody Employee newEmployee) {
return repository.save(newEmployee);
}

@GetMapping("/employees/{id}")
Employee one(@PathVariable Long id) {

return repository.findById(id).orElseThrow(() -> new EmployeeNotFoundException(id));
}

@PutMapping("/employees/{id}")
Employee replaceEmployee(@RequestBody Employee newEmployee, @PathVariable Long id) {

return repository.findById(id).map(employee -> {
employee.setName(newEmployee.getName());
employee.setJob(newEmployee.getJob());
return repository.save(employee);
}).orElseGet(() -> {
newEmployee.setId(id);
return repository.save(newEmployee);
});
}

@DeleteMapping("/employees/{id}")
void deleteEmployee(@PathVariable Long id) {
repository.deleteById(id);
}
}
Loading

0 comments on commit 8637e3d

Please sign in to comment.