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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
* SonarQube
* Copyright (C) 2009-2021 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db;
import java.lang.reflect.Field;
import java.util.Objects;
import java.util.function.Supplier;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import static com.google.common.base.Preconditions.checkState;
import static java.lang.String.format;
import static java.lang.Thread.currentThread;
public class DBSessionsImpl implements DBSessions {
private static final Logger LOG = Loggers.get(DBSessionsImpl.class);
private static final ThreadLocal<Boolean> CACHING_ENABLED = ThreadLocal.withInitial(() -> Boolean.FALSE);
private final ThreadLocal<DelegatingDbSessionSupplier> regularDbSession = ThreadLocal.withInitial(this::buildRegularDbSessionSupplier);
private final ThreadLocal<DelegatingDbSessionSupplier> batchDbSession = ThreadLocal.withInitial(this::buildBatchDbSessionSupplier);
private final MyBatis myBatis;
public DBSessionsImpl(MyBatis myBatis) {
this.myBatis = myBatis;
}
private DelegatingDbSessionSupplier buildRegularDbSessionSupplier() {
return new DelegatingDbSessionSupplier(() -> {
DbSession res = myBatis.openSession(false);
ensureAutoCommitFalse(res);
return res;
});
}
private DelegatingDbSessionSupplier buildBatchDbSessionSupplier() {
return new DelegatingDbSessionSupplier(() -> {
DbSession res = myBatis.openSession(true);
ensureAutoCommitFalse(res);
return res;
});
}
private static void ensureAutoCommitFalse(DbSession dbSession) {
try {
SqlSession sqlSession = dbSession.getSqlSession();
if (sqlSession instanceof DefaultSqlSession) {
Field f = sqlSession.getClass().getDeclaredField("autoCommit");
f.setAccessible(true);
checkState(!((boolean) f.get(sqlSession)), "Autocommit must be false");
}
} catch (NoSuchFieldException | IllegalAccessException e) {
LOG.debug("Failed to check the autocommit status of SqlSession: {}", e);
}
}
@Override
public void enableCaching() {
CACHING_ENABLED.set(Boolean.TRUE);
}
@Override
public DbSession openSession(boolean batch) {
if (!CACHING_ENABLED.get()) {
return myBatis.openSession(batch);
}
if (batch) {
return new NonClosingDbSession(batchDbSession.get().get());
}
return new NonClosingDbSession(regularDbSession.get().get());
}
@Override
public void disableCaching() {
close(regularDbSession, "regular");
close(batchDbSession, "batch");
regularDbSession.remove();
batchDbSession.remove();
CACHING_ENABLED.remove();
}
public void close(ThreadLocal<DelegatingDbSessionSupplier> dbSessionThreadLocal, String label) {
DelegatingDbSessionSupplier delegatingDbSessionSupplier = dbSessionThreadLocal.get();
boolean getCalled = delegatingDbSessionSupplier.isPopulated();
if (getCalled) {
try {
DbSession res = delegatingDbSessionSupplier.get();
res.close();
} catch (Exception e) {
LOG.error(format("Failed to close %s connection in %s", label, currentThread()), e);
}
}
}
/**
* A {@link Supplier} of {@link DelegatingDbSession} which logs whether {@link Supplier#get() get} has been called at
* least once, delegates the actual supplying to the a specific {@link Supplier<NonClosingDbSession>} instance and
* caches the supplied {@link NonClosingDbSession}.
*/
private static final class DelegatingDbSessionSupplier implements Supplier<DbSession> {
private final Supplier<DbSession> delegate;
private DbSession dbSession;
DelegatingDbSessionSupplier(Supplier<DbSession> delegate) {
this.delegate = delegate;
}
@Override
public DbSession get() {
if (dbSession == null) {
dbSession = Objects.requireNonNull(delegate.get());
}
return dbSession;
}
boolean isPopulated() {
return dbSession != null;
}
}
}
|