이번에 회사에서 DB 서버를 AWS RDS로 단계적으로 이전하던 도중에 프로덕션 데이터가 소실되는 상황이 발생하였고 다행히 바이너리 로그를 보관하는 기간안이어서 MySQL 바이너리 로그로 복구를 하였는데 이때 학승하였던것을 기록차 남긴다.

  1. 프로덕션에서 Binlog 있는지 확인
SHOW BINARY LOGS;
+---------------+-----------+
| Log_name      | File_size |
+---------------+-----------+
| binlog.000015 |    724935 |
| binlog.000016 |    733481 |
+---------------+-----------+
  1. Binlog 덤프진행
    mysqlbinlog --read-from-remote-server --host=<프로덕션호스트> --raw binlog.000001 -u<계정> -p
  2. Binlog 에서 INSERT 및 UPDATE 추출하기 위해 파일 파싱
    mysqlbinlog binlog.000001 -d <DB명> --base64-output=AUTO --verbose >> output.txt
  3. 분석 및 SQL 문으로 수기로 변환하고 INSERT 와 UPDATE 실행

다음에 진행한다면 다르게 했을것

  1. 추후에는 같은 이슈 발생시 GitHub - danfengcao/binlog2sql: Parse MySQL binlog to SQL you want 사용가능해보인다. DDL 과 DML 쿼리문을 바로 사용할수 있게 파싱해서 사용해준다
  2. Binlog 를 사실 Point-in-time recovery 로 binlog를 바로 사용하고 시간대를 지정하여 지원하는데 복구가 가능한데 수동으로 쿼리문을 파싱하지 않아도 복구가 가능하다

복구 진행시 참조글

  1. https://dev.mysql.com/doc/refman/8.0/en/mysqlbinlog.html
  2. Accessing MySQL binary logs - Amazon Relational Database Service
  3. https://www.percona.com/blog/binlog2sql-binlog-to-raw-sql-conversion-and-point-in-time-recovery/

이미 현업에서는 자주 사용하지만 따로 정리하지는 않아서 이번기회에 정식 레퍼런스를 보면서 내용을 찾아보았다

스프링 트랜잭션 처리

https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#transaction-declarative-attransactional-settings

 

Data Access

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies (such as JDBC, Hibernate, or JPA) in a consistent way. This lets you switch between the aforementioned persistence technologies fairly easily, a

docs.spring.io

The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics (for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction"). The default @Transactional settings are as follows:

 

  • The propagation setting is PROPAGATION_REQUIRED.
  • The isolation level is ISOLATION_DEFAULT.
  • The transaction is read-write.
  • The transaction timeout defaults to the default timeout of the underlying transaction system, or to none if timeouts are not supported.
  • Any RuntimeException triggers rollback, and any checked Exception does not

스프링 트랜잭션 처리중에 예외가 발생했을때 명시적으로 아래의 옵션들을 사용해서 롤백 여부를 결정할수 있다

rollbackFor 롤백을 실행시키는 예외 클래스 목록들
rollbackForClassName 롤백을 실행시키는 예외 클래스 이름들
noRollbackFor 롤백을 실행시키지 않는 예외 클랙스 목록들
noRollbackForClassName 롤백을 실행시키지 않는 예외 클래스 이름들

 

자바 vs 스프링 트랙잭션 처리에 대한 오해

구글에서 자바 트랜잭션 처리로 검색해 보면 Checked vs Unchecekd exception 에 대해 비교하고 unchecekd exception 에 대해 rollback 을 시행한다고 적혀있는 글을 많이 볼수있다.

 

이는 염언히 틀린말이다. 자바에서는 기존벅으로 트랜잭션에 대한 기본적인 처리 메커니즘을 제공하지 않으므로 Checked 이든 Unchecked 이든 프로그래머가 어떻게 프로그램을 짜느냐에 달려있다. 즉 Unchecked 에 롤백하는 메커니즘은 스프링 프레임워크를 사용할때 적용되는 기본 세팅이지 다른 프레임워크 혹은 프레임워크를 사용하지 않고 순수 자바 SDK 에서 제공하는 인터페이스로만 DB처리를 할경우 맞지 않는 말이된다. 

 

이는 자바=스프링을 동일시하는데 발생해서 불러오는 자바 개발자들 사이에서 불러오는 흔한 오해라고 생각한다.

하이버네이트는 내부적으로 두종류의 캐쉬를 지원하는데 하나는 First-level cache 이고 다른 한종류는 Second-level Cache 이다

 

First-level cache?

대부분의 ORM (Object Relational Mapping) 프레임워크가 지원하는것처럼 하이버네이트또한 일차적 캐쉬를 지원한다. 

일차캐쉬는 Hibernate 의 Session 단계에서 지원하는 캐쉬로 가장 비싼 연산작업중 하나인 데이터베이스와의 대화를 줄여주기 위해 존재한다. Session 안에서 동작하는 캐쉬이기 때문에 Session 이 종료되면 캐쉬도 같이 사라지게 된다.

 

Second-level cache?

First level cache 이 세션 단계에서의 캐쉬라면 second level cache 는 session factory 단계에서 지원하는 캐쉬로 session factory 에서 생성되는 session 간에 공유가 된다. 

 

 

표로 다시 정리하면

  First Level  Second Level
범위 Session  Session Factory (all sessions) 
기본 활성화 O X
설정 따른 설정 필요없음 설정필요
캐쉬 백엔드에 따라 다른 설정 추가 필요

 

동시성 캐쉬 전략

이름  
READ_ONLY 읽기에 대한 캐쉬를 생성한다. 설정값 같이 어플리케이션이 시작되고 변화하지 않는 값에 대해 사용
NONSTRICT_READ_WRITE READ_WRITE 와 비슷하지만 때때로 데이터를 읽는 경우에 최신의 데이터를 가지고 오지 않을수도 있늠점에 유의해햐한다. 
어플리케이션이 같은 데이터에 대해 접근하는 일이 많이 없고 강한 트랜잭션 격리가 필요없는 경우 사용
READ_WRITE 읽기와 쓰기에 대해 캐쉬를 생성한다. Seriazliable 트랜잭션 격리수준은 적용되지 않는다
TRANSACTIONAL
트랜잭션 대한 캐쉬를 지원한다. Seriazliable 트랜잭션 격리.
TA Transaction Provider 와 같이 사용하여 분산 트랙잭션을 사용하는 경우 사용하면 좋을 거 같다 

 

Entity 에 대해 캐쉬 사용할때 샘플코드

@Entity(name = "Company")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public static class Company {

}

 

참조: 

1. https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#caching

'Java > JPA' 카테고리의 다른 글

JPA Cascade Types  (0) 2021.12.19

JPA를 사용해서 엔티티간의 관계를 설정할때 아래와 어노테이션을 작성하는 일이 많은데 어떤 의미를 가지고 있는지 알아보자

@OneToMany(cascade={CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.LAZY)

 

먼저 각각의 CaseType에 들어가기전에 영속성 컨텍스트와 JPA의 상태에 대한 선행지식이 필요하다

 

Persistence Conxtet (영속성 컨텍스트)

공식문서의 정의를 참조해보자 

 

EntityManager (Java(TM) EE 7 Specification APIs)

Interface used to interact with the persistence context. An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. W

docs.oracle.com

A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed. The 
EntityManager
 API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.

어렵게 정의되어 있지만 간단하게 요약하자면 영속성 컨텍스트가 영속성 컨텍스트안에 있는 엔티티들의 변화를 추적하고 관리한다는 이야기다.

 

엔티티 객체의 상태

상태 설명
Transient 엔티티 객체가 데이터베이스에 아직 저장되기 전의 상태 
Persistent 엔티티 객체가 데이터베이스에 저장된 상태
Detached Persistent 상태였던  엔티티 객체가 더이상 영속성 컨텍스트에 속해있지 않는 상태

JPA의 동작들

동작 설명 특징
Save 하이버네이트 구현체에만 있는 기능 Persist 와 같은 기능.  Persist 는 생성된 ID 를 돌려주지 않으나 Save 는 돌려준다
Persist 엔티티를 영속성 컨텍스트에 포함시킨다 1. 엔티티가 Transient 상태라면 엔티티는 Persistent 상태가 된고 관련 동작들을 전파한다. (PERSIST, ALL로 설정된 경우)
2. 엔티티가 이미 Persistent 상태라면 엔티티에 직접적인 영향은 없다. 그러나 관련 동작들은 여전히 자식으로 전파된다
3. 엔티티가 Detached 상태라면 에러를 발생시킨다
Merge Persistent 상태의 엔티티 객체를 Deatched 상태의 객체의 값들로 업데이트한다  
Update 하이버네이트에만 존재하는 동작으로 Merge 와 같은 동작을 수행한다  
SaveOrUpdate 하이버네이트에만 존재하는 동작으로   

Cascade 종류

 

 

Hibernate ORM 5.6.3.Final User Guide

Fetching, essentially, is the process of grabbing data from the database and making it available to the application. Tuning how an application does fetching is one of the biggest factors in determining how an application will perform. Fetching too much dat

docs.jboss.org

JPA 표준에 사용하면 아래와 같이 6가지 Cascade Type 있다

종류 특징
ALL 아래에 기술된 모든 동장들을 자식 엔티티에게 전파한다
PERSIST JPA의 Persist 동작 (save, persist )을 부모 엔티티에서 자식 엔티티에게 전파된다
MERGE 부모 엔티티가 업데이트 될때 자식 엔티티들도 업데이트 된다
REMOVE 자식 엔티티들을 부모 엔티티 삭제시 동시에 삭제한다
REFRESH 데이터베이스로부터 데이터르 다시 읽어 들이는 refresh 동작을 부모에서 자식 엔티티로 전파
DETACH Detach 를 부모에서 자식 엔티티로 전파한다

하이버네이트에만 존재하는 CaseCade Type

종류 특징
REPLICATE Replicate 를 사용할떄 자식엔티티에게도 같은 동작을 전파한다.
**자동생성되는 ID 사용하지 않고 엔티티를 복제할 필요가 있을때 사용하면 좋다.
SAVE_UPDATE 하이버네이트 구현체의 save, update, saveOrUpdate 동작을 수행시에 자식 엔티티에게 같은 동작을전파한다
LOCK 이미 Detached 된 부모 엔티티 객체를 다시 영속성 객체에 추가시에 자식엔티티도 같이 추가된다

 

참조:

1. https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html

2. https://stackoverflow.com/questions/161224/what-are-the-differences-between-the-different-saving-methods-in-hibernate

'Java > JPA' 카테고리의 다른 글

JPA & Hibernate Cache  (0) 2022.01.19

어플리케이션을 구동하기 위해 사용되는 설정값들을 어플리케이션이 돌아가는 플랫폼안에서 보통 환경변수 혹은 파일안에 저장해두고 쓰는데 어플리케이션 설정값을 Zookeepr, AWS Secrets 과 같은 외부 서비스에서 불러들이거나 보안을 위해 설정값들을 메모리 안에서만 저장이 필요한 경우에 활용 가능한 방법이다.

Spring boot 에서 만 활용 가능한 방법

EnvironmentPostProcessor 를 활용

public class PriceCalculationEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, 
      SpringApplication application) {
        PropertySource<?> system = environment.getPropertySources()
          .get(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);

        Map<String, Object> prefixed = names.stream()
          .collect(Collectors.toMap(this::rename, system::getProperty));
        environment.getPropertySources()
          .addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new MapPropertySource("prefixer", prefixed));
    }

}

Spring boot 와 Spring boot를 사용하지 않는 환경에서 활용 가능한 방법

ApplicationContextInitializer 를 활용한 방법

먼저 아래와 같이 프로퍼티 값을 오버라이드 하는 코드를 작성해준다.

public class PropertyOverrideContextInitializer
  implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableWebApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        MyConfigProps configProps = Binder.get(environment).bind("my-config", MyConfigProps);
        System.out.println(configProps.getHomekey());
    }
  }

여기서 네가지 방법으로 어플리케이션에 적용 가능하다

1. web.xml 안에 있는 contextInitializerClasses 에 추가 혹은 상응하는 Java Config (Spring MVC)

    <context-param>
        <param-name>contextInitializerClasses</param-name>
        <param-value>com.xxxx.PropertyOverrideContextInitializer</param-value>
    </context-param>

 

자바로 설정시에 

 

@ContextConfiguration(initializers = PropertyOverrideContextInitializer.class)
public class AppConfig {

}

스프링만 사용시 

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
MyAppContextInitializer initializer = new MyAppContextInitializer();
initializer.initialize( ctx );

ctx.register( com.my.classpath.StackOverflowConfiguration.class );
ctx.refresh()

JobLauncher launcher = context.getBean(JobLauncher.class);

 

2. META-INF/spring.factories 추가하여 자동 설정 추가 (Spring Boot) 

org.springframework.context.ApplicationContextInitializer=
org.springframework.boot.context.PropertyOverrideContextInitializer

3. 어플리케이션 추가 실행코드에 추가코드에 추가하는 방법 (Spring boot)

application.addInitializers(PropertyOverrideContextInitializer.class);
application.run(args);

혹은

new SpringApplicationBuilder(YourApp.class)
    .initializers(PropertyOverrideContextInitializer.class);
    .run(args);

4. context.initializer.classes 프라퍼티 설정 (Spring boot)

context.initializer.classes=com.xxxx.PropertyOverrideContextInitializer

참조

  1. https://www.baeldung.com/spring-tests-override-properties
  2. https://stackoverflow.com/questions/35217354/how-to-add-custom-applicationcontextinitializer-to-a-spring-boot-application
  3. https://stackoverflow.com/questions/35048164/spring-applicationcontextinitializer-and-properties
  4. https://stackoverflow.com/questions/13288841/applicationcontextinitializer-in-a-non-web-spring-context

+ Recent posts