Skip to content

Commit

Permalink
feat: jpa 및 transaction db 설정 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimdoha authored Dec 8, 2023
1 parent 4a0d32c commit 4df0fd4
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 23 deletions.
1 change: 0 additions & 1 deletion .github/CODEOWNERS

This file was deleted.

7 changes: 1 addition & 6 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
## 💡 개요
- Jira
## 🤔배경

### 📄 작업 사항



### ✔️ 코드 리뷰 반영 사항


### 📚 레퍼런스
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ out/
/.nb-gradle/

### VS Code ###
.vscode/
.vscode/

### ENV ###
.env
13 changes: 8 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'

// jpa + mysql
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'

// lombok
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

//actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3'
services:
mysql:
image: mysql:8
ports:
- 3306:3306
container_name: mysql
environment:
MYSQL_DATABASE: moneymong
MYSQL_ROOT_PASSWORD: 1234
volumes:
- ./mysqldata:/var/lib/mysql
restart: always
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AppApplication {
public class MoneymongApplication {

public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
SpringApplication.run(MoneymongApplication.class, args);
}

}
63 changes: 63 additions & 0 deletions src/main/java/com/moneymong/api/config/JPAConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.moneymong.api.config;

import com.zaxxer.hikari.HikariDataSource;
import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

@EnableJpaRepositories(
basePackages = {
"com.moneymong.api"
},
entityManagerFactoryRef = "moneyMongEntityManagerFactory",
transactionManagerRef = "moneyMongTransactionManager"
)
@Configuration(proxyBeanMethods = false)
public class JPAConfig {
@Bean
@ConfigurationProperties(prefix = "datasource.db")
public DataSource moneyMongDataSource() {
return new HikariDataSource();
}

@Primary
@Bean
@DependsOn("moneyMongDataSource")
public DataSource moneyMongLazyDataSource(
@Qualifier("moneyMongDataSource") DataSource dbDataSource
) {
return new LazyConnectionDataSourceProxy(dbDataSource);
}

@Primary
@Bean
public LocalContainerEntityManagerFactoryBean moneyMongEntityManagerFactory(
EntityManagerFactoryBuilder entityManagerFactoryBuilder,
@Qualifier("moneyMongLazyDataSource") DataSource lazyDataSource
) {
return entityManagerFactoryBuilder
.dataSource(lazyDataSource)
.packages("com.moneymong.api.domain")
.persistenceUnit("moneymong")
.build();
}

@Bean
public PlatformTransactionManager moneyMongTransactionManager(
@Qualifier("moneyMongEntityManagerFactory") EntityManagerFactory moneyMongEntityManagerFactory
) {
return new JpaTransactionManager(moneyMongEntityManagerFactory);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/moneymong/api/controller/TestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.moneymong.api.controller;

import com.moneymong.api.service.TestService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
public class TestController {
private final TestService testService;

@GetMapping("/test/create")
public void create(
@RequestParam("name") String name
) {
testService.create(name);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/moneymong/api/domain/TestEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.moneymong.api.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "test")
public class TestEntity {
@Id
@Column(name = "id")
Long id;

@Column(name = "name")
String name;


public static TestEntity createNew(String name) {
return TestEntity.builder()
.id(1L)
.name(name)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.moneymong.api.repository;

import com.moneymong.api.domain.TestEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TestRepository extends JpaRepository<TestEntity, Long> {
}
19 changes: 19 additions & 0 deletions src/main/java/com/moneymong/api/service/TestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.moneymong.api.service;

import com.moneymong.api.domain.TestEntity;
import com.moneymong.api.repository.TestRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class TestService {
private final TestRepository testRepository;

@Transactional
public TestEntity create(String name) {
TestEntity testEntity = TestEntity.createNew(name);
return testRepository.save(testEntity);
}
}
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

64 changes: 59 additions & 5 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,68 @@
spring:
config:
import: optional:file:.env[.properties]

server:
port: ${SERVER_PORT}
port: 8080
shutdown: graceful

spring:
profiles:
active: local

jpa:
open-in-view: false
show-sql: true
properties:
format_sql: true
hibernate:
ddl-auto: create


# actuator 관리
management:
endpoints:
web:
base-path: /
exposure:
include: health

# db 설정
datasource:
db:
pool-name: moneymong
jdbc-url: jdbc:mysql://localhost:3306/moneymong?useSSL=false&&allowPublicKeyRetrieval=true
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
maximum-pool-size: 5
connection-timeout: 3000
cache-prep-stmts: true
prep-stmt-cache-size: 250
prep-stmt-cache-sql-limit: 2048
use-server-prep-stmts: true
use-local-session-state: true
cache-result-set-metadata: true
maintain-time-stats: false
leak-detection-threshold: 5000
autocommit: false
connection-init-sql: "SET NAMES 'utf8mb4'"
read-only: false
data-source-properties:
useCursors: false
sendStringParametersAsUnicode: false
characterEncoding: utf8
zeroDateTimeBehavior: convertToNull
useSSL: false
autoReconnect: true
autoReconnectForPools: true
usePipelineAuth: false
useBatchMultiSend: false

---
spring:
config:
activate:
on-profile: local

---
spring:
config:
activate:
on-profile: dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AppApplicationTests {
class MoneymongApplicationTests {

@Test
void contextLoads() {
Expand Down

0 comments on commit 4df0fd4

Please sign in to comment.