Skip to content

Commit

Permalink
PostgreSQL driver support for JPA/Hibernate in native image (#2596)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>
  • Loading branch information
Tomas-Kraus authored Dec 16, 2020
1 parent baf91a7 commit 23eb466
Show file tree
Hide file tree
Showing 13 changed files with 540 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ pipeline {
}
}
}
}
}
2 changes: 1 addition & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<version.lib.opentracing.grpc>0.2.1</version.lib.opentracing.grpc>
<version.lib.opentracing.tracerresolver>0.1.8</version.lib.opentracing.tracerresolver>
<version.lib.persistence-api>2.2.3</version.lib.persistence-api>
<version.lib.postgresql>42.2.16</version.lib.postgresql>
<version.lib.postgresql>42.2.18</version.lib.postgresql>
<version.lib.prometheus>0.9.0</version.lib.prometheus>
<version.lib.reactive-streams-tck>1.0.3</version.lib.reactive-streams-tck>
<version.lib.reactivestreams>1.0.3</version.lib.reactivestreams>
Expand Down
33 changes: 33 additions & 0 deletions etc/pods/pgsql.yaml
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"
8 changes: 6 additions & 2 deletions etc/scripts/test-integ-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ mvn ${MAVEN_ARGS} -f ${WS_DIR}/pom.xml \

# Run tests in Java VM application
(cd tests/integration/jpa && \
mvn ${MAVEN_ARGS} clean verify -Dmysql)
mvn ${MAVEN_ARGS} clean verify \
-Dmaven.test.failure.ignore=true -Dmysql \
-pl model,appl)

# Run tests in native image application
(cd tests/integration/jpa && \
mvn ${MAVEN_ARGS} clean verify -Dmysql -Pnative-image -Dnative-image)
mvn ${MAVEN_ARGS} clean verify \
-Dmaven.test.failure.ignore=true -Dmysql \
-Pnative-image -Dnative-image -pl model,appl)
63 changes: 63 additions & 0 deletions etc/scripts/test-integ-pgsql.sh
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)
86 changes: 86 additions & 0 deletions integrations/db/pgsql/pom.xml
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>
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;
}

}
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;
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"
]
}
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
Loading

0 comments on commit 23eb466

Please sign in to comment.