환경변수Tip

bonjour·2019년 10월 2일
0

개발PC에서 개발하고 도커에서 운영시 주의점

로컬에서 오라클의 프로시져를 호출 시
파라미터(가령 날짜)가 잘 전달 되었지만
도커에서는 에러가 난다.

프로시져도 마찬가지로 로컬에서 호출 시
문제없다.

실제 서버에서 호출 시 ORA-01861이 발생했고
이는 서로 오라클 세션간의 환경이 다름을 의미한다.

그래서 비교한것이

SELECT * FROM nls_session_parameters WHERE PARAMETER LIKE '%DATE%' OR PARAMETER LIKE '%LANG%'; 

그래서 DB Connection을 얻을 때 아래와 같은 처리를 했다

  @Bean
    public DataSource dataSource() {
        HikariDataSource dataSource = new HikariDataSource() {
            @Override
            public Connection getConnection() throws SQLException {
                Connection connection = super.getConnection();
                setupConnection(connection);
                return connection;
            }

        };
        dataSource.setJdbcUrl(this.jdbcUrl);
        dataSource.setDriverClassName(this.driverClassName);
        dataSource.setUsername(this.username);
        dataSource.setPassword(this.password);

        return dataSource;
    }


  public void setupConnection(Connection connection) {

        try (Statement stmt = connection.createStatement()) {
            stmt.addBatch("ALTER SESSION SET TIME_ZONE='+9:00'");
            stmt.addBatch("ALTER SESSION SET NLS_LANGUAGE='KOREAN'");
            stmt.addBatch("ALTER SESSION SET NLS_DATE_FORMAT='RR/MM/DD'");
            stmt.addBatch("ALTER SESSION SET NLS_DATE_LANGUAGE='KOREAN'");
            stmt.executeBatch();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

더 나은 해결책을 찾아보다가 도커로 배포되는 리눅스에 환경변수가
생각나서 아래와 같이 설정으로 소스수정은 원상태로 돌렸다

현재 리눅스(alpine)이므로

FROM openjdk:8-jre-alpine
RUN apk --no-cache add tzdata && \
     cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime && \
     echo "Asia/Seoul" > /etc/timezone

우분투는 /etc/localtime 이 /usr/share/zoneinfo/Asia/Seoul 로 링크를 걸면 된다.
EC2일 경우에

ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime

만약 EB(Elastic Beanstalk)일 경우
.ebextensions 디렉토리에
00_timezone.config를 작성한다
https://docs.aws.amazon.com/ko_kr/elasticbeanstalk/latest/dg/ebextensions.html

commands:
set_time_zone:
 command: ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
profile
봉쥬

0개의 댓글