-
Notifications
You must be signed in to change notification settings - Fork 10
/
TransactionHelper.java
112 lines (96 loc) · 3.75 KB
/
TransactionHelper.java
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.webpieces.plugin.hibernate;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import org.webpieces.ctx.api.Current;
import org.webpieces.ctx.api.RequestContext;
import org.webpieces.ctx.api.RouterRequest;
import org.webpieces.plugin.hibernate.metrics.DatabaseMetric;
import org.webpieces.plugin.hibernate.metrics.DatabaseTransactionTags;
import org.webpieces.util.context.Context;
import org.webpieces.util.context.ContextKey;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @deprecated Replaced by {@link PersistenceHelper}.
*/
@Deprecated
@Singleton
public class TransactionHelper {
private final EntityManagerFactory factory;
private final TxCompleters txCompleters;
private final MeterRegistry meterRegistry;
@Inject
public TransactionHelper(EntityManagerFactory factory, TxCompleters txCompleters, MeterRegistry meterRegistry) {
this.factory = factory;
this.txCompleters = txCompleters;
this.meterRegistry = meterRegistry;
}
public <Resp> Resp runWithEm(Supplier<Resp> function) {
if (Em.get() != null) {
throw new IllegalStateException("Cannot open another entityManager when are already have one open");
}
EntityManager mgr = factory.createEntityManager();
Em.set(mgr);
try {
Resp resp = function.get();
mgr.close();
return resp;
} catch (Throwable t) {
txCompleters.closeEm(t, mgr);
throw t;
} finally {
Em.set(null);
}
}
/**
* @deprecated Use {@link #runTransaction(String, Supplier)} instead where you can supply a name for the transaction
*/
@Deprecated
public <Resp> Resp runTransaction(Supplier<Resp> supplier) {
return runTransaction("unknown", supplier);
}
public <Resp> Resp runTransaction(String transactionName, Supplier<Resp> supplier) {
if (Em.get() == null) {
return runWithEm(() -> runTransactionImpl(transactionName, supplier));
} else {
return runTransactionImpl(transactionName, supplier);
}
}
private <Resp> Resp runTransactionImpl(String transactionName, Supplier<Resp> supplier) {
long begin = System.currentTimeMillis();
EntityTransaction tx = Em.get().getTransaction();
if (tx.isActive()) {
throw new IllegalStateException("Cannot open another transaction when one is already open");
}
tx.begin();
try {
Resp resp = supplier.get();
tx.commit();
return resp;
} catch (RuntimeException e) {
txCompleters.rollbackTx(e, tx);
// Rethrow with suppressed exception if the rollback fails too
throw e;
} finally {
monitorTransactionTime(transactionName, begin);
}
}
private void monitorTransactionTime(String transactionName, long begin) {
String requestPath = (String) Context.get(ContextKey.REQUEST_PATH.toString());
if (requestPath == null || requestPath.isBlank()) {
requestPath = "unknown";
}
Tags transactionTags = Tags.of(
DatabaseTransactionTags.EXECUTION_ID, transactionName,
DatabaseTransactionTags.REQUEST, requestPath
);
meterRegistry.timer(DatabaseMetric.EXECUTION_TIME.getDottedMetricName(), transactionTags)
.record(System.currentTimeMillis() - begin, TimeUnit.MILLISECONDS);
}
}