-
Notifications
You must be signed in to change notification settings - Fork 300
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
[JDBC 라이브러리 구현하기 - 4단계] 이리내(성채연) 미션 제출합니다. #588
Changes from 1 commit
974d28d
fbc0f6c
d08c11f
67417a1
24ff04a
309ca75
882c2f6
bd626c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.springframework.jdbc; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
import java.sql.Connection; | ||
|
||
public class ConnectionHolder { | ||
|
||
private Connection connection; | ||
private boolean transactionActive = false; | ||
|
||
public ConnectionHolder(final Connection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
public void setTransactionActive(final boolean transactionActive) { | ||
this.transactionActive = transactionActive; | ||
} | ||
|
||
public boolean isTransactionActive() { | ||
return transactionActive; | ||
} | ||
|
||
public Connection getConnection() { | ||
return connection; | ||
} | ||
|
||
public boolean has(final Connection connection) { | ||
return this.connection == connection; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package org.springframework.jdbc.datasource; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.CannotGetJdbcConnectionException; | ||
import org.springframework.jdbc.ConnectionHolder; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
import javax.sql.DataSource; | ||
|
@@ -13,22 +17,57 @@ public abstract class DataSourceUtils { | |
private DataSourceUtils() {} | ||
|
||
public static Connection getConnection(final DataSource dataSource) throws CannotGetJdbcConnectionException { | ||
Connection connection = TransactionSynchronizationManager.getResource(dataSource); | ||
if (connection != null) { | ||
return connection; | ||
final ConnectionHolder connectionHolder = TransactionSynchronizationManager.getResource(dataSource); | ||
if (connectionHolder != null) { | ||
return connectionHolder.getConnection(); | ||
} | ||
|
||
try { | ||
connection = dataSource.getConnection(); | ||
final Connection connection = dataSource.getConnection(); | ||
TransactionSynchronizationManager.bindResource(dataSource, connection); | ||
return connection; | ||
} catch (SQLException ex) { | ||
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex); | ||
} | ||
} | ||
|
||
public static void startTransaction(final Connection connection, final DataSource dataSource) { | ||
final ConnectionHolder connectionHolder = getConnectionHolder(connection, dataSource); | ||
try{ | ||
connectionHolder.setTransactionActive(true); | ||
connection.setAutoCommit(false); | ||
}catch(SQLException e) { | ||
throw new DataAccessException(); | ||
} | ||
} | ||
|
||
private static ConnectionHolder getConnectionHolder(final Connection connection, final DataSource dataSource) { | ||
final ConnectionHolder connectionHolder = TransactionSynchronizationManager.getResource(dataSource); | ||
if(isNull(connectionHolder)) { | ||
throw new IllegalStateException(); | ||
} | ||
if(!connectionHolder.has(connection)) { | ||
throw new IllegalStateException(); | ||
} | ||
return connectionHolder; | ||
} | ||
|
||
public static void finishTransaction(final Connection connection, final DataSource dataSource) { | ||
final ConnectionHolder connectionHolder = getConnectionHolder(connection, dataSource); | ||
try{ | ||
connection.commit(); | ||
connectionHolder.setTransactionActive(false); | ||
}catch(SQLException e) { | ||
throw new DataAccessException(); | ||
} | ||
} | ||
|
||
public static void releaseConnection(final Connection connection, final DataSource dataSource) { | ||
try { | ||
final ConnectionHolder connectionHolder = getConnectionHolder(connection, dataSource); | ||
if(connectionHolder.isTransactionActive()) { | ||
return; | ||
} | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. connection.close(); 에서 예외가 발생하더라도 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그러게요 생각해보지 못했는데 고민할만한 내용인 것 같습니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저또한 예외가 발생하더라도 비워주는것이 현재코드로서는 안전하다는 생각이 드네요 |
||
connection.close(); | ||
} catch (SQLException ex) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코멘트에 남겨주신것처럼 커넥션 홀더를 적용해주셨네요! 감동1