-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PostgreSQL driver support for JPA/Hibernate in native image (#2596)
Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>
- Loading branch information
1 parent
baf91a7
commit 23eb466
Showing
13 changed files
with
540 additions
and
4 deletions.
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 |
---|---|---|
|
@@ -67,4 +67,4 @@ pipeline { | |
} | ||
} | ||
} | ||
} | ||
} |
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
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,33 @@ | ||
# | ||
# Copyright (c) 2018, 2020 Oracle and/or its affiliates. | ||
# | ||
# 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 | ||
# | ||
# http://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. | ||
# | ||
|
||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: pgsql | ||
spec: | ||
containers: | ||
- name: pgsql | ||
image: postgres | ||
ports: | ||
- containerPort: 5432 | ||
env: | ||
- name: POSTGRES_USER | ||
value: "user" | ||
- name: POSTGRES_PASSWORD | ||
value: "password" | ||
- name: POSTGRES_DB | ||
value: "pokemon" |
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
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,63 @@ | ||
#!/bin/bash -ex | ||
# | ||
# Copyright (c) 2018, 2020 Oracle and/or its affiliates. | ||
# | ||
# 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 | ||
# | ||
# http://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. | ||
# | ||
|
||
set -o pipefail || true # trace ERR through pipes | ||
set -o errtrace || true # trace ERR through commands and functions | ||
set -o errexit || true # exit the script if any statement returns a non-true return value | ||
|
||
on_error(){ | ||
CODE="${?}" && \ | ||
set +x && \ | ||
printf "[ERROR] Error(code=%s) occurred at %s:%s command: %s\n" \ | ||
"${CODE}" "${BASH_SOURCE}" "${LINENO}" "${BASH_COMMAND}" | ||
} | ||
trap on_error ERR | ||
|
||
# Path to this script | ||
if [ -h "${0}" ] ; then | ||
readonly SCRIPT_PATH="$(readlink "${0}")" | ||
else | ||
readonly SCRIPT_PATH="${0}" | ||
fi | ||
|
||
# Path to the root of the workspace | ||
readonly WS_DIR=$(cd $(dirname -- "${SCRIPT_PATH}") ; cd ../.. ; pwd -P) | ||
|
||
source ${WS_DIR}/etc/scripts/pipeline-env.sh | ||
|
||
JAVA_HOME='/tools/graalvm-ce-java11-20.2.0' | ||
PATH="${PATH}:${JAVA_HOME}/bin" | ||
|
||
mvn ${MAVEN_ARGS} --version | ||
|
||
# Temporary workaround until job stages will share maven repository | ||
mvn ${MAVEN_ARGS} -f ${WS_DIR}/pom.xml \ | ||
clean install -e \ | ||
-DskipTests \ | ||
-Ppipeline | ||
|
||
# Run tests in Java VM application | ||
(cd tests/integration/jpa && \ | ||
mvn ${MAVEN_ARGS} clean verify \ | ||
-Dmaven.test.failure.ignore=true -Dpgsql \ | ||
-pl model,appl) | ||
|
||
# Run tests in native image application | ||
(cd tests/integration/jpa && \ | ||
mvn ${MAVEN_ARGS} clean verify \ | ||
-Dmaven.test.failure.ignore=true -Dpgsql \ | ||
-Pnative-image -Dnative-image -pl model,appl) |
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,86 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2020 Oracle and/or its affiliates. | ||
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 | ||
http://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. | ||
--> | ||
|
||
<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>io.helidon.integrations.db</groupId> | ||
<artifactId>helidon-integrations-db-project</artifactId> | ||
<version>2.1.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>helidon-integrations-db-pgsql</artifactId> | ||
<name>Helidon Integrations DB PostgreSQL JDBC</name> | ||
<description>JDBC driver for PostgreSQL database with native-image support</description> | ||
|
||
<properties> | ||
<spotbugs.skip>true</spotbugs.skip> | ||
<maven.javadoc.skip>true</maven.javadoc.skip> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.graalvm.nativeimage</groupId> | ||
<artifactId>svm</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<profiles> | ||
<profile> | ||
<id>release</id> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>empty-javadoc-jar</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
<configuration> | ||
<classifier>javadoc</classifier> | ||
<classesDirectory>${project.build.directory}/javadoc</classesDirectory> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
</project> |
121 changes: 121 additions & 0 deletions
121
...s/db/pgsql/src/main/java/io/helidon/integrations/db/pgsql/BaseDataSourceSubstitution.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,121 @@ | ||
/* | ||
* Copyright (c) 2020 Oracle and/or its affiliates. | ||
* | ||
* 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 | ||
* | ||
* http://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 io.helidon.integrations.db.pgsql; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.lang.reflect.Field; | ||
import java.util.Properties; | ||
|
||
import com.oracle.svm.core.annotate.Alias; | ||
import com.oracle.svm.core.annotate.Delete; | ||
import com.oracle.svm.core.annotate.Substitute; | ||
import com.oracle.svm.core.annotate.TargetClass; | ||
import org.postgresql.ds.common.BaseDataSource; | ||
|
||
/** | ||
* Replace serialization code. | ||
*/ | ||
@TargetClass(className = "org.postgresql.ds.common.BaseDataSource") | ||
public final class BaseDataSourceSubstitution { | ||
|
||
@Alias | ||
private String[] serverNames; | ||
@Alias | ||
private String databaseName; | ||
@Alias | ||
private String user; | ||
@Alias | ||
private String password; | ||
@Alias | ||
private int[] portNumbers; | ||
@Alias | ||
private Properties properties; | ||
|
||
/* | ||
* Original PgSQL prototype uses Serialization to create deep clone of BaseDataSource | ||
* class. This replacement makes just shallow copy but it seems to be enough. | ||
* IOException is thrown to match prototype's failures. | ||
*/ | ||
/** | ||
* Initialize PgSQL {@code BaseDataSource} from another instance. | ||
* | ||
* @param source source instance | ||
* @throws IOException when any issue with data copying occurs | ||
*/ | ||
@Substitute | ||
public void initializeFrom(BaseDataSource source) throws IOException { | ||
final String[] serverNamesSrc = source.getServerNames(); | ||
final int[] portNumbersSrc = source.getPortNumbers(); | ||
if (serverNamesSrc != null) { | ||
serverNames = new String[serverNamesSrc.length]; | ||
for (int i = 0; i < serverNamesSrc.length; i++) { | ||
serverNames[i] = serverNamesSrc[i] != null ? serverNamesSrc[i] : null; | ||
} | ||
} else { | ||
serverNames = null; | ||
} | ||
databaseName = source.getDatabaseName(); | ||
user = source.getUser(); | ||
password = source.getPassword(); | ||
if (portNumbersSrc != null) { | ||
portNumbers = new int[portNumbersSrc.length]; | ||
System.arraycopy(portNumbersSrc, 0, portNumbers, 0, portNumbersSrc.length); | ||
} else { | ||
portNumbers = null; | ||
} | ||
try { | ||
Field propertiesField = BaseDataSource.class.getDeclaredField("properties"); | ||
boolean propertiesAcc = propertiesField.canAccess(source); | ||
propertiesField.setAccessible(true); | ||
final Properties propertiesSrc = (Properties) propertiesField.get(source); | ||
propertiesField.setAccessible(propertiesAcc); | ||
properties = new Properties(); | ||
properties.putAll(propertiesSrc); | ||
} catch (IllegalAccessException | NoSuchFieldException | SecurityException e) { | ||
throw new IOException("Could not initialize Properties class", e); | ||
} | ||
} | ||
|
||
/* | ||
* Removing writeBaseObject from original. It shall not be accessible now. | ||
*/ | ||
@Delete | ||
protected void writeBaseObject(ObjectOutputStream out) throws IOException { | ||
} | ||
|
||
/* | ||
* Removing readBaseObject from original. It shall not be accessible now. | ||
*/ | ||
@Delete | ||
protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException { | ||
} | ||
|
||
/** | ||
* Creates an instance of PgSQL BaseDataSource substitution class. | ||
* For testing purposes only. | ||
*/ | ||
BaseDataSourceSubstitution() { | ||
this.serverNames = null; | ||
this.databaseName = null; | ||
this.user = null; | ||
this.password = null; | ||
this.portNumbers = null; | ||
this.properties = null; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
integrations/db/pgsql/src/main/java/io/helidon/integrations/db/pgsql/package-info.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,20 @@ | ||
/* | ||
* Copyright (c) 2020 Oracle and/or its affiliates. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
/** | ||
* Substitutions for GraalVM native image for PostgreSQL database driver. | ||
*/ | ||
package io.helidon.integrations.db.pgsql; |
17 changes: 17 additions & 0 deletions
17
...grations/db/pgsql/src/main/resources/META-INF/helidon/native-image/reflection-config.json
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,17 @@ | ||
{ | ||
"classes": [ | ||
"org.postgresql.Driver", | ||
"org.postgresql.ds.common.BaseDataSource", | ||
"org.hibernate.dialect.PostgreSQL10Dialect", | ||
"org.hibernate.dialect.PostgreSQL81Dialect", | ||
"org.hibernate.dialect.PostgreSQL82Dialect", | ||
"org.hibernate.dialect.PostgreSQL91Dialect", | ||
"org.hibernate.dialect.PostgreSQL92Dialect", | ||
"org.hibernate.dialect.PostgreSQL93Dialect", | ||
"org.hibernate.dialect.PostgreSQL94Dialect", | ||
"org.hibernate.dialect.PostgreSQL95Dialect", | ||
"org.hibernate.dialect.PostgreSQL9Dialect", | ||
"org.hibernate.dialect.PostgreSQLDialect", | ||
"org.hibernate.dialect.PostgresPlusDialect" | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
.../resources/META-INF/native-image/io.helidon.integrations.db/pgsql/native-image.properties
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,17 @@ | ||
# | ||
# Copyright (c) 2020 Oracle and/or its affiliates. | ||
# | ||
# 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 | ||
# | ||
# http://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. | ||
# | ||
|
||
Args= --initialize-at-run-time=org.postgresql.sspi.SSPIClient |
Oops, something went wrong.