diff --git a/mens-api/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java b/mens-api/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java index ebfd8c8..ceb36d7 100644 --- a/mens-api/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java +++ b/mens-api/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java @@ -41,7 +41,7 @@ import javax.sql.DataSource; public class DatasourceConfig { @Bean(name = "primaryHikariConfig") @Primary - @ConfigurationProperties(prefix = "spring.datasource.hikari.maria") + @ConfigurationProperties(prefix = "spring.datasource.hikari.primary") public HikariConfig primaryHikariConfig() { // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); // hikariConfig.setAutoCommit(false); diff --git a/mens-api/src/main/resources/config/application-local.yml b/mens-api/src/main/resources/config/application-local.yml index 6a7e069..ba79dd2 100644 --- a/mens-api/src/main/resources/config/application-local.yml +++ b/mens-api/src/main/resources/config/application-local.yml @@ -5,7 +5,7 @@ spring: datasource: hikari: # 9 server - maria: + primary: driver-class-name: org.mariadb.jdbc.Driver jdbc-url: jdbc:mariadb://211.119.124.9:4407/mens?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&autoReconnect=true username: root diff --git a/mens-batch/src/main/java/kr/xit/biz/ens/model/EnsDTO.java b/mens-batch/src/main/java/kr/xit/biz/ens/model/EnsDTO.java index 1c0ff89..2a22b14 100644 --- a/mens-batch/src/main/java/kr/xit/biz/ens/model/EnsDTO.java +++ b/mens-batch/src/main/java/kr/xit/biz/ens/model/EnsDTO.java @@ -1,6 +1,7 @@ package kr.xit.biz.ens.model; import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import io.swagger.v3.oas.annotations.media.Schema; @@ -841,7 +842,8 @@ public class EnsDTO { * 등록 일시 */ @JsonDeserialize(using = LocalDateDeserializer.class) - @JsonFormat(pattern = "yyyy-MM-dd kk:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd kk:mm:ss.SSS") + @JsonIgnore private LocalDateTime registDt; /** * 등록자 diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDatasourceConfig.java similarity index 57% rename from mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java rename to mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDatasourceConfig.java index 290867b..1ded30d 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/DatasourceConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryDatasourceConfig.java @@ -1,47 +1,44 @@ -package kr.xit.core.spring.config.support; +package kr.xit.core.spring.config.db; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; import kr.xit.core.consts.Constants; -import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; - -import javax.sql.DataSource; -//FIXME :: 재설정이 필요한 경우 해당 프로젝트에 동일한 파일로 재정의 하여 사용 /** *
- * description : Datasource 설정 - FIXME:: spring.datasource 설정이 있는 경우만 loading - * - 조건 : spring.datasource + * description : Datasource 설정 - spring.datasource.hikari.primary + * - 조건 : spring.datasource.hikari.primary.username 이 있는 경우 * 실제 필요한 경우만 커넥션을 점유하도록 * LazyConnectionDataSourceProxy 사용 * 일반 Datasource 사용시 * - Spring은 트랜잭션에 진입시 데이타 소스의 커넥션을 get * - ehcache, hibernate 영속성 컨택슽트 1차캐시 등에도 커넥션을 get * - multi-datasource 에서 트랜잭션 진입 이후 datasource 분기 불가 - * packageName : kr.xit.core.spring.config.support - * fileName : DatasourceConfig + * packageName : kr.xit.core.spring.config.db + * fileName : PrimaryDatasourceConfig * author : julim * date : 2023-04-28 * ====================================================================== * 변경일 변경자 변경 내용 * ---------------------------------------------------------------------- * 2023-04-28 julim 최초 생성 + * 2023-10-30 julim dynamic 설정 적용 * ** @see PrimaryMybatisConfig */ -//@ConditionalOnProperty(value = "spring", havingValue = "datasource", matchIfMissing = false) -@Slf4j @Configuration -public class DatasourceConfig { +@ConditionalOnProperty(value = "spring.datasource.hikari.primary.username") +public class PrimaryDatasourceConfig { @Bean(name = "primaryHikariConfig") @Primary - @ConfigurationProperties(prefix = "spring.datasource.hikari.maria") + @ConfigurationProperties(prefix = "spring.datasource.hikari.primary") public HikariConfig primaryHikariConfig() { // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); // hikariConfig.setAutoCommit(false); @@ -51,23 +48,8 @@ public class DatasourceConfig { @Bean(Constants.PRIMARY_DATA_SOURCE) @Primary - public DataSource primaryDataSource() throws Exception{ + public DataSource primaryDataSource() { //return new LazyConnectionDataSourceProxy(new HikariDataSource(primaryHikariConfig())); return new HikariDataSource(primaryHikariConfig()); } - - @Bean(name = "secondaryHikariConfig") - @ConfigurationProperties(prefix = "spring.datasource.hikari.oracle") - public HikariConfig secondaryHikariConfig() { - // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); - // hikariConfig.setAutoCommit(false); - // return hikariConfig; - return new HikariConfig(); - } - - @Bean(name = Constants.SECONDARY_DATA_SOURCE) - public DataSource secondaryDataSource() throws Exception{ - //return new LazyConnectionDataSourceProxy(new HikariDataSource(secondaryHikariConfig())); - return new HikariDataSource(secondaryHikariConfig()); - } } diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/PrimaryMybatisConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryMybatisConfig.java similarity index 75% rename from mens-batch/src/main/java/kr/xit/core/spring/config/support/PrimaryMybatisConfig.java rename to mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryMybatisConfig.java index d2b6133..7f97479 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/PrimaryMybatisConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/PrimaryMybatisConfig.java @@ -1,41 +1,40 @@ -package kr.xit.core.spring.config.support; +package kr.xit.core.spring.config.db; +import javax.sql.DataSource; import kr.xit.core.consts.Constants; -import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.annotation.*; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.support.lob.DefaultLobHandler; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import javax.sql.DataSource; -//FIXME :: 재설정이 필요한 경우 해당프로젝트에 동일한 파일로 재정의 하여 사용 /** *
- * description : Mybatis 설정 - FIXME:: @DependsOn(value = {"dataSource"}) loading - * - 조건 : @DependsOn(value = {"dataSource"}) - * packageName : kr.xit.core.spring.config.support - * fileName : MybatisConfig + * description : Mybatis 설정 - spring.datasource.hikari.primary + * - 조건 : spring.datasource.hikari.primary.username이 있는 경우 loading + * packageName : kr.xit.core.spring.config.db + * fileName : PrimaryMybatisConfig * author : julim * date : 2023-04-28 * ====================================================================== * 변경일 변경자 변경 내용 * ---------------------------------------------------------------------- * 2023-04-28 julim 최초 생성 + * 2023-10-30 julim dynamic 설정 적용 * *- * @see DatasourceConfig + * @see PrimaryDatasourceConfig */ -@Slf4j @Configuration +@ConditionalOnProperty(value = "spring.datasource.hikari.primary.username") @MapperScan( basePackages = { "kr.xit.core.**.mapper", diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDatasourceConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDatasourceConfig.java new file mode 100644 index 0000000..59982fb --- /dev/null +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryDatasourceConfig.java @@ -0,0 +1,52 @@ +package kr.xit.core.spring.config.db; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; +import kr.xit.core.consts.Constants; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + *
+ * description : Datasource 설정 - spring.datasource.hikari.secondary + * - 조건 : spring.datasource.hikari.secondary.username 이 있는 경우 + * 실제 필요한 경우만 커넥션을 점유하도록 + * LazyConnectionDataSourceProxy 사용 + * 일반 Datasource 사용시 + * - Spring은 트랜잭션에 진입시 데이타 소스의 커넥션을 get + * - ehcache, hibernate 영속성 컨택슽트 1차캐시 등에도 커넥션을 get + * - multi-datasource 에서 트랜잭션 진입 이후 datasource 분기 불가 + * packageName : kr.xit.core.spring.config.db + * fileName : SecondaryMybatisConfig + * author : julim + * date : 2023-04-28 + * ====================================================================== + * 변경일 변경자 변경 내용 + * ---------------------------------------------------------------------- + * 2023-04-28 julim 최초 생성 + * 2023-10-30 julim dynamic 설정 적용 + * + *+ * @see SecondaryMybatisConfig + */ +@Configuration +@ConditionalOnProperty(value = "spring.datasource.hikari.secondary.username") +public class SecondaryDatasourceConfig { + @Bean(name = "secondaryHikariConfig") + @ConfigurationProperties(prefix = "spring.datasource.hikari.secondary") + public HikariConfig secondaryHikariConfig() { + // HikariConfig hikariConfig = new HikariConfig("spring.datasource.hikari"); + // hikariConfig.setAutoCommit(false); + // return hikariConfig; + return new HikariConfig(); + } + + @Bean(name = Constants.SECONDARY_DATA_SOURCE) + public DataSource secondaryDataSource() { + //return new LazyConnectionDataSourceProxy(new HikariDataSource(secondaryHikariConfig())); + return new HikariDataSource(secondaryHikariConfig()); + } +} diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryMybatisConfig.java similarity index 71% rename from mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java rename to mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryMybatisConfig.java index 808e8f0..db73dc4 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/SecondaryMybatisConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/SecondaryMybatisConfig.java @@ -1,54 +1,43 @@ -package kr.xit.core.spring.config.support; +package kr.xit.core.spring.config.db; +import javax.sql.DataSource; import kr.xit.core.consts.Constants; -import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Lazy; -import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.jdbc.support.lob.DefaultLobHandler; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import javax.sql.DataSource; - -//FIXME :: 재설정이 필요한 경우 해당프로젝트에 동일한 파일로 재정의 하여 사용 /** *
- * description : Mybatis 설정 - FIXME:: @DependsOn(value = {"dataSource"}) loading - * - 조건 : @DependsOn(value = {"dataSource"}) - * packageName : kr.xit.core.spring.config.support - * fileName : MybatisConfig + * description : Mybatis 설정 - spring.datasource.hikari.secondary + * - 조건 : spring.datasource.hikari.secondary.username이 있는 경우 loading + * packageName : kr.xit.core.spring.config.db + * fileName : SecondaryMybatisConfig * author : julim * date : 2023-04-28 * ====================================================================== * 변경일 변경자 변경 내용 * ---------------------------------------------------------------------- * 2023-04-28 julim 최초 생성 + * 2023-10-30 julim dynamic 설정 적용 * *- * @see DatasourceConfig + * @see SecondaryDatasourceConfig */ -@Slf4j @Configuration +@ConditionalOnProperty(value = "spring.datasource.hikari.secondary.username") @MapperScan( basePackages = {"kr.xit.biz.sms.mapper"}, sqlSessionFactoryRef = Constants.SECONDARY_SQL_SESSION ) public class SecondaryMybatisConfig { - static final String MYBATIS_CONFIG_FILE = "classpath:/egovframework/mapper/mapper-config.xml"; - @Bean(name = Constants.SECONDARY_SQL_SESSION) public SqlSessionFactory secondarySqlSession(@Qualifier(Constants.SECONDARY_DATA_SOURCE) DataSource dataSource) throws Exception { PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); @@ -59,6 +48,7 @@ public class SecondaryMybatisConfig { return sessionFactory.getObject(); } + //@ConditionalOnProperty(value = "spring.datasource.hikari.secondary") @Bean(name = "secondarySqlSessionTemplate") public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier(Constants.SECONDARY_SQL_SESSION) SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); diff --git a/mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java b/mens-batch/src/main/java/kr/xit/core/spring/config/db/TransactionConfig.java similarity index 61% rename from mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java rename to mens-batch/src/main/java/kr/xit/core/spring/config/db/TransactionConfig.java index 9ab3c26..84b83b7 100644 --- a/mens-batch/src/main/java/kr/xit/core/spring/config/support/TransactionConfig.java +++ b/mens-batch/src/main/java/kr/xit/core/spring/config/db/TransactionConfig.java @@ -1,8 +1,10 @@ -package kr.xit.core.spring.config.support; +package kr.xit.core.spring.config.db; +import javax.sql.DataSource; import kr.xit.core.consts.Constants; -import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -11,15 +13,13 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; -import javax.sql.DataSource; - //FIXME :: 재설정이 필요한 경우 해당프로젝트에 동일한 파일로 재정의 하여 사용 /** *
- * description : Mybatis 설정 - FIXME:: @DependsOn(value = {"dataSource"}) loading - * - 조건 : @DependsOn(value = {"dataSource"}) - * packageName : kr.xit.core.spring.config.support + * description : Mybatis 설정 + * - single DB or multi DB 설정에 따라 transaction 설정 적용 + * packageName : kr.xit.core.spring.config.db * fileName : MybatisConfig * author : julim * date : 2023-04-28 @@ -27,33 +27,49 @@ import javax.sql.DataSource; * 변경일 변경자 변경 내용 * ---------------------------------------------------------------------- * 2023-04-28 julim 최초 생성 + * 2023-10-30 julim dynamic 설정 적용 * *- * @see DatasourceConfig + * @see PrimaryDatasourceConfig + * @see SecondaryMybatisConfig */ -@Slf4j @Configuration @EnableTransactionManagement public class TransactionConfig { + + /** + * primary DB 만 선언 된 경우 + * @param ds DataSource + * @return PlatformTransactionManager + */ + @ConditionalOnMissingBean(SecondaryMybatisConfig.class) + @Primary + @Bean + public PlatformTransactionManager primaryTransactionManager(@Qualifier(Constants.PRIMARY_DATA_SOURCE)DataSource ds) { + DataSourceTransactionManager dstm = new DataSourceTransactionManager(ds); + dstm.setGlobalRollbackOnParticipationFailure(false); + return dstm; + } + //////////////////////////////////////////////////////////////////////////////////////////// // ChainedTransactionManager : trsnsaction binding /////////////////////////////////////////////////////////////////////////////////////////// /** - * mariaDB & oracleDB Transaction binding - * @param mariaDS Maria DataSource - * @param oracleDS Oracle DataSource + * primary DB & secondary DB Transaction binding + * @param primaryDS primary DataSource + * @param secondaryDS secondary DataSource * @return PlatformTransactionManager - * @throws Exception Exception */ + @ConditionalOnProperty(value = "spring.datasource.hikari.secondary.username") @Primary @Bean - public PlatformTransactionManager transactionManager(@Qualifier(Constants.PRIMARY_DATA_SOURCE)DataSource mariaDS, - @Qualifier(Constants.SECONDARY_DATA_SOURCE)DataSource oracleDS) { - DataSourceTransactionManager mariaTm = new DataSourceTransactionManager(mariaDS); + public PlatformTransactionManager transactionManager(@Qualifier(Constants.PRIMARY_DATA_SOURCE)DataSource primaryDS, + @Qualifier(Constants.SECONDARY_DATA_SOURCE) DataSource secondaryDS) { + DataSourceTransactionManager mariaTm = new DataSourceTransactionManager(primaryDS); mariaTm.setGlobalRollbackOnParticipationFailure(false); mariaTm.setNestedTransactionAllowed(true); - DataSourceTransactionManager oracleTm = new DataSourceTransactionManager(oracleDS); + DataSourceTransactionManager oracleTm = new DataSourceTransactionManager(secondaryDS); oracleTm.setGlobalRollbackOnParticipationFailure(false); oracleTm.setNestedTransactionAllowed(true); diff --git a/mens-batch/src/main/resources/META-INF/spring.factories b/mens-batch/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..0c0d27a --- /dev/null +++ b/mens-batch/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +#org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +# kr.xit.core.spring.auto.PrimaryDatabaseAutoConfiguration, kr.xit.core.spring.auto.SecondaryDatabaseAutoConfiguration] +# diff --git a/mens-batch/src/main/resources/config/application-local.yml b/mens-batch/src/main/resources/config/application-local.yml index 3a94df3..ef0cb5a 100644 --- a/mens-batch/src/main/resources/config/application-local.yml +++ b/mens-batch/src/main/resources/config/application-local.yml @@ -9,7 +9,7 @@ spring: # root / xit1807 hikari: # 9 server - maria: + primary: driver-class-name: org.mariadb.jdbc.Driver jdbc-url: jdbc:mariadb://211.119.124.9:4407/mens?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&autoReconnect=true username: root @@ -19,7 +19,7 @@ spring: # 115 server # jdbc:oracle:thin:@211.119.124.115:1521:XITSMS # xit_sms_lg / xit_sms_lg - oracle: + secondary: driver-class-name: oracle.jdbc.OracleDriver jdbc-url: jdbc:oracle:thin:@211.119.124.115:1521:XITSMS username: xit_sms_lg diff --git a/mens-batch/src/main/resources/egovframework/mapper/biz/ens-mysql-mapper.xml b/mens-batch/src/main/resources/egovframework/mapper/biz/ens-mysql-mapper.xml index f01b284..f3bd866 100644 --- a/mens-batch/src/main/resources/egovframework/mapper/biz/ens-mysql-mapper.xml +++ b/mens-batch/src/main/resources/egovframework/mapper/biz/ens-mysql-mapper.xml @@ -652,7 +652,7 @@ AND tesm.ffnlg_code = #{ffnlgCode} -