-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTransactionManager.java
More file actions
76 lines (64 loc) · 2.7 KB
/
TransactionManager.java
File metadata and controls
76 lines (64 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.github.collinalpert.java2db.database;
import com.github.collinalpert.java2db.utilities.ThrowableConsumer;
import com.github.collinalpert.java2db.utilities.ThrowableFunction;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* Allows to execute code within a database transaction.
*
* This class maintains the notion of a 'current' database connection, which is bound to the currently
* executing thread via a {@link ThreadLocal}. The first call in the stack to execute code within a
* transaction opens a new connection and binds it to this thread local variable. Subsequent calls within
* the same thread which wish to participate within the transaction will then re-use this connection.
*
* @author Tyler Sharpe
*/
public class TransactionManager {
private static final ThreadLocal<DBConnection> CURRENT_THREAD_CONNECTION = new ThreadLocal<>();
private final DataSource dataSource;
public TransactionManager(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* Run some code inside of a database transaction, creating one if it does not already exist.
*/
public void transact(ThrowableConsumer<DBConnection, SQLException> action) throws SQLException {
transactAndReturn(connection -> {
action.consume(connection);
return null;
});
}
/**
* Run some code inside of a database transaction, creating one if it does not already exist, and then return a value.
* @param action Action to run
* @param <T> Type returned from the action lambda
* @return
* @throws SQLException
*/
public <T> T transactAndReturn(ThrowableFunction<DBConnection, T, SQLException> action) throws SQLException {
if (CURRENT_THREAD_CONNECTION.get() != null) {
return action.run(CURRENT_THREAD_CONNECTION.get());
}
try (Connection rawConnection = dataSource.getConnection()) {
rawConnection.setAutoCommit(false);
DBConnection dbConnection = new DBConnection(rawConnection);
CURRENT_THREAD_CONNECTION.set(dbConnection);
try {
T result = action.run(dbConnection);
rawConnection.commit();
return result;
} catch (Exception exception) {
// rollback transaction on error
try {
rawConnection.rollback();
} catch (Exception rollbackException) {
exception.addSuppressed(rollbackException);
}
throw new SQLException(exception);
}
} finally {
CURRENT_THREAD_CONNECTION.remove();
}
}
}