-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JDBC 라이브러리 구현하기 - 3단계] 연어(황재현) 미션 제출합니다. (#461)
* feat: 유저 비밀번호 변경 기능 구현 * test: AOP 미션 0단계 * test: AOP 미션 1단계 * test: AOP 미션 2단계 * style: 메서드 위치 변경 * fix: 롤백되지 않는 현상 해결
- Loading branch information
Showing
23 changed files
with
325 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
jdbc/src/main/java/org/springframework/jdbc/datasource/ConnectionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.springframework.jdbc.datasource; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public class ConnectionManager { | ||
|
||
private final DataSource dataSource; | ||
|
||
public ConnectionManager(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public Connection getConnection() { | ||
try { | ||
return dataSource.getConnection(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
|
||
public void close(final Connection connection) { | ||
try { | ||
connection.close(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
|
||
public void rollback(final Connection connection) { | ||
try { | ||
connection.rollback(); | ||
} catch (final SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package aop.common; | ||
|
||
import aop.DataAccessException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.TransactionStatus; | ||
import org.springframework.transaction.support.DefaultTransactionDefinition; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
|
||
public class TransactionHandler implements InvocationHandler { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(TransactionHandler.class); | ||
|
||
private final PlatformTransactionManager transactionManager; | ||
private final Object target; | ||
|
||
public TransactionHandler(final PlatformTransactionManager transactionManager, final Object target) { | ||
this.transactionManager = transactionManager; | ||
this.target = target; | ||
} | ||
|
||
@Override | ||
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { | ||
final Method targetMethod = target.getClass().getDeclaredMethod(method.getName(), method.getParameterTypes()); | ||
|
||
if (targetMethod.isAnnotationPresent(Transactional.class)) { | ||
return invokeWithTransaction(target, method, args); | ||
} | ||
|
||
return method.invoke(target, args); | ||
} | ||
|
||
private Object invokeWithTransaction(final Object target, final Method method, final Object[] args) { | ||
final TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionDefinition()); | ||
|
||
try { | ||
log.info("Start Transaction for: {}", method.getName()); | ||
final Object result = method.invoke(target, args); | ||
transactionManager.commit(transaction); | ||
log.info("End Transaction for: {}", method.getName()); | ||
|
||
return result; | ||
} catch (final Exception e) { | ||
transactionManager.rollback(transaction); | ||
log.info("Errors occurred when committing: {}", method.getName()); | ||
|
||
throw new DataAccessException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package aop.common; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
@Documented | ||
public @interface Transactional { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.