TIL

2021.11.02 기록장

Gisungcu 2021. 11. 2. 20:09

ToDo

  • 세션 db 적용

Done

  • 세션 db 적용

Weekly goal

  • 책 읽기
  • 영어 레퍼런스 읽기
  • 회고

spring boot에는 내장 tomcat이 들어있다.
우리는 이를 통해 session을 사용할 수 있었다.
tomcat에서 Map형태로 HttpSession을 가지고 있기 때문이다.

https://github.com/apache/tomcat/blob/main/java/org/apache/catalina/session/ManagerBase.java

GitHub - apache/tomcat: Apache Tomcat

Apache Tomcat. Contribute to apache/tomcat development by creating an account on GitHub.

github.com

https://www.waitingforcode.com/tomcat/session-storage-in-tomcat/read

Session storage in Tomcat

Knowing what is under the hood of your car, simplifies always its reparation. In this article we'll focus on session management in Tomcat to better debug our applications in the future.

www.waitingforcode.com

근데 분산 서버가 되면 세션은 여러 방법으로 관리될 수 있다.
스태키 세션, 세션 클러스터링 등

위의 방법들이 있지만 자주 쓰이는 방법은 외부 메모리 DB에서 세션을 관리하는 것이다.

spring에서 이를 위해 라이브러리를 제공해주는데 아주 간단하다.
https://www.baeldung.com/spring-session-jdbc
peroperties에 간단하 store-jdbc 설정을 추가해주면 된다.

근데 우리는 대부분 애플리케이션용 DB가 있다. 그래서 2개의 dataSource가 생겨야 한다.
애플리케이션용, 세션용

spring-jdbc는 default로 기존의 dataSource를 사용하기에 직접 dataSource를 엮어줘야 한다.

schema는 라이브러리에서 제공해준다


엮어주는 작업은
https://springhow.com/spring-session-different-database/

Spring sessions in a Separate Database

Storing session details in Redis or database is usually a good idea. However, the default implementation of spring-session-jdbc uses the primary data

springhow.com


기본적으로 이 라이브러리는 springsessionrepositoryfilter를 통해 HttpSession을 만들어준다.

attribute table에서 객체를 직렬화해서 저장하기에
직렬화를 해야 한다. 간단하게 imple 하자.

직렬화를 하지 않을 시 duplicate exception가 난다. notserializableexception가 나면 더 쉽게 오류를 찾았을 텐데
직렬화를 하지 않았을 때 여러 번의 insert가 이뤄지는 이유는 모르겠다.






https://docs.spring.io/spring-session/docs/2.5.3/reference/html5/

Spring Session

With the new major release version, the Spring Session team took the opportunity to make some non-passive changes. The focus of these changes is to improve and harmonize Spring Session’s APIs as well as remove the deprecated components. 11.1. Baseline Up

docs.spring.io

문제점
https://nidev.gitlab.io/2021/07/24/spring-session-jdbc%EC%99%80-%EB%8B%A4%EC%A4%91-%EC%A0%91%EC%86%8D-%ED%99%98%EA%B2%BD%EC%97%90%EC%84%9C%EC%9D%98-%EB%AC%B8%EC%A0%9C/

spring-session-jdbc와 다중 접속 환경에서의 문제

HTTP SessionHTTP로 일반적인 API를 제공하는 업무가 아니라 사용자와의 상호작용하는 웹 서비스를 제공하다보면 현재 사용자와 상호작용된 결과 중 일부를 서버사이드에 저장해둘 필요가 생긴다.

git.lily.rs