Skip to content

Commit d8608b5

Browse files
committed
🐛 修复数据库连接泄露
1 parent 60c2350 commit d8608b5

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

src/main/java/org/b3log/solo/service/ImportService.java

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.b3log.latke.logging.Level;
2323
import org.b3log.latke.logging.Logger;
2424
import org.b3log.latke.model.User;
25+
import org.b3log.latke.repository.jdbc.JdbcRepository;
2526
import org.b3log.latke.service.annotation.Service;
2627
import org.b3log.latke.util.Strings;
2728
import org.b3log.solo.SoloServletListener;
@@ -37,7 +38,7 @@
3738
* Import service.
3839
*
3940
* @author <a href="http://88250.b3log.org">Liang Ding</a>
40-
* @version 1.0.0.2, Jul 3, 2017
41+
* @version 1.0.1.0, Aug 31, 2017
4142
* @since 2.2.0
4243
*/
4344
@Service
@@ -71,72 +72,76 @@ public class ImportService {
7172
*/
7273
public void importMarkdowns() {
7374
new Thread(() -> {
74-
final ServletContext servletContext = SoloServletListener.getServletContext();
75-
final String markdownsPath = servletContext.getRealPath("markdowns");
76-
LOGGER.debug("Import directory [" + markdownsPath + "]");
77-
78-
JSONObject admin;
7975
try {
80-
admin = userQueryService.getAdmin();
81-
} catch (final Exception e) {
82-
return;
83-
}
84-
85-
if (null == admin) { // Not init yet
86-
return;
87-
}
76+
final ServletContext servletContext = SoloServletListener.getServletContext();
77+
final String markdownsPath = servletContext.getRealPath("markdowns");
78+
LOGGER.debug("Import directory [" + markdownsPath + "]");
8879

89-
final String adminEmail = admin.optString(User.USER_EMAIL);
90-
91-
int succCnt = 0, failCnt = 0;
92-
final Set<String> failSet = new TreeSet<>();
93-
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true);
94-
if (null == mds || mds.isEmpty()) {
95-
return;
96-
}
97-
98-
for (final File md : mds) {
99-
final String fileName = md.getName();
100-
if (StringUtils.equalsIgnoreCase(fileName, "README.md")) {
101-
continue;
80+
JSONObject admin;
81+
try {
82+
admin = userQueryService.getAdmin();
83+
} catch (final Exception e) {
84+
return;
10285
}
10386

104-
try {
105-
final String fileContent = FileUtils.readFileToString(md, "UTF-8");
106-
final JSONObject article = parseArticle(fileName, fileContent);
107-
article.put(Article.ARTICLE_AUTHOR_EMAIL, adminEmail);
87+
if (null == admin) { // Not init yet
88+
return;
89+
}
10890

109-
final JSONObject request = new JSONObject();
110-
request.put(Article.ARTICLE, article);
91+
final String adminEmail = admin.optString(User.USER_EMAIL);
11192

112-
final String id = articleMgmtService.addArticle(request);
113-
FileUtils.moveFile(md, new File(md.getPath() + "." + id));
114-
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
115-
succCnt++;
116-
} catch (final Exception e) {
117-
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
93+
int succCnt = 0, failCnt = 0;
94+
final Set<String> failSet = new TreeSet<>();
95+
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true);
96+
if (null == mds || mds.isEmpty()) {
97+
return;
98+
}
11899

119-
failCnt++;
120-
failSet.add(fileName);
100+
for (final File md : mds) {
101+
final String fileName = md.getName();
102+
if (StringUtils.equalsIgnoreCase(fileName, "README.md")) {
103+
continue;
104+
}
105+
106+
try {
107+
final String fileContent = FileUtils.readFileToString(md, "UTF-8");
108+
final JSONObject article = parseArticle(fileName, fileContent);
109+
article.put(Article.ARTICLE_AUTHOR_EMAIL, adminEmail);
110+
111+
final JSONObject request = new JSONObject();
112+
request.put(Article.ARTICLE, article);
113+
114+
final String id = articleMgmtService.addArticle(request);
115+
FileUtils.moveFile(md, new File(md.getPath() + "." + id));
116+
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
117+
succCnt++;
118+
} catch (final Exception e) {
119+
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
120+
121+
failCnt++;
122+
failSet.add(fileName);
123+
}
121124
}
122-
}
123125

124-
if (0 == succCnt && 0 == failCnt) {
125-
return;
126-
}
126+
if (0 == succCnt && 0 == failCnt) {
127+
return;
128+
}
127129

128-
final StringBuilder logBuilder = new StringBuilder();
129-
logBuilder.append("[").append(succCnt).append("] imported, [").append(failCnt).append("] failed");
130-
if (failCnt > 0) {
131-
logBuilder.append(": ").append(Strings.LINE_SEPARATOR);
130+
final StringBuilder logBuilder = new StringBuilder();
131+
logBuilder.append("[").append(succCnt).append("] imported, [").append(failCnt).append("] failed");
132+
if (failCnt > 0) {
133+
logBuilder.append(": ").append(Strings.LINE_SEPARATOR);
132134

133-
for (final String fail : failSet) {
134-
logBuilder.append(" ").append(fail).append(Strings.LINE_SEPARATOR);
135+
for (final String fail : failSet) {
136+
logBuilder.append(" ").append(fail).append(Strings.LINE_SEPARATOR);
137+
}
138+
} else {
139+
logBuilder.append(" :p");
135140
}
136-
} else {
137-
logBuilder.append(" :p");
141+
LOGGER.info(logBuilder.toString());
142+
} finally {
143+
JdbcRepository.dispose();
138144
}
139-
LOGGER.info(logBuilder.toString());
140145
}).start();
141146
}
142147

0 commit comments

Comments
 (0)