From c85217b2be0f8b15dca26df9814fd080928dd268 Mon Sep 17 00:00:00 2001 From: Wanja Zaeske Date: Mon, 29 Jan 2024 14:29:13 +0100 Subject: [PATCH 1/2] feat: add junixsocket to allow UDS psql connection This commit adds the `junixsocket` library to the dependencies. This allows the usage of a UNIX domain socket connection to a `postgresql` server. This promises both better performance, and it allows to use a Linux user identity for authentication against the DB (versus username/password auth for TCP/IP based communication to the DB). The change is non-intrusive, i.e. per default TCP/IP is still used and works as intended. For more information, checkout: - https://jdbc.postgresql.org/documentation/use/#unix-sockets - https://kohlschutter.github.io/junixsocket/dependency.html#postgresql In particular, setting the following two properties in the `persistence.xml` suffices to connect to a postgresql via a socket file: ``` javax.persistence.jdbc.url='jdbc:postgresql://localhost/db-name?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$SystemProperty' org.newsclub.net.unix.socket.default=/run/postgresql/.s.PGSQL.5432 ``` --- build.sbt | 1 + 1 file changed, 1 insertion(+) diff --git a/build.sbt b/build.sbt index 80054dd7..12ec88b0 100644 --- a/build.sbt +++ b/build.sbt @@ -18,6 +18,7 @@ libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.9. libraryDependencies += "com.fasterxml.jackson.datatype" % "jackson-datatype-hibernate5" % "2.9.8" libraryDependencies += "io.swagger" % "swagger-play2_2.12" % "1.6.0" libraryDependencies += "org.reflections" % "reflections" % "0.9.10" +libraryDependencies += "com.kohlschutter.junixsocket" % "junixsocket-native-common" % "2.8.3" javacOptions ++= Seq("-s", "app") From 809037cff1d6124e3ae1ca9811f5c16c63bf9eb3 Mon Sep 17 00:00:00 2001 From: Wanja Zaeske Date: Mon, 29 Jan 2024 14:38:01 +0100 Subject: [PATCH 2/2] feat: allow overriding of persistence properties Prior to this change, the only way to change the persistence settings was to edit the `persistence.xml`. In case of a pre-build .jar, the `persistence.xml` has to be contained in the .jar with the persistence module [1], and can not be overridden. However, the current repo hard-codes the DB connection details in the persistence.xml, which makes it impossible to override the DB connection details for a fully built .jar without changing that .jar file. This change enable the overriding of the properties used for the JPA `EntityManagerFactory` with system properties, so that DB connection details can be altered without changing the .jar file. [1] https://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_conf_specify.html --- app/jpa/manager/impl/HibernateManager.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/jpa/manager/impl/HibernateManager.java b/app/jpa/manager/impl/HibernateManager.java index 793d29c4..bf8df831 100644 --- a/app/jpa/manager/impl/HibernateManager.java +++ b/app/jpa/manager/impl/HibernateManager.java @@ -38,7 +38,8 @@ public class HibernateManager implements JPAManager { private final EntityManagerFactory entityManagerFactory; public HibernateManager() { - entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); + entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, + System.getProperties()); } @Override