Closed cotchan closed 2 years ago
2가지 설정을 수정
해야 합니다.hikari 내부로 옮깁니다.
@ConfigurationProperties
로 dataSource 정보를 읽어들일 때, 트리형태로 datasource 하위 키인 hikari
를 읽어들이지 않고, 같은 키 레벨에 설정된 곳에서 database url
, driver-class-name
, username
, password
및 기타 정보를 읽어들입니다.spring:
datasource:
hikari:
driver-class-name: org.h2.Driver
jdbc-url: jdbc:h2:tcp://localhost/~/daangn;MODE=MYSQL;
username: sa
password:
jdbc-url로 변경합니다.
CamelCase
에서 SnakeCase
로 자동으로 변환해줬습니다.spring:
jpa:
properties:
hibernate:
implicit_naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
physical_naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
package com.daangndaangn.common.configure;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration // Configuration 클래스임을 명시
@EnableJpaRepositories(
basePackages = "com.daangndaangn.common.api.repository", // 해당 DataSource를 적용할 패키지 경로, 해당 패키지가 적용될 repository
entityManagerFactoryRef = "apiEntityManager", // 해당 TransactionEntityManagerFactory가 사용할 메소드명
transactionManagerRef = "apiTransactionManager" // 해당 TransactinoManager가 참조할 메소드 명
)
public class ApiDbConfigure {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource apiDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.jpa")
public JpaProperties jpaProperties() {
return new JpaProperties();
}
@Bean
public LocalContainerEntityManagerFactoryBean apiEntityManager(EntityManagerFactoryBuilder builder,
DataSource dataSource,
JpaProperties jpaProperties) {
return builder
.dataSource(dataSource)
.properties(jpaProperties.getProperties())
.packages("com.daangndaangn.common.api.entity")
.persistenceUnit("default")
.build();
}
@Primary
@Bean(name = "apiTransactionManager")
public PlatformTransactionManager apiTransactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager(entityManagerFactory.getObject());
return transactionManager;
}
}
연동시킬 repositories 패키지
와 entityManagerFactoryRef, transactionManagerRef를 설정하는 부분입니다.spring.datasource.hikari에 설정한 구성 정보
를 가져와 dataSource 빈을 구성
합니다.spring.jpa에 설정한 jpa 관련 설정
을 읽어들여 jpa 설정
을 합니다.@Bean
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource apiDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.jpa")
public JpaProperties jpaProperties() {
return new JpaProperties();
}
dataSource
, jpaProperties
설정을 가져와 엔티티매니저를 만드는 entityManagerFactory 빈을 만듭니다.
@Bean
public LocalContainerEntityManagerFactoryBean apiEntityManager(EntityManagerFactoryBuilder builder,
DataSource dataSource,
JpaProperties jpaProperties) {
return builder
.dataSource(dataSource)
.properties(jpaProperties.getProperties())
.packages("com.daangndaangn.common.api.entity")
.persistenceUnit("default")
.build();
}
트랜젝션 처리에 이용되는 transactionManager 빈을 만듭니다.
@Bean(name = "apiTransactionManager")
public PlatformTransactionManager apiTransactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager(entityManagerFactory.getObject());
return transactionManager;
}
multi-document에 대해 ACID 트랜잭션을 지원
ChattingRoom
Document row 1개를 생성하고 이어서
Participant
Doucment row 2개를 생성합니다.ChattingRoom
을 생성한 후에 Participant
생성 과정에서 에러 발생 시 이미 생성한 ChattingRoom
도 롤백하기 위해 이걸 적용합니다.dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
}
Replica-Set
으로 띄워야합니다.$ mongod --dbpath=/Users/Chanyoung/data/db --replSet rs
// 몽고DB 접근 시
$ mongo
@Configuration
@EnableMongoRepositories(
basePackages = "com.daangndaangn.common.chat.repository"
)
public class MongoDbConfigure extends AbstractMongoClientConfiguration {
@Bean(name="mongoTransactionManager")
public MongoTransactionManager mongoTransactionManager(MongoDatabaseFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
}
@Override
protected String getDatabaseName() {
return "daangn_test";
}
}
@Configuration
@EnableMongoRepositories(basePackages = "com.baeldung.repository")
public class MongoConfig extends AbstractMongoClientConfiguration{
@Bean
MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
}
@Override
protected String getDatabaseName() {
return "test";
}
@Override
public MongoClient mongoClient() {
final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test");
final MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.build();
return MongoClients.create(mongoClientSettings);
}
}
작업 목적
mysql
,mongodb
를 사용하면서 각 각의 DB를 핸들링하는transactionManager
정의 필요multi document transaction
을 위해서는transactionManager
선언이 필수작업 내용 목차