Skip to content

Commit 7824758

Browse files
committed
Initial commit
1 parent 8ce8528 commit 7824758

File tree

5 files changed

+357
-0
lines changed

5 files changed

+357
-0
lines changed

recipes/hikari-ucp/.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
*.log
6+
7+
### IntelliJ IDEA ###
8+
.idea/modules.xml
9+
.idea/jarRepositories.xml
10+
.idea/compiler.xml
11+
.idea/libraries/
12+
*.iws
13+
*.iml
14+
*.ipr
15+
.idea
16+
.idea/**
17+
18+
### Eclipse ###
19+
.apt_generated
20+
.classpath
21+
.factorypath
22+
.project
23+
.settings
24+
.springBeans
25+
.sts4-cache
26+
27+
### NetBeans ###
28+
/nbproject/private/
29+
/nbbuild/
30+
/dist/
31+
/nbdist/
32+
/.nb-gradle/
33+
build/
34+
!**/src/main/**/build/
35+
!**/src/test/**/build/
36+
37+
### VS Code ###
38+
.vscode/
39+
/.jpb/**
40+
41+
### Mac OS ###
42+
.DS_Store

recipes/hikari-ucp/pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Copyright (c) 2025, Oracle and/or its affiliates. -->
3+
<!-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. -->
4+
<project xmlns="http://maven.apache.org/POM/4.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
7+
<modelVersion>4.0.0</modelVersion>
8+
9+
<groupId>atael.example</groupId>
10+
<artifactId>hikari-ucp</artifactId>
11+
<version>1.0-SNAPSHOT</version>
12+
13+
<properties>
14+
<java.version>17</java.version>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<rewrite.version>8.47.2</rewrite.version> <!-- Latest as of Feb 2025 -->
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.openrewrite</groupId>
24+
<artifactId>rewrite-java</artifactId>
25+
<version>${rewrite.version}</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.openrewrite</groupId>
30+
<artifactId>rewrite-properties</artifactId>
31+
<version>${rewrite.version}</version>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.openrewrite</groupId>
36+
<artifactId>rewrite-yaml</artifactId>
37+
<version>${rewrite.version}</version>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.openrewrite</groupId>
42+
<artifactId>rewrite-java</artifactId>
43+
<version>${rewrite.version}</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.openrewrite</groupId>
48+
<artifactId>rewrite-maven</artifactId>
49+
<version>${rewrite.version}</version>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.openrewrite.maven</groupId>
57+
<artifactId>rewrite-maven-plugin</artifactId>
58+
<version>6.2.1</version>
59+
<!-- <configuration>-->
60+
<!-- <exportDatatables>true</exportDatatables>-->
61+
<!-- <activeRecipes>-->
62+
<!-- <recipe>ConvertHikariToUCP</recipe>-->
63+
<!-- </activeRecipes>-->
64+
<!-- </configuration>-->
65+
<!-- <dependencies>-->
66+
<!-- <dependency>-->
67+
<!-- <groupId>org.openrewrite.recipe</groupId>-->
68+
<!-- <artifactId>rewrite-spring</artifactId>-->
69+
<!-- <version>6.1.0</version>-->
70+
<!-- </dependency>-->
71+
<!-- </dependencies>-->
72+
</plugin>
73+
74+
</plugins>
75+
</build>
76+
77+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2025, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
3+
4+
package oracle.com.cloud.recipes.hikariucp;
5+
6+
import org.openrewrite.NlsRewrite;
7+
import org.openrewrite.ExecutionContext;
8+
import org.openrewrite.Recipe;
9+
import org.openrewrite.TreeVisitor;
10+
import org.openrewrite.properties.PropertiesVisitor;
11+
import org.openrewrite.properties.tree.Properties;
12+
13+
public class ConvertMsToSecondsInPropertiesRecipe extends Recipe {
14+
15+
private final String keyRegex;
16+
17+
// Takes a keyRegex parameter to specify which property keys to target (e.g., Hikari timeout properties).
18+
public ConvertMsToSecondsInPropertiesRecipe(String keyRegex) {
19+
this.keyRegex = keyRegex;
20+
}
21+
22+
// Extends PropertiesVisitor to process each Properties.Entry (key-value pair) in application.properties.
23+
@Override
24+
public TreeVisitor<?, ExecutionContext> getVisitor() {
25+
26+
return new PropertiesVisitor<ExecutionContext>() {
27+
28+
@Override
29+
public Properties visitEntry(Properties.Entry entry, ExecutionContext ctx) {
30+
if (entry.getKey().matches(keyRegex)) {
31+
String value = entry.getValue().getText().trim();
32+
try {
33+
long ms = Long.parseLong(value);
34+
double seconds = ms / 1000.0;
35+
String newValue = String.valueOf(seconds);
36+
Properties.Value updatedValue = entry.getValue().withText(newValue);
37+
return entry.withValue(updatedValue);
38+
} catch (NumberFormatException e) {
39+
// If the value isn't a valid number, ignore it.
40+
}
41+
}
42+
return super.visitEntry(entry, ctx);
43+
}
44+
45+
};
46+
}
47+
48+
@Override
49+
public @NlsRewrite.DisplayName String getDisplayName() {
50+
return "Convert milliseconds to seconds for Hikari properties";
51+
}
52+
53+
@Override
54+
public @NlsRewrite.Description String getDescription() {
55+
return "Transforms millisecond values to seconds for Hikari connection pool properties matching the given regex.";
56+
}
57+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2025, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
3+
4+
package oracle.com.cloud.recipes.hikariucp;
5+
6+
import org.openrewrite.ExecutionContext;
7+
import org.openrewrite.NlsRewrite;
8+
import org.openrewrite.Recipe;
9+
import org.openrewrite.TreeVisitor;
10+
import org.openrewrite.yaml.YamlVisitor;
11+
import org.openrewrite.yaml.tree.Yaml;
12+
13+
import java.util.regex.Pattern;
14+
15+
public class ConvertMsToSecondsInYamlRecipe extends Recipe {
16+
17+
private final String pathRegex;
18+
19+
public ConvertMsToSecondsInYamlRecipe(String pathRegex) {
20+
this.pathRegex = pathRegex;
21+
}
22+
23+
@Override
24+
public TreeVisitor<?, ExecutionContext> getVisitor() {
25+
26+
Pattern pattern = Pattern.compile(pathRegex);
27+
28+
return new YamlVisitor<ExecutionContext>() {
29+
private String currentPath = "";
30+
31+
@Override
32+
public Yaml visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext ctx) {
33+
String previousPath = currentPath;
34+
currentPath += (currentPath.isEmpty() ? "" : ".") + entry.getKey().getValue();
35+
Yaml.Mapping.Entry updatedEntry = (Yaml.Mapping.Entry) super.visitMappingEntry(entry, ctx);
36+
currentPath = previousPath;
37+
return updatedEntry;
38+
}
39+
40+
@Override
41+
public Yaml visitScalar(Yaml.Scalar scalar, ExecutionContext ctx) {
42+
if (pattern.matcher(currentPath).matches()) {
43+
String value = scalar.getValue().trim();
44+
try {
45+
long ms = Long.parseLong(value);
46+
double seconds = ms / 1000.0;
47+
String newValue = String.valueOf(seconds);
48+
return scalar.withValue(newValue);
49+
} catch (NumberFormatException e) {
50+
// If the value isn't a valid number, ignore it.
51+
}
52+
}
53+
return super.visitScalar(scalar, ctx);
54+
}
55+
};
56+
57+
}
58+
59+
@Override
60+
public @NlsRewrite.DisplayName String getDisplayName() {
61+
return "";
62+
}
63+
64+
@Override
65+
public @NlsRewrite.Description String getDescription() {
66+
return "";
67+
}
68+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#
2+
# Copyright © 2025, Oracle and/or its affiliates.
3+
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
#
5+
# Source for the recipe:
6+
# https://github.com/brettwooldridge/HikariCP?tab=readme-ov-file#gear-configuration-knobs-baby
7+
---
8+
type: specs.openrewrite.org/v1beta/recipe
9+
name: oracle.com.cloud.recipes.hikariucp.ConvertHikariToUCP
10+
displayName: Migrate Hikari CP to Oracle UCP
11+
description: Migrate Hikari Connection Pool to Oracle Universal Connection Pool
12+
tags:
13+
- spring
14+
- oracle
15+
- ucp
16+
- hikari
17+
recipeList:
18+
19+
# For pom.xml
20+
- org.openrewrite.java.dependencies.RemoveDependency:
21+
groupId: com.zaxxer*
22+
artifactId: HikariCP*
23+
- org.openrewrite.java.dependencies.AddDependency:
24+
groupId: com.oracle.database.spring
25+
artifactId: oracle-spring-boot-starter-ucp
26+
version: 25.0.0
27+
28+
# spring.datasource.oracleucp.connection-factory-class: oracle.jdbc.pool.OracleDataSource
29+
- org.openrewrite.java.spring.ChangeSpringPropertyKey:
30+
oldPropertyKey: spring.datasource.hikari.data-source-class-name
31+
newPropertyKey: spring.datasource.oracleucp.connection-factory-class-name
32+
- org.openrewrite.java.spring.ChangeSpringPropertyValue:
33+
propertyKey: spring.datasource.oracleucp.connection-factory-class-name
34+
newValue: oracle.jdbc.pool.OracleDataSource
35+
36+
# spring.datasource.driver-class-name: OracleDriver
37+
- org.openrewrite.java.spring.ChangeSpringPropertyValue:
38+
propertyKey: spring.datasource.driver-class-name
39+
newValue: oracle.jdbc.OracleDriver
40+
41+
# spring.datasource.type: oracle.ucp.jdbc.PoolDataSource
42+
- org.openrewrite.java.spring.ChangeSpringPropertyValue:
43+
propertyKey: spring.datasource.type
44+
newValue: oracle.ucp.jdbc.PoolDataSource
45+
46+
# spring.datasource.oracleucp.connection-pool-name
47+
- org.openrewrite.java.spring.ChangeSpringPropertyKey:
48+
oldPropertyKey: spring.datasource.hikari.pool-name
49+
newPropertyKey: spring.datasource.oracleucp.connection-pool-name
50+
51+
# spring.datasource.oracleucp.max-pool-size
52+
- org.openrewrite.java.spring.ChangeSpringPropertyKey:
53+
oldPropertyKey: spring.datasource.hikari.maximum-pool-size
54+
newPropertyKey: spring.datasource.oracleucp.max-pool-size
55+
56+
# spring.datasource.oracleucp.min-pool-size
57+
- org.openrewrite.java.spring.ChangeSpringPropertyKey:
58+
oldPropertyKey: spring.datasource.hikari.minimum-idle
59+
newPropertyKey: spring.datasource.oracleucp.min-pool-size
60+
61+
# spring.datasource.oracleucp.connection-validation-timeout
62+
- org.openrewrite.java.spring.ChangeSpringPropertyKey:
63+
oldPropertyKey: spring.datasource.hikari.connection-timeout
64+
newPropertyKey: spring.datasource.oracleucp.connection-wait-timeout
65+
66+
# spring.datasource.oracleucp.inactive-connection-timeout
67+
- org.openrewrite.java.spring.ChangeSpringPropertyKey:
68+
oldPropertyKey: spring.datasource.hikari.idle-timeout
69+
newPropertyKey: spring.datasource.oracleucp.inactive-connection-timeout
70+
71+
# spring.datasource.oracleucp.s-q-l-for-validate-connection
72+
- org.openrewrite.java.spring.ChangeSpringPropertyKey:
73+
oldPropertyKey: spring.datasource.hikari.connection-test-query
74+
newPropertyKey: spring.datasource.oracleucp.s-q-l-for-validate-connection
75+
76+
# spring.datasource.oracleucp.max-connection-reuse-time
77+
- org.openrewrite.java.spring.ChangeSpringPropertyKey:
78+
oldPropertyKey: spring.datasource.hikari.max-lifetime
79+
newPropertyKey: spring.datasource.oracleucp.max-connection-reuse-time
80+
81+
# spring.datasource.oracleucp.max-connection-reuse-time
82+
- org.openrewrite.java.spring.ChangeSpringPropertyKey:
83+
oldPropertyKey: spring.datasource.hikari.validation-timeout
84+
newPropertyKey: spring.datasource.oracleucp.connection-validation-timeout
85+
86+
# spring.datasource.oracleucp.initial-pool-size
87+
- org.openrewrite.java.spring.AddSpringProperty:
88+
property: spring.datasource.oracleucp.initial-pool-size
89+
value: 5
90+
comment: "Specifies the number of available connections created after the pool is initiated"
91+
92+
# HikariCP properties that don’t have identical UCP properties
93+
- org.openrewrite.java.spring.CommentOutSpringPropertyKey:
94+
propertyKey: spring.datasource.hikari.auto-commit
95+
comment: "Use Oracle JDBC driver connection property autoCommit."
96+
- org.openrewrite.java.spring.CommentOutSpringPropertyKey:
97+
propertyKey: spring.datasource.hikari.register-mbeans
98+
comment: "UCP always attempts registration."
99+
- org.openrewrite.java.spring.CommentOutSpringPropertyKey:
100+
propertyKey: spring.datasource.hikari.thread-factory
101+
comment: "UCP supports setTaskManager instead."
102+
- org.openrewrite.java.spring.CommentOutSpringPropertyKey:
103+
propertyKey: spring.datasource.hikari.scheduled-executor
104+
comment: "UCP supports setTaskManager instead."
105+
- org.openrewrite.java.spring.CommentOutSpringPropertyKey:
106+
propertyKey: spring.datasource.hikari.keepalive-time
107+
comment: "Closest is to use driver connection properties oracle.net.keepAlive + oracle.net.TCP_KEEPIDLE"
108+
109+
# Convert milliseconds to seconds
110+
- oracle.com.cloud.recipes.hikariucp.ConvertMsToSecondsInPropertiesRecipe:
111+
keyRegex: 'spring\.datasource\.hikari\.(connectionTimeout|idleTimeout|maxLifetime|leakDetectionThreshold)'
112+
- oracle.com.cloud.recipes.hikariucp.ConvertMsToSecondsInYamlRecipe:
113+
pathRegex: 'spring\.datasource\.hikari\.(connectionTimeout|idleTimeout|maxLifetime|leakDetectionThreshold)'

0 commit comments

Comments
 (0)