Skip to content

Commit 7f04674

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 5c6ad86 + 9c3ea26 commit 7f04674

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}

src/main/java/com/softwareverde/database/transaction/DatabaseTransaction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import 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
}

src/main/java/com/softwareverde/database/transaction/JdbcDatabaseTransaction.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.softwareverde.database.DatabaseException;
66

77
import java.sql.Connection;
8+
import java.sql.SQLException;
89

910
public 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);

0 commit comments

Comments
 (0)