Skip to content

Commit

Permalink
MCR-2647 update database configuration (#935)
Browse files Browse the repository at this point in the history
* MCR-2647 update database configuration

automatically quote database identifier
fix dependencies
added migration scripts and new driver to docker image


---------

Co-authored-by: Sebastian Hofmann <[email protected]>
yagee-de and sebhofmann authored Dec 7, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 4028ddc commit d1e3383
Showing 8 changed files with 77 additions and 39 deletions.
75 changes: 61 additions & 14 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -59,8 +59,56 @@ function downloadDriver {
fi
}

function migrateC3P0toHikari {
# check if c3p0 is used (if any c3p0-* file present)
if ls "${MCR_CONFIG_DIR}lib/c3p0-"*.jar 1> /dev/null 2>&1; then
echo "Migrate from c3p0 to HikariCP"
# delete old c3p0 drivers
rm "${MCR_CONFIG_DIR}lib/c3p0-"*.jar
rm "${MCR_CONFIG_DIR}lib/mchange-commons-java-"*.jar
rm "${MCR_CONFIG_DIR}lib/hibernate-c3p0-"*.jar

# delete old database drivers
rm "${MCR_CONFIG_DIR}lib/postgresql-42.2.9.jar"
rm "${MCR_CONFIG_DIR}lib/mariadb-java-client-2.5.4.jar"
rm "${MCR_CONFIG_DIR}lib/h2-1.4.200.jar"
rm "${MCR_CONFIG_DIR}lib/mysql-connector-java-8.0.19.jar"

# delete old configuration and add new configuration
if grep -q "hibernate.c3p0" "${PERSISTENCE_XML}"; then
sed -ri "s/.*hibernate.c3p0.*//" "${PERSISTENCE_XML}"
sed -ri "s/(<property name=\"hibernate.connection.provider_class\" value=\")(.*)(\" \/>)/\1org.hibernate.hikaricp.internal.HikariCPConnectionProvider\3/" "${PERSISTENCE_XML}"
sed -ri "s/(<\/properties>)/<property name=\"hibernate.hikari.maximumPoolSize\" value=\"30\" \/>\n<property name=\"hibernate.hikari.minimumIdle\" value=\"2\" \/>\n<property name=\"hibernate.hikari.idleTimeout\" value=\"30000\" \/>\n<property name=\"hibernate.hikari.maxLifetime\" value=\"1800000\" \/>\n<property name=\"hibernate.hikari.leakDetectionThreshold\" value=\"9000\" \/>\n<property name=\"hibernate.hikari.registerMbeans\" value=\"true\" \/>\n\1/" "${PERSISTENCE_XML}"
fi

/opt/mir/mir/bin/mir.sh reload mappings in jpa configuration file
else
echo "No c3p0 driver found. Skip migration."
fi
}

function migrateJavaxPropertiesToJakarta() {
if grep -q "javax.persistence" "${PERSISTENCE_XML}"; then
echo "Migrate properties in persistence.xml from javax to jakarta"
sed -ri "s/(<property name=\")javax.persistence(.*\" value=\".*\" \/>)/\1jakarta.persistence\2/" "${PERSISTENCE_XML}"
fi
if grep -q "xmlns.jcp.org" "${PERSISTENCE_XML}"; then
echo "Migrate xmlns in persistence.xml from jcp.org to jakarta.ee"
sed -ri "s/xmlns=\".+persistence\"/xmlns=\"https:\/\/jakarta.ee\/xml\/ns\/persistence\"/" "${PERSISTENCE_XML}"
echo "Migrate schemaLocation in persistence.xml from jcp.org to jakarta.ee"
grep -ri "s/(xsi:schemaLocation=\").*jcp.org.*(\")/\1https:\/\/jakarta.ee\/xml\/ns\/persistence https:\/\/jakarta.ee\/xml\/ns\/persistence\/persistence_3_0.xsd\2/" "${PERSISTENCE_XML}"
fi
if grep -q "version=\"2" "${PERSISTENCE_XML}"; then
echo "Migrate version in persistence.xml from 2.* to 3.0"
sed -ri "s/version=\"2.*\"/version=\"3.0\"/" "${PERSISTENCE_XML}"
fi
}

function setDockerValues() {
echo "Set Docker Values to Config!"

migrateJavaxPropertiesToJakarta

if [ -n "${SOLR_URL}" ]; then
sed -ri "s/#?(MCR\.Solr\.ServerURL=).+/\1${SOLR_URL_ESCAPED}/" "${MYCORE_PROPERTIES}";
fi
@@ -74,19 +122,19 @@ function setDockerValues() {
fi

if [ -n "${JDBC_NAME}" ]; then
sed -ri "s/(name=\"javax.persistence.jdbc.user\" value=\").*(\")/\1${JDBC_NAME_ESCAPED}\2/" "${PERSISTENCE_XML}"
sed -ri "s/(name=\"jakarta.persistence.jdbc.user\" value=\").*(\")/\1${JDBC_NAME_ESCAPED}\2/" "${PERSISTENCE_XML}"
fi

if [ -n "${JDBC_PASSWORD}" ]; then
sed -ri "s/(name=\"javax.persistence.jdbc.password\" value=\").*(\")/\1${JDBC_PASSWORD_ESCAPED}\2/" "${PERSISTENCE_XML}"
sed -ri "s/(name=\"jakarta.persistence.jdbc.password\" value=\").*(\")/\1${JDBC_PASSWORD_ESCAPED}\2/" "${PERSISTENCE_XML}"
fi

if [ -n "${JDBC_DRIVER}" ]; then
sed -ri "s/(name=\"javax.persistence.jdbc.driver\" value=\").*(\")/\1${JDBC_DRIVER_ESCAPED}\2/" "${PERSISTENCE_XML}"
sed -ri "s/(name=\"jakarta.persistence.jdbc.driver\" value=\").*(\")/\1${JDBC_DRIVER_ESCAPED}\2/" "${PERSISTENCE_XML}"
fi

if [ -n "${JDBC_URL}" ]; then
sed -ri "s/(name=\"javax.persistence.jdbc.url\" value=\").*(\")/\1${JDBC_URL_ESCAPED}\2/" "${PERSISTENCE_XML}"
sed -ri "s/(name=\"jakarta.persistence.jdbc.url\" value=\").*(\")/\1${JDBC_URL_ESCAPED}\2/" "${PERSISTENCE_XML}"
fi

if [ -n "${SOLR_CLASSIFICATION_CORE}" ]; then
@@ -113,28 +161,27 @@ function setDockerValues() {
echo "MCR.Save.FileSystem=${MCR_SAVE_DIR}">>"${MYCORE_PROPERTIES}"
fi

migrateC3P0toHikari

case $JDBC_DRIVER in
org.postgresql.Driver) downloadDriver "https://jdbc.postgresql.org/download/postgresql-42.2.9.jar";;
org.mariadb.jdbc.Driver) downloadDriver "https://repo.maven.apache.org/maven2/org/mariadb/jdbc/mariadb-java-client/2.5.4/mariadb-java-client-2.5.4.jar";;
org.hsqldb.jdbcDriver) downloadDriver "https://repo.maven.apache.org/maven2/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0.jar";;
org.h2.Driver) downloadDriver "https://repo.maven.apache.org/maven2/com/h2database/h2/1.4.200/h2-1.4.200.jar";;
com.mysql.jdbc.Driver) downloadDriver "https://repo.maven.apache.org/maven2/mysql/mysql-connector-java/8.0.19/mysql-connector-java-8.0.19.jar";;
org.postgresql.Driver) downloadDriver "https://jdbc.postgresql.org/download/postgresql-42.7.0.jar";;
org.mariadb.jdbc.Driver) downloadDriver "https://repo.maven.apache.org/maven2/org/mariadb/jdbc/mariadb-java-client/3.3.0/mariadb-java-client-3.3.0.jar";;
org.h2.Driver) downloadDriver "https://repo1.maven.org/maven2/com/h2database/h2/2.2.224/h2-2.2.224.jar";;
com.mysql.jdbc.Driver) downloadDriver "https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/8.2.0/mysql-connector-j-8.2.0.jar";;
esac

mkdir -p "${MCR_CONFIG_DIR}lib"

downloadDriver https://repo1.maven.org/maven2/org/hibernate/hibernate-c3p0/5.3.9.Final/hibernate-c3p0-5.3.9.Final.jar
downloadDriver https://repo1.maven.org/maven2/com/mchange/c3p0/0.9.5.2/c3p0-0.9.5.2.jar
downloadDriver https://repo1.maven.org/maven2/com/mchange/mchange-commons-java/0.2.15/mchange-commons-java-0.2.15.jar
downloadDriver https://repo1.maven.org/maven2/com/zaxxer/HikariCP/5.1.0/HikariCP-5.1.0.jar
}

function setUpMyCoRe {
echo "Set up MyCoRe!"
/opt/mir/mir/bin/mir.sh create configuration directory
setDockerValues
setupLog4jConfig
sed -ri "s/<mapping-file>META-INF\/mycore-viewer-mappings.xml<\/mapping-file>/&\n <mapping-file>META-INF\/mir-module-mappings.xml<\/mapping-file>\n <mapping-file>META-INF\/mycore-acl-mappings.xml<\/mapping-file>\n <mapping-file>META-INF\/mycore-jobqueue-mappings.xml<\/mapping-file>/" "${MCR_CONFIG_DIR}resources/META-INF/persistence.xml"
sed -ri "s/(<\/properties>)/<property name=\"hibernate\.connection\.provider_class\" value=\"org\.hibernate\.connection\.C3P0ConnectionProvider\" \/>\n<property name=\"hibernate\.c3p0\.min_size\" value=\"2\" \/>\n<property name=\"hibernate\.c3p0\.max_size\" value=\"50\" \/>\n<property name=\"hibernate\.c3p0\.acquire_increment\" value=\"2\" \/>\n<property name=\"hibernate\.c3p0\.max_statements\" value=\"30\" \/>\n<property name=\"hibernate\.c3p0\.timeout\" value=\"1800\" \/>\n\1/" "${MCR_CONFIG_DIR}resources/META-INF/persistence.xml"
/opt/mir/mir/bin/mir.sh reload mappings in jpa configuration file
sed -ri "s/(<\/properties>)/<property name=\"hibernate.hikari.maximumPoolSize\" value=\"30\" \/>\n<property name=\"hibernate.hikari.minimumIdle\" value=\"2\" \/>\n<property name=\"hibernate.hikari.idleTimeout\" value=\"30000\" \/>\n<property name=\"hibernate.hikari.maxLifetime\" value=\"1800000\" \/>\n<property name=\"hibernate.hikari.leakDetectionThreshold\" value=\"9000\" \/>\n<property name=\"hibernate.hikari.registerMbeans\" value=\"true\" \/>\n\1/" "${MCR_CONFIG_DIR}resources/META-INF/persistence.xml"
/opt/mir/mir/bin/setup.sh
}

Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.jdbc.batch_size" value="0" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.auto_quote_keyword" value="true" />
</properties>
</persistence-unit>
</persistence>
13 changes: 5 additions & 8 deletions mir-module/pom.xml
Original file line number Diff line number Diff line change
@@ -114,10 +114,6 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@@ -174,10 +170,6 @@
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-hikaricp</artifactId>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
@@ -246,6 +238,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mycore</groupId>
<artifactId>mycore-csl</artifactId>
2 changes: 2 additions & 0 deletions mir-module/src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.jdbc.batch_size" value="0" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.globally_quoted_identifiers" value="true" />
<property name="hibernate.globally_quoted_identifiers_skip_column_definitions" value="true" />
</properties>
</persistence-unit>
</persistence>
8 changes: 4 additions & 4 deletions mir-wizard/src/main/resources/setup/dbtypes.xml
Original file line number Diff line number Diff line change
@@ -12,8 +12,8 @@
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://127.0.0.1/mir</url>
<library>
<url>https://jdbc.postgresql.org/download/postgresql-42.6.0.jar</url>
<url>https://repo1.maven.org/maven2/org/postgresql/postgresql/42.6.0/postgresql-42.6.0.jar</url>
<url>https://jdbc.postgresql.org/download/postgresql-42.7.0.jar</url>
<url>https://repo1.maven.org/maven2/org/postgresql/postgresql/42.6.0/postgresql-42.7.0.jar</url>
</library>
</db>
<db name="HSQLDB">
@@ -28,14 +28,14 @@
<driver>org.mariadb.jdbc.Driver</driver>
<url>jdbc:mariadb://127.0.0.1/mir</url>
<library>
<url>https://repo1.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/3.2.0/mariadb-java-client-3.2.0.jar</url>
<url>https://repo1.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/3.3.0/mariadb-java-client-3.3.0.jar</url>
</library>
</db>
<db name="MySQL">
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://127.0.0.1/mir</url>
<library>
<url>https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/8.1.0/mysql-connector-j-8.1.0.jar</url>
<url>https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/8.2.0/mysql-connector-j-8.2.0.jar</url>
</library>
</db>
</dbtypes>
2 changes: 2 additions & 0 deletions mir-wizard/src/main/resources/setup/template-persistence.xml
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@
<property name="jakarta.persistence.jdbc.password" />
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<!-- Automatically quote database identifiers that are keywords -->
<property name="hibernate.auto_quote_keyword" value="true" />
<property name="hibernate.default_schema" />
<property name="hibernate.default_catalog" />
<!-- Connection-Pool -->
2 changes: 2 additions & 0 deletions mir-wizard/src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.jdbc.batch_size" value="0" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.globally_quoted_identifiers" value="true" />
<property name="hibernate.globally_quoted_identifiers_skip_column_definitions" value="true" />
</properties>
</persistence-unit>
</persistence>
13 changes: 0 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -40,7 +40,6 @@
<changelog.since>2022-08-02</changelog.since>
<!-- until snapshot matches released version (required for 'mvn changes:changes-check') -->
<changes.version>2023.02</changes.version>
<hibernate.version>6.3.1.Final</hibernate.version>
<java.target.version>17</java.target.version>
<jetty.version>12.0.0</jetty.version>
<maven.compiler.arg>-Werror</maven.compiler.arg>
@@ -427,18 +426,6 @@
<artifactId>pica2mods-xslt</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>${hibernate.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mycore</groupId>
<artifactId>mycore-classifications</artifactId>

0 comments on commit d1e3383

Please sign in to comment.