Skip to content

Commit

Permalink
Change to a simple configuration with only one data source management.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkazama committed Nov 22, 2024
1 parent 9e3900b commit ad3397c
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 401 deletions.
74 changes: 4 additions & 70 deletions src/main/java/sample/ApplicationDbConfig.java
Original file line number Diff line number Diff line change
@@ -1,89 +1,23 @@
package sample;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import jakarta.persistence.EntityManagerFactory;
import sample.context.DomainHelper;
import sample.context.orm.OrmInterceptor;
import sample.context.orm.OrmRepository;
import sample.context.orm.repository.DefaultRepository;
import sample.context.orm.repository.SystemRepository;

/**
* Represents a database connection definition for an application.
*/
@Configuration
public class ApplicationDbConfig {

/** Represents a connection definition to a standard schema. */
@Configuration
static class DefaultDbConfig {

@Bean
@Primary
OrmRepository defaultRepository(DomainHelper dh, OrmInterceptor interceptor) {
return DefaultRepository.of(dh, interceptor);
}

@Bean(name = DefaultRepository.BeanNameDs, destroyMethod = "close")
@Primary
DataSource dataSource(ApplicationProperties props) {
return props.getDatasource().getApp().dataSource();
}

@Bean(name = DefaultRepository.BeanNameEmf)
@Primary
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
ApplicationProperties props,
@Qualifier(DefaultRepository.BeanNameDs) final DataSource dataSource) {
return props.getDatasource().getApp().entityManagerFactoryBean(dataSource);
}

@Bean(name = DefaultRepository.BeanNameTx)
@Primary
JpaTransactionManager transactionManager(
ApplicationProperties props,
@Qualifier(DefaultRepository.BeanNameEmf) final EntityManagerFactory emf) {
return props.getDatasource().getApp().transactionManager(emf);
}

}

/** Represents a connection definition to the system schema. */
@Configuration
static class SystemDbConfig {

@Bean
SystemRepository systemRepository(DomainHelper dh, OrmInterceptor interceptor) {
return SystemRepository.of(dh, interceptor);
}

@Bean(name = SystemRepository.BeanNameDs, destroyMethod = "close")
DataSource systemDataSource(ApplicationProperties props) {
return props.getDatasource().getSystem().dataSource();
}

@Bean(name = SystemRepository.BeanNameEmf)
LocalContainerEntityManagerFactoryBean systemEntityManagerFactoryBean(
ApplicationProperties props,
@Qualifier(SystemRepository.BeanNameDs) final DataSource dataSource) {
return props.getDatasource().getSystem().entityManagerFactoryBean(dataSource);
}

@Bean(name = SystemRepository.BeanNameTx)
JpaTransactionManager systemTransactionManager(
ApplicationProperties props,
@Qualifier(SystemRepository.BeanNameEmf) final EntityManagerFactory emf) {
return props.getDatasource().getSystem().transactionManager(emf);
}

@Bean
@Primary
OrmRepository defaultRepository(DomainHelper dh, OrmInterceptor interceptor) {
return OrmRepository.of(dh, interceptor);
}

}
9 changes: 0 additions & 9 deletions src/main/java/sample/ApplicationProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;
import sample.context.orm.repository.DefaultRepository.DefaultDataSourceProperties;
import sample.context.orm.repository.SystemRepository.SystemDataSourceProperties;

/**
* Represents the property concept of the application.
Expand All @@ -13,15 +11,8 @@
@Data
public class ApplicationProperties {
private boolean cors;
private DatasourceProps datasource = new DatasourceProps();
private MailProps mail;

@Data
public static class DatasourceProps {
private DefaultDataSourceProperties app = new DefaultDataSourceProperties();
private SystemDataSourceProperties system = new SystemDataSourceProperties();
}

@Data
public static class MailProps {
private boolean enabled;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/sample/context/audit/AuditHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
Expand All @@ -23,8 +22,8 @@
import sample.context.actor.ActorSession;
import sample.context.audit.AuditActor.RegAuditActor;
import sample.context.audit.AuditEvent.RegAuditEvent;
import sample.context.orm.OrmRepository;
import sample.context.orm.TxTemplate;
import sample.context.orm.repository.SystemRepository;

/**
* this component handle user audits and system audits (regular jobs, daily
Expand Down Expand Up @@ -220,8 +219,7 @@ public <T> T callEvent(String category, String message, Supplier<T> callable) {
@Component
@RequiredArgsConstructor(staticName = "of")
public static class AuditPersister {
private final SystemRepository rep;
@Qualifier(SystemRepository.BeanNameTx)
private final OrmRepository rep;
private final PlatformTransactionManager txm;

public AuditActor start(final RegAuditActor p) {
Expand Down
81 changes: 0 additions & 81 deletions src/main/java/sample/context/orm/OrmDataSourceProperties.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/sample/context/orm/OrmRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.LockModeType;
import jakarta.persistence.PersistenceContext;
import lombok.RequiredArgsConstructor;
import sample.context.DomainEntity;
import sample.context.DomainHelper;
Expand Down Expand Up @@ -163,4 +164,28 @@ public OrmRepository flushAndClear() {
return this;
}

public static OrmRepository of(DomainHelper dh, OrmInterceptor interceptor) {
return new DefaultRepository(dh, interceptor);
}

/** Repository of the standard schema. */
public static class DefaultRepository extends OrmRepository {

@PersistenceContext
private EntityManager em;

public DefaultRepository(DomainHelper dh, OrmInterceptor interceptor) {
super(dh, interceptor);
}

@Override
public EntityManager em() {
return em;
}

@Override
public void em(EntityManager em) {
this.em = em;
}
}
}
69 changes: 0 additions & 69 deletions src/main/java/sample/context/orm/OrmRepositoryProperties.java

This file was deleted.

Loading

0 comments on commit ad3397c

Please sign in to comment.