Spring&SpringBoot
-
스프링 트랜잭션 전파Spring&SpringBoot 2025. 11. 20. 12:54
1. commit()@Slf4j@SpringBootTestpublic class BasicTxTest { @Autowired PlatformTransactionManager txManager; @TestConfiguration static class config { @Bean public PlatformTransactionManager txManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } } @Test void commit() { log.info("트랜잭션 시작"); Transac..
-
데이터 접근 기술 - 스프링 데이터 JPASpring&SpringBoot 2025. 11. 20. 09:59
스프링 데이터 JPA 주요 기능 공통 인터페이스 기능쿼리 메서드 기능 1. 공통 인터페이스 기능 스프링 데이터 JPA가 구현 클래스를 대신 생성JpaRepository 인터페이스만 상속받으면 스프링 데이터 JPA가 프록시 기술을 사용해서 구현 클래스를 만들어준다. 그리고 만든 구현 클래스의 인스턴스를 만들어서 스프링 빈으로 등록한다. JpaRepository 사용법public interface ItemRepository extends JpaRepository {}JpaRepository 인터페이스를 인터페이스 상속 받고 제네릭에 관리할 를 주면 된다JpaRepository가 제공하는 기본 CRUD 기능을 모두 사용할 수 있다 2. 쿼리 메서드 기능// 순수 JPA 리포지토리public List..
-
트랜잭션 커밋/롤백과 예외Spring&SpringBoot 2025. 11. 20. 09:33
예외 발생예외 발생 시 스프링 트랜잭션 AOP는 예외 종류에 따라 트랜잭션을 커밋하거나 롤백한다언체크 예외(RuntimeException, Error)와 그 하위 예외가 발생하면 트랜잭션을 롤백한다체크 예외(Exception)와 그 하위 예외가 발생하면 트랜잭션을 커밋한다 application.properties 수정# application.propertieslogging.level.org.springframework.transaction.interceptor=TRACElogging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG# JPA loglogging.level.org.springframework.orm.j..
-
@TransactionSpring&SpringBoot 2025. 11. 13. 08:39
트랜잭션 추상화 // JDBC 트랜잭션 코드 예시public void accountTransfer(String fromId, String toId, int money) throws SQLException { Connection con = dataSource.getConnection(); try { con.setAutoCommit(false); //트랜잭션 시작 //비즈니스 로직 bizLogic(con, fromId, toId, money); con.commit(); //성공시 커밋 } catch (Exception e) { con.rollback(); //실패시 롤백 throw new IllegalS..
-
트랜잭션, DB 락Spring&SpringBoot 2025. 11. 12. 15:04
트랜잭션 트랜잭션 ACID원자성(Atomicity) : 트랜잭션 내에서 실행한 작업들은 마치 하나의 작업인 것처럼 모두 성공하거나 모두 실패해야 한다일관성(Consistency) : 모든 트랜잭션은 일관성 있는 데이터베이스 상태를 유지해야 한다예) 데이터베이스에서 정한 무결성 제약 조건을 항상 만족해야 한다격리성(Isolation) : 동시에 실행되는 트랜잭션들이 서로에게 영향을 미치지 않도록 격리한다예) 동시에 같은 데이터를 수정하지 못하도록 해야 한다트랜잭션 격리 수준 - Isolation levelREAD UNCOMMITED(커밋되지 않은 읽기) READ COMMITTED(커밋된 읽기) - 실무 REPEATABLE READ(반복 가능한 읽기)SERIALIZABLE(직렬화 가능)지속성(Durabil..
-
JDBCSpring&SpringBoot 2025. 11. 12. 13:04
애플리케이션 서버와 DB - 일반적인 사용법커넥션 연결 : 주로 TCP/IP 사용해 커넥션을 연결SQL 전달 : 애플리케이션 서버는 DB가 이해할 수 있는 SQL을 연결된 커넥션을 통해 DB에 전달결과 응답 : DB는 전달된 SQL을 수행하고 그 결과를 응답문제데이터베이스를 다른 종류의 데이터베이스로 변경하면 애플리케이션 서버에 개발된 데이터베이스 사용 코드도 함께 변경 필요개발자가 각각의 DB마다 커넥션 연결, SQL 전달, 결과 응답 방법을 새로 학습 필요 JDBC 표준 인터페이스JDBC(Java DataBase Connection) : JDBC는 자바에서 데이터베이스에 접속할 수 있도록 하는 자바 API JDBC와 최신 데이터 접근 기술 JDBC DriverManager 연결 이해애플..
-
스프링 AOP 실무 주의사항Spring&SpringBoot 2025. 10. 21. 16:25
프록시 내부 호출 문제스프링은 프록시 방식의 AOP를 사용한다. 따라서 AOP를 적용하려면 항상 프록시를 통해서 대상 객체(Target)을 호출해야 한다이렇게 해야 프록시에서 먼저 어드바이스를 호출하고, 이후에 대상 객체를 호출한다만약 프록시를 거치지 않고 대상 객체를 직접 호출하게 되면 AOP가 적용되지 않고, 어드바이스도 호출되지 않는다@Slf4j@Componentpublic class CallServiceV0 { public void external() { log.info("call external"); internal(); // 내부 메서드 호출 this.internal() } public void internal() { log.info("ca..
-
로그출력AOP & 재시도AOPSpring&SpringBoot 2025. 10. 21. 11:14
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface Trace { // 로그출력 AOP}@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface Retry { // 재시도 AOP int value() default 3;} @Slf4j@Aspectpublic class RetryAspect { @Around("@annotation(retry)") public Object doRetry(ProceedingJoinPoint joinPoint, Retry retry) throws Throwable { ..