Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: backport configurable DatabaseCtx #2653

Merged
merged 7 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ansible/roles/xroad-ss/templates/db.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
serverconf.hibernate.connection.url = jdbc:postgresql://{{database_host}}/serverconf
op-monitor.hibernate.connection.url = jdbc:postgresql://{{database_host}}/op-monitor
messagelog.hibernate.connection.url = jdbc:postgresql://{{database_host}}/messagelog
xroad.db.serverconf.hibernate.connection.url = jdbc:postgresql://{{database_host}}/serverconf
xroad.db.op-monitor.hibernate.connection.url = jdbc:postgresql://{{database_host}}/op-monitor
xroad.db.messagelog.hibernate.connection.url = jdbc:postgresql://{{database_host}}/messagelog
19 changes: 10 additions & 9 deletions src/addons/messagelog/messagelog-db/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
plugins {
id("xroad.java-conventions")
id("xroad.java-conventions")
alias(libs.plugins.jandex)
}

dependencies {
annotationProcessor(libs.mapstructProcessor)
annotationProcessor(libs.lombokMapstructBinding)
annotationProcessor(libs.mapstructProcessor)
annotationProcessor(libs.lombokMapstructBinding)

implementation(project(":common:common-db"))
implementation(project(":common:common-message"))
implementation(project(":common:common-messagelog"))
implementation(libs.bouncyCastle.bcpkix)
implementation(libs.slf4j.api)
implementation(libs.mapstruct)
implementation(project(":common:common-db"))
implementation(project(":common:common-message"))
implementation(project(":common:common-messagelog"))
implementation(libs.bouncyCastle.bcpkix)
implementation(libs.slf4j.api)
implementation(libs.mapstruct)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,31 @@
package ee.ria.xroad.messagelog.database;

import ee.ria.xroad.common.db.DatabaseCtx;
import ee.ria.xroad.common.db.TransactionCallback;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Disposes;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Named;
import org.niis.xroad.common.messagelog.MessageLogDbProperties;

/**
* Message log database context.
*/
public final class MessageLogDatabaseCtx {

private static final DatabaseCtx CTX = new DatabaseCtx("messagelog");
public class MessageLogDatabaseConfig {
public static final String MESSAGE_LOG_DB_CTX = "messageLogCtx";

private MessageLogDatabaseCtx() {
@Produces
@Named(MESSAGE_LOG_DB_CTX)
@ApplicationScoped
DatabaseCtx serverConfCtx(MessageLogDbProperties messageLogDbProperties) {
return create(messageLogDbProperties);
}

/**
* @return the current context.
*/
public static DatabaseCtx get() {
return CTX;
public static DatabaseCtx create(MessageLogDbProperties messageLogDbProperties) {
return new DatabaseCtx("messagelog", messageLogDbProperties.hibernate());
}

/**
* Convenience method for a transaction callback.
* @param <T> the type of result.
* @param callback the callback.
* @return the result.
* @throws Exception if an error occurs.
*/
public static <T> T doInTransaction(TransactionCallback<T> callback) throws Exception {
return CTX.doInTransaction(callback);
public void cleanup(@Named(MESSAGE_LOG_DB_CTX) @Disposes DatabaseCtx databaseCtx) {
databaseCtx.destroy();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,44 @@

import ee.ria.xroad.common.CodedException;

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.resource.transaction.spi.TransactionStatus;

import java.util.Map;

import static ee.ria.xroad.common.ErrorCodes.X_DATABASE_ERROR;
import static ee.ria.xroad.common.db.HibernateUtil.getSessionFactory;

/**
* Database context manages database connections for a specific session
* factory.
*/
@Slf4j
@RequiredArgsConstructor
@AllArgsConstructor
@Deprecated(forRemoval = true)
public class DatabaseCtx {
private final String name;
private final SessionFactory sessionFactory;

private final String sessionFactoryName;
public DatabaseCtx(String name, Map<String, String> hibernateProperties) {
this(name, hibernateProperties, null);
}

private Interceptor interceptor = null;
public DatabaseCtx(String name, Map<String, String> hibernateProperties, Interceptor interceptor) {
this.name = name;
this.sessionFactory = HibernateUtil.createSessionFactory(name, hibernateProperties, interceptor);
}

/**
* Gets called within a transactional context. Begins a transaction,
* calls the callback and then commits the transaction or rollbacks the
* transaction depending whether the callback finished successfully or
* threw an exception.
* @param <T> the type of result
*
* @param <T> the type of result
* @param callback the callback to call
* @return the result from the callback
* @throws Exception if an exception occurred
Expand Down Expand Up @@ -109,16 +114,16 @@ public <T> T doInTransaction(TransactionCallback<T> callback)
* @return the current session
*/
public Session getSession() {
return getSessionFactory(sessionFactoryName, interceptor)
.getCurrentSession();
return sessionFactory.getCurrentSession();
}

/**
* Starts a new transaction.
*
* @return the current session
*/
public Session beginTransaction() {
log.trace("beginTransaction({})", sessionFactoryName);
log.trace("beginTransaction({})", name);

Session session = getSession();
if (session.getTransaction().getStatus() == TransactionStatus.NOT_ACTIVE) {
Expand All @@ -132,7 +137,7 @@ public Session beginTransaction() {
* Commits the transaction.
*/
public void commitTransaction() {
log.trace("commitTransaction({})", sessionFactoryName);
log.trace("commitTransaction({})", name);

Transaction tx = getSession().getTransaction();
if (tx.getStatus() == TransactionStatus.ACTIVE) {
Expand All @@ -144,27 +149,35 @@ public void commitTransaction() {
* Rollbacks the transaction.
*/
public void rollbackTransaction() {
log.trace("rollbackTransaction({})", sessionFactoryName);
log.trace("rollbackTransaction({})", name);

Transaction tx = getSession().getTransaction();
if (tx.getStatus().canRollback()) {
tx.rollback();
}
}

/**
* Closes the session factory.
*/
public void closeSessionFactory() {
HibernateUtil.closeSessionFactory(sessionFactoryName);
}

private Exception customizeException(Exception e) {
if (e instanceof JDBCException) {
return new CodedException(X_DATABASE_ERROR,
"Error accessing database (%s)", sessionFactoryName);
"Error accessing database (%s)", name);
}

return e;
}

public void destroy() {
try {
sessionFactory.getCurrentSession().close();
} catch (HibernateException e) {
log.error("Error closing session", e);
}

try {
sessionFactory.close();
} catch (HibernateException e) {
log.error("Error closing session factory", e);
}
}
}
Loading
Loading