Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿš€ 4๋‹จ๊ณ„ - ํŠธ๋žœ์žญ์…˜ ๋™๊ธฐํ™” ๊ตฌํ˜„ํ•˜๊ธฐ #41

Open
wants to merge 18 commits into
base: sm9171
Choose a base branch
from

Conversation

sm9171
Copy link

@sm9171 sm9171 commented Aug 30, 2024

์•ˆ๋…•ํ•˜์„ธ์š” ์ •์™„๋‹˜

๋ฏธ์…˜์„ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ UserServiceTest์—์„œ testChangePassword ํ…Œ์ŠคํŠธ์—์„œ ์‹คํŒจ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋””๋ฒ„๊น…์„ ํ–ˆ์„ ๋•Œ AppUserService์—์„œ userHistoryDao.log(new UserHistory(user, createBy)); ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ jdbcTemplateํด๋ž˜์Šค์˜ update๋ฉ”์†Œ๋“œ์—์„œ `PreparedStatement๋ฅผ ํ• ๋‹นํ•  ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๊ตฌ์š”.

ํ˜ผ์ž ์›์ธ ํŒŒ์•…์ด ์–ด๋ ค์›Œ์„œ ํ•ด๋‹น ๋‚ด์šฉ ๊ณต์œ ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  3๋ฒˆ์งธ ๋ฏธ์…˜๋•Œ ์ฃผ์‹  ์งˆ๋ฌธ์ด ์ €ํ•œํ…Œ๋Š” ์–ด๋ ค์›Œ์„œ ํ˜น์‹œ ์ •์™„๋‹˜์˜ ์ƒ๊ฐ์€ ์–ด๋– ์‹ ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
#39 (comment)

sm9171 added 18 commits August 19, 2024 15:58
# Conflicts:
#	README.md
#	app/src/main/java/camp/nextstep/dao/UserDao.java
#	app/src/main/java/camp/nextstep/dao/UserRowMapper.java
- update ๋ฉ”์†Œ๋“œ์—์„œ connection์„ DataSourceUtils์—์„œ ๊ฐ€์ ธ์˜ค๋Š”๊ฑธ๋กœ ๋ณ€๊ฒฝ
# Conflicts:
#	README.md
#	app/src/main/java/camp/nextstep/dao/UserHistoryDao.java
#	app/src/main/java/camp/nextstep/service/UserService.java
#	app/src/test/java/camp/nextstep/service/UserServiceTest.java
@ghojeong
Copy link
Member

๊ทธ๋ฆฌ๊ณ  3๋ฒˆ์งธ ๋ฏธ์…˜๋•Œ ์ฃผ์‹  ์งˆ๋ฌธ์ด ์ €ํ•œํ…Œ๋Š” ์–ด๋ ค์›Œ์„œ ํ˜น์‹œ ์ •์™„๋‹˜์˜ ์ƒ๊ฐ์€ ์–ด๋– ์‹ ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
#39 (comment)

๋‚˜์ƒ๋ฏผ ๋‹˜!
์ €์—๊ฒŒ๋„ ํ•™์Šตํ•ด์•ผํ•˜๋Š” ๋‚ด์šฉ์ธ์ง€๋ผ, ์ œ๊ฐ€ ๊ณต๋ถ€ํ–ˆ๊ณ  ๊ฒฝํ—˜ํ–ˆ๋˜ ๋‚ด์šฉ๋“ค์„ ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
์ €๋„ ์–ด๋ ค์šด ์ฃผ์ œ์ธ๋ฐ ๋‹ต๋ณ€ ๋‹ฌ์•„์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

Copy link
Member

@ghojeong ghojeong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋‚˜์ƒ๋ฏผ๋‹˜!
๋””๋ฒ„๊น… ๋„์›€ ์š”์ฒญ์ฃผ์‹  ๊ฒƒ์— ๋Œ€ํ•ด ์˜๊ฒฌ ๋‚จ๊ฒจ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
์ œ ์ถ”์ธก์€ DataSourceUtils.releaseConnection ๊ณผ๋Š” ๋ณ„๊ฐœ๋กœ
try-with-resources ๋ฌธ๋ฒ•์—์„œ ํ•œ๋ฒˆ ๋” connection ์„ close ํ•˜๋ ค ํ•ด์„œ ๋ฐœ์ƒํ•œ ๋ฒ„๊ทธ๊ฐ€ ์•„๋‹Œ๊ฐ€ ์‹ถ์Šต๋‹ˆ๋‹ค.

ํ˜„์žฌ Transaction ๊ด€๋ จ ๊ฐ์ฒด๋“ค์— ๊ด€ํ•œ ํ…Œ์ŠคํŠธ๊ฐ€ ์ „๋ฌดํ•œ๋ฐ,
ํ…Œ์ŠคํŠธ ์ž‘์„ฑ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

Comment on lines +27 to 28
try (final Connection connection = DataSourceUtils.getConnection(dataSource);
final PreparedStatement pstmt = connection.prepareStatement(sql)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
try (final Connection connection = DataSourceUtils.getConnection(dataSource);
final PreparedStatement pstmt = connection.prepareStatement(sql)) {
try {
final Connection connection = DataSourceUtils.getConnection(dataSource);
final PreparedStatement pstmt = connection.prepareStatement(sql);

๋ฏธ์…˜์„ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ UserServiceTest์—์„œ testChangePassword ํ…Œ์ŠคํŠธ์—์„œ ์‹คํŒจ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๋””๋ฒ„๊น…์„ ํ–ˆ์„ ๋•Œ AppUserService์—์„œ userHistoryDao.log(new UserHistory(user, createBy)); ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ jdbcTemplateํด๋ž˜์Šค์˜ update๋ฉ”์†Œ๋“œ์—์„œ `PreparedStatement๋ฅผ ํ• ๋‹นํ•  ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๊ตฌ์š”.

์ œ๊ฐ€ ๋กœ๊ทธ์ƒ ํ™•์ธํ•œ ์—๋Ÿฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
org.h2.jdbc.JdbcSQLNonTransientException: The object is already closed

์ด๋ฏธ closed ๋œ ์ž์›์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋กœ๊ทธ๊ฐ€ ๋งํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋ฏธ์…˜์—์„œ closed ๋œ ์ž์›์ด๋ž€ ๊ณง connection ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.

Try-With-Resources ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋” ๊นŠ๊ฒŒ ๊ณต๋ถ€ํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ง€๊ธˆ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ๋Š” update ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค, connection ์ด ๋ฐ”๋กœ closed ๋˜๊ฒŒ ๋˜์–ด,
๊ฐ™์€ ํŠธ๋žœ์žญ์…˜ ์•ˆ์— ์žˆ๋Š” ๋‹ค๋ฅธ update ์—์„œ connection ์„ ์žฌํ™œ์šฉํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

๊ฐ™์€ ํŠธ๋žœ์žญ์…˜์ด๋ผ๋ฉด connection ์ด closed ๋˜์ง€ ์•Š๊ณ , ๊ฐ™์€ connection ์„ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ ,
ํŠธ๋žœ์žญ์…˜์ด ์™„๋ฃŒ๋˜์—ˆ์„ ๋•Œ connection ์ด closed ๋˜๋„๋ก ํ•ด์ฃผ์„ธ์š”.

์ฐธ๊ณ ๋กœ ๋งจ ์œ„์˜ suggestion code ๋Š” ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค์ด์ง€,
์ด๋ ‡๊ฒŒ ๊ณ ์ณ์•ผ ํ•œ๋‹ค๋Š” ๋œป์ด ์ ˆ๋Œ€ ์•„๋‹™๋‹ˆ๋‹ค.
์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ๊ทธ๋ž˜์„œ connection ์„ ์–ด๋””์„œ ์–ด๋–ป๊ฒŒ close ํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ด ๋น ์ ธ์žˆ๋Š”๋ฐ,
์ด ๋ถ€๋ถ„์„ ๊ตฌํ˜„ํ•ด์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

Comment on lines +10 to +16
public class TxUserService implements UserService {
private final UserService userService;
private final TransactionTemplate transactionTemplate;

@Autowired
public TxUserService(final UserService userService, DataSource dataSource) {
this.userService = userService;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DI ๋ฅผ ํ•˜๋Š” ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์„ ๊ณ ๋ คํ•˜๋ฉด ์ข‹์ง€ ์•Š์„๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค.
๊ฐ€๋ น ํ˜„์žฌ UserService ๋ฅผ implements ํ•˜๋Š” ํด๋ž˜์Šค๋Š” TxUserService ์™€ AppUserService 2๊ฐœ๋ผ์„œ,

๋‚˜์ค‘์— ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ DI ๋ฅผ ํ•˜๋ คํ•  ๋•Œ ์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ์˜์กด์„ฑ ์ฃผ์ž…์„ ํ•ด์•ผํ• ์ง€ ํ—ท๊ฐˆ๋ฆด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๋‚ด๋ถ€ ๋งด๋ฒ„์˜ ํƒ€์ž…์ด UserService ์ด๋”๋ผ๋„,
DI ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ํ™•์ธํ•˜๋Š” ์ƒ์„ฑ์ž ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž…์€ AppUserService ๋กœ ๋ช…์‹œํ•˜๋Š”๊ฒŒ ๋” ์•ˆ์ „ํ•˜์ง€ ์•Š์„๊นŒ ์ œ์•ˆํ•ด๋ด…๋‹ˆ๋‹ค.

Comment on lines +37 to +43
@Override
public void changePassword(final long id, final String newPassword, final String createdBy) {
transactionTemplate.execute(ignored -> {
userService.changePassword(id, newPassword, createdBy);
return null;
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฏธ์…˜ ์˜๋„์™€ ์ผ์น˜ํ•˜๊ฒŒ transactionTemplate ์ž˜ ํ™œ์šฉํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ‘

Comment on lines +10 to +16
public <T> T execute(TransactionCallback<T> action) {
TransactionStatus status = transactionManager.getTransaction();

try {
T result = action.doInTransaction(status);
transactionManager.commit(status);
return result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TransactionCallback ์„ ๋ณ„๋„ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ฌด์ฒ™ ์ž˜ ์„ ์–ธํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ‘
์˜์™ธ๋กœ Callback ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐ์ง€ ์•Š๊ฒŒ ๋„˜๊ธฐ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ๋”๋ผ๊ณ ์š”.

Comment on lines +27 to +50
public void commit(TransactionStatus status) {
Connection connection = status.connection();

status.checkConnectionActive();
try {
connection.commit();
} catch (SQLException e) {
throw new DataAccessException(e);
}
DataSourceUtils.releaseConnection(connection, dataSource);
}

public void rollback(TransactionStatus status) {
Connection connection = status.connection();

status.checkConnectionActive();
try {
connection.rollback();
} catch (SQLException e) {
throw new DataAccessException(e);
} finally {
DataSourceUtils.releaseConnection(connection, dataSource);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commit ํ˜น์€ rollback ์ด ์™„๋ฃŒ๋œ ์ดํ›„์—๋Š” DataSourceUtils.releaseConnection ๋ฅผ ํ†ตํ•ด connection ์„ ํ•ด๋ฐฉํ•˜๋Š” ๊ตฐ์š”

Comment on lines 32 to 37
connection.close();
} catch (SQLException ex) {
throw new CannotGetJdbcConnectionException("Failed to close JDBC Connection");
} finally {
TransactionSynchronizationManager.unbindResource(dataSource);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DataSourceUtils ์—์„œ connection ์˜ close ๋ฅผ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

Comment on lines 14 to 32
public static Connection getResource(DataSource key) {
return null;
final Map<DataSource, Connection> dataSourceConnectionMap = resources.get();

return dataSourceConnectionMap.get(key);
}

public static void bindResource(DataSource key, Connection value) {
final Map<DataSource, Connection> dataSourceConnectionMap = resources.get();

if (dataSourceConnectionMap.containsKey(key)) {
throw new IllegalStateException("Resource is already bound to key " + key);
}

dataSourceConnectionMap.put(key, value);
}

public static Connection unbindResource(DataSource key) {
return null;
return resources.get().remove(key);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•ˆํƒ€๊นŒ์šด ์ ์€ ์ด๋ฒˆ ๋ฏธ์…˜์˜ ํ•ต์‹ฌ์ธ transaction/support ํŒจํ‚ค์ง€ ๋‚ด๋ถ€์˜ ๊ฐ์ฒด์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ๊ฐ€ ์ „ํ˜€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ตฐ์š”.
๊ฐ™์€ ํŠธ๋žœ์žญ์…˜์ด๋ผ๋ฉด ๊ฐ™์€ connection ์ธ์ง€,
ํŠธ๋žœ์žญ์…˜์ด ๋‹ฌ๋ผ์ง„๋‹ค๋ฉด connection ์ด ๋‹ฌ๋ผ์ง€๋Š”์ง€,
ํ•œ๋ฒˆ unbind ๋œ resource ๋Š” ๋‹ค์‹œ ์กฐํšŒ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋Š”์ง€ ๋“ฑ๋“ฑ
์œ ์˜๋ฏธํ•œ ํ…Œ์ŠคํŠธ๋“ค์„ ์ž‘์„ฑํ•ด์ฃผ์‹œ๊ธฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants