-
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.
- Loading branch information
Showing
6 changed files
with
94 additions
and
77 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
7 changes: 7 additions & 0 deletions
7
jdbc/src/main/java/org/springframework/jdbc/core/transaction/Executor.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,7 @@ | ||
package org.springframework.jdbc.core.transaction; | ||
|
||
@FunctionalInterface | ||
public interface Executor { | ||
|
||
Object execute(); | ||
} |
9 changes: 9 additions & 0 deletions
9
jdbc/src/main/java/org/springframework/jdbc/core/transaction/ExecutorWithoutReturnValue.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,9 @@ | ||
package org.springframework.jdbc.core.transaction; | ||
|
||
import java.sql.SQLException; | ||
|
||
@FunctionalInterface | ||
public interface ExecutorWithoutReturnValue { | ||
|
||
void execute() throws SQLException; | ||
} |
67 changes: 67 additions & 0 deletions
67
jdbc/src/main/java/org/springframework/jdbc/core/transaction/TxExecutor.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,67 @@ | ||
package org.springframework.jdbc.core.transaction; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.datasource.DataSourceUtils; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public class TxExecutor { | ||
|
||
public static void executeWithoutReturnValue( | ||
final DataSource dataSource, | ||
final ExecutorWithoutReturnValue executor | ||
) { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
conn.setAutoCommit(false); | ||
executor.execute(); | ||
|
||
commit(conn); | ||
} catch (SQLException e) { | ||
rollback(conn); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
} | ||
} | ||
|
||
public static Object execute( | ||
final DataSource dataSource, | ||
final Executor executor | ||
) { | ||
final Connection conn = DataSourceUtils.getConnection(dataSource); | ||
try { | ||
conn.setAutoCommit(false); | ||
final Object result = executor.execute(); | ||
|
||
conn.commit(); | ||
return result; | ||
} catch (SQLException e) { | ||
rollback(conn); | ||
throw new DataAccessException(e); | ||
} finally { | ||
DataSourceUtils.releaseConnection(conn, dataSource); | ||
TransactionSynchronizationManager.unbindResource(dataSource); | ||
} | ||
} | ||
|
||
private static void commit(final Connection conn) { | ||
try { | ||
conn.commit(); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
|
||
private static void rollback(final Connection conn) { | ||
try { | ||
conn.rollback(); | ||
} catch (SQLException e) { | ||
throw new DataAccessException(e); | ||
} | ||
} | ||
} |