Notice
Recent Posts
Recent Comments
Link
반응형
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- jpa 환경에서 json type 활용하기
- 무료 인스턴스
- spring boot 멀티모듈
- aws cli s3
- 트랜잭션 전파속성
- h2 intellij
- 멀티모듈
- JSON type
- Oracle Linux 8
- aws cli s3 exclude directory
- jenkins 배포
- in memory
- AWS IAM MFA
- h2 연동
- json type column 생성
- oracle cloud
- aws cli s3 cp
- multi module
- json type index
- Jenkins
- aws cli s3 exclude folder
- json type 활용
- jpa json type
- IAM이란
- 프리티어 인스턴스
- 선언적 트랜잭션
- server mode
- Transactiona
- IAM MFA
- mysql json type
Archives
- Today
- Total
Chris Devlog
[H2] In-Memory feat.Spring boot, JPA, IntelliJ, tcp 본문
반응형
목적
H2 In Memory를 이용해 JVM내에 테스트용 DB를 생성하며, IntelliJ database를 통해 h2 콘솔 접속 없이 데이터 확인이 가능하도록 설정
* 단순히 테스트가 목적이므로 In Memory설정만으로도 상관없었지만, IntelliJ database에서 데이터를 확인하고싶어 여기까지 옴..
H2 연동방식
1. 직접 설치: 영구 저장
2. in-memory(local mode): 서버 종료 시 데이터 삭제, 외부 접속 X
3. server mode: tcp를 통해 우회하여 외부 접속이 가능하도록 처리
- In Memory
- File
준비물
- SpringBoot Project
Project 설정
build.gradle
- 의존성 추가
dependencies {
implementation 'com.h2database:h2'
}
application.yml
- MODE: mysql문법 사용
- DATABASE_TO_UPPER: H2 DB 명칭이 대문자 처리 방지
- ddl-auto: 매번 실행시 모든테이블 삭제후 재생성
- show-sql: sql 로그
- format_sql: sql 로깅시 이쁘게 보여줌
spring:
datasource:
continue-on-error: true
hikari:
jdbc-url: jdbc:h2:tcp://localhost:9092/mem:test;MODE=mysql;DATABASE_TO_UPPER=false
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
show-sql: true
properties:
hibernate:
format_sql: true
logging:
level:
org:
hibernate:
type:
descriptor:
sql: trace
H2ServerConfiguration.java
import com.zaxxer.hikari.HikariDataSource;
import java.sql.SQLException;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.h2.tools.Server;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
public class H2ServerConfiguration {
@Bean
@ConfigurationProperties("spring.datasource.hikari")
public DataSource dataSource() throws SQLException {
Server server = defaultRun(9092);
if (server.isRunning(true)) {
log.info("server run success");
}
log.info("h2 server url = {}", server.getURL());
return new HikariDataSource();
}
private Server defaultRun(int port) throws SQLException {
return Server.createTcpServer(
"-tcp",
"-tcpAllowOthers",
"-ifNotExists",
"-tcpPort", port + "").start();
}
}
Member.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
@Getter
@Entity(name = "member")
@NoArgsConstructor
@AllArgsConstructor
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", length = 50, nullable = false)
private String name;
@Column(name = "email", length = 200, nullable = false)
private String email;
@Column(name = "passwd", length = 50, nullable = false)
private String passwd;
@ColumnDefault("0")
@Column(name = "age", nullable = false)
private int age;
@Builder
public Member(String name, String email, String passwd, int age) {
this.name = name;
this.email = email;
this.passwd = passwd;
this.age = age;
}
서버 실행 후 로그 확인
IntelliJ Database 등록
Cmd+Shift+A(Window Ctrl+Shift+A) "database" 검색
URL 데이터 소스 -> URL/드라이버 입력
드라이버 -> H2 -> 버전 1.4.200
- 작성일(2022-07-13) 기준 다른 버전 선택 시 오류 발생
데이터 소스 -> 프로젝트 데이터 소스 -> 연결 타입 확인(URL only) -> 연결 테스트 success -> 확인
끝!
참조
- http://www.h2database.com/html/features.html#database_url
- https://jehuipark.github.io/java/springboot-h2-tcp-setup
Features
Features Feature List H2 in Use Connection Modes Database URL Overview Connecting to an Embedded (Local) Database In-Memory Databases Database Files Encryption Database File Locking Opening a Database Only if it Already Exists Closing a Database Ignore
www.h2database.com
Springboot 시작하기 쉬운 Database H2
Springboot에서 가장 빠르게 접근할 수 있는 RDBMS H2를 TCP모드로 셋업하는 과정을 공유합니다.
jehuipark.github.io
반응형
'Spring Boot' 카테고리의 다른 글
[Spring Boot] Multi Module Project feat. jdk17, Gradle 7.5.1 (0) | 2022.11.30 |
---|---|
선언적 트랜잭션(@Transactional)의 트랜잭션 전파속성 (3) | 2022.09.23 |