File tree Expand file tree Collapse file tree
src/main/java/com/softwareverde/database/transaction Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .softwareverde .database .transaction ;
2+
3+ import com .softwareverde .database .DatabaseConnection ;
4+ import com .softwareverde .database .DatabaseException ;
5+
6+ public interface DatabaseCallable <ReturnType , ConnectionType > {
7+ ReturnType call (final DatabaseConnection <ConnectionType > databaseConnection ) throws DatabaseException ;
8+ }
Original file line number Diff line number Diff line change 22
33import com .softwareverde .database .DatabaseException ;
44
5- public interface DatabaseTransaction <T > {
6- void execute (final DatabaseRunnable <T > databaseRunnable ) throws DatabaseException ;
5+ public interface DatabaseTransaction <ConnectionType > {
6+ void execute (final DatabaseRunnable <ConnectionType > databaseRunnable ) throws DatabaseException ;
7+ <ReturnType > ReturnType call (final DatabaseCallable <ReturnType , ConnectionType > databaseCallable ) throws DatabaseException ;
78}
Original file line number Diff line number Diff line change 55import com .softwareverde .database .DatabaseException ;
66
77import java .sql .Connection ;
8+ import java .sql .SQLException ;
89
910public class JdbcDatabaseTransaction implements DatabaseTransaction <Connection > {
1011 protected final DatabaseConnectionFactory <Connection > _databaseConnectionFactory ;
@@ -27,7 +28,27 @@ public void execute(final DatabaseRunnable<Connection> databaseConnectedRunnable
2728 connection .rollback ();
2829 throw exception ;
2930 }
31+ }
32+ catch (final Exception exception ) {
33+ throw new DatabaseException ("Unable to complete query." , exception );
34+ }
35+ }
36+
37+ @ Override
38+ public <T > T call (final DatabaseCallable <T , Connection > databaseConnectedCallable ) throws DatabaseException {
39+ try (final DatabaseConnection <Connection > databaseConnection = _databaseConnectionFactory .newConnection ()) {
40+ final Connection connection = databaseConnection .getRawConnection ();
3041
42+ try {
43+ connection .setAutoCommit (false );
44+ final T returnValue = databaseConnectedCallable .call (databaseConnection );
45+ connection .commit ();
46+ return returnValue ;
47+ }
48+ catch (final SQLException exception ) {
49+ connection .rollback ();
50+ throw exception ;
51+ }
3152 }
3253 catch (final Exception exception ) {
3354 throw new DatabaseException ("Unable to complete query." , exception );
You can’t perform that action at this time.
0 commit comments