forked from santanusinha/dropwizard-db-sharding-bundle
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TransactionRunner.java
108 lines (93 loc) · 3.95 KB
/
TransactionRunner.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
/*
* Copyright 2018 Cleartax
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package in.cleartax.dropwizard.sharding.transactions;
import in.cleartax.dropwizard.sharding.hibernate.ConstTenantIdentifierResolver;
import in.cleartax.dropwizard.sharding.hibernate.DelegatingTenantResolver;
import in.cleartax.dropwizard.sharding.hibernate.MultiTenantUnitOfWorkAspect;
import in.cleartax.dropwizard.sharding.hibernate.MultiTenantUnitOfWorkAwareProxyFactory;
import in.cleartax.dropwizard.sharding.transactions.listeners.TransactionRunnerListener;
import io.dropwizard.hibernate.UnitOfWork;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInvocation;
import org.hibernate.SessionFactory;
import org.hibernate.context.internal.ManagedSessionContext;
import javax.annotation.Nullable;
import java.lang.reflect.Method;
import java.util.Objects;
@RequiredArgsConstructor
@Slf4j
public abstract class TransactionRunner<T> {
private final MultiTenantUnitOfWorkAwareProxyFactory proxyFactory;
private final SessionFactory sessionFactory;
private final ConstTenantIdentifierResolver tenantIdentifierResolver;
private final TransactionContext transactionContext;
@Nullable
@Setter
private TransactionRunnerListener listener;
public T start(boolean reUseSession, UnitOfWork unitOfWork) throws Throwable {
if (reUseSession && ManagedSessionContext.hasBind(sessionFactory) &&
tenantIdentifierResolver.resolveCurrentTenantIdentifier()
.equals(DelegatingTenantResolver.getInstance().resolveCurrentTenantIdentifier())) {
return run();
}
DelegatingTenantResolver.getInstance().setDelegate(tenantIdentifierResolver);
MultiTenantUnitOfWorkAspect aspect = proxyFactory.newAspect();
Exception ex = null;
T result = null;
long startTime = System.currentTimeMillis();
try {
invokeStartListener(unitOfWork);
aspect.beforeStart(unitOfWork);
result = run();
aspect.afterEnd();
} catch (Exception e) {
ex = e;
aspect.onError();
} finally {
aspect.onFinish();
long timeElapsed = System.currentTimeMillis() - startTime;
log.trace("[DATABASE] transaction={} error={} context={} time-elapsed={}",
unitOfWork.transactional(), ex != null, resolveOperationName(transactionContext.getMethodOfInvocation()), timeElapsed);
DelegatingTenantResolver.getInstance().setDelegate(null);
invokeFinishListener(ex != null, unitOfWork, timeElapsed);
}
if (ex != null) {
throw ex;
}
return result;
}
private void invokeStartListener(UnitOfWork unitOfWork) {
if (listener != null) {
listener.onStart(unitOfWork, transactionContext);
}
}
private void invokeFinishListener(boolean success, UnitOfWork unitOfWork, long timeElapsed) {
if (listener != null) {
listener.onFinish(success, unitOfWork, transactionContext, timeElapsed);
}
}
private String resolveOperationName(Method method) {
if (Objects.nonNull(method)) {
return String.format("%s#%s", method.getDeclaringClass().getSimpleName(), method.getName());
}
return null;
}
public abstract T run() throws Throwable;
}