Skip to content

Commit

Permalink
refactor: exception 처리 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kevstevie committed Oct 13, 2023
1 parent 75c300d commit b4d62c6
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.sql.SQLException;

public class CannotGetJdbcConnectionException extends RuntimeException {
public class CannotGetJdbcConnectionException extends TransactionException {

public CannotGetJdbcConnectionException(String msg) {
super(msg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.jdbc.exception;

public class DataAccessException extends RuntimeException {
public class DataAccessException extends TransactionException {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.springframework.jdbc.exception;

public class PreparedStatementExecuteException extends RuntimeException {
public class PreparedStatementExecuteException extends TransactionException {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.springframework.jdbc.exception;

public class ResultSetMappingException extends RuntimeException {
public class ResultSetMappingException extends TransactionException {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.springframework.jdbc.exception;

public class TransactionException extends RuntimeException {

public TransactionException() {
}

public TransactionException(String message) {
super(message);
}

public TransactionException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.springframework.transaction.support;

import org.springframework.jdbc.exception.TransactionException;

import java.sql.Connection;

public class ConnectionHolder {
Expand All @@ -15,23 +17,23 @@ public void commit() {
try {
connection.commit();
} catch (Exception e) {
throw new RuntimeException(e);
throw new TransactionException(e.getMessage(), e);
}
}

public void rollback() {
try {
connection.rollback();
} catch (Exception e) {
throw new RuntimeException(e);
throw new TransactionException(e.getMessage(), e);
}
}

public void close() {
try {
connection.close();
} catch (Exception e) {
throw new RuntimeException(e);
throw new TransactionException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.springframework.transaction.support;

import org.springframework.jdbc.exception.TransactionException;

import javax.sql.DataSource;
import java.sql.Connection;

Expand All @@ -19,7 +21,7 @@ public void createConnection() {
connectionHolder.setIsTransactionActive(true);
TransactionSynchronizationManager.bindResource(dataSource, connectionHolder);
} catch (Exception e) {
throw new RuntimeException(e);
throw new TransactionException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.springframework.transaction.support;

import org.springframework.jdbc.exception.TransactionException;

import java.util.function.Supplier;

public class TransactionTemplate {
Expand All @@ -16,7 +18,7 @@ private <T> T executeWithTransaction(Supplier<T> supplier) {
T result = supplier.get();
transactionManager.commit();
return result;
} catch (RuntimeException ex) {
} catch (TransactionException ex) {
transactionManager.rollback();
throw ex;
} finally {
Expand Down

0 comments on commit b4d62c6

Please sign in to comment.