Commit 84654d51 authored by Liang Ding's avatar Liang Ding

在 Latke 框架内回收可能泄露的 DB 连接

parent 023fc9c4
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
Description: Solo POM. Description: Solo POM.
Version: 3.18.3.31, Oct 27, 2018 Version: 3.18.3.32, Oct 31, 2018
Author: <a href="http://88250.b3log.org">Liang Ding</a> Author: <a href="http://88250.b3log.org">Liang Ding</a>
Author: <a href="http://www.annpeter.cn">Ann Peter</a> Author: <a href="http://www.annpeter.cn">Ann Peter</a>
Author: <a href="http://vanessa.b3log.org">Vanessa</a> Author: <a href="http://vanessa.b3log.org">Vanessa</a>
...@@ -75,7 +75,7 @@ ...@@ -75,7 +75,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.b3log.latke.version>2.4.23</org.b3log.latke.version> <org.b3log.latke.version>2.4.25</org.b3log.latke.version>
<servlet.version>3.1.0</servlet.version> <servlet.version>3.1.0</servlet.version>
<slf4j.version>1.7.5</slf4j.version> <slf4j.version>1.7.5</slf4j.version>
......
...@@ -29,7 +29,6 @@ import org.b3log.latke.logging.Logger; ...@@ -29,7 +29,6 @@ import org.b3log.latke.logging.Logger;
import org.b3log.latke.plugin.PluginManager; import org.b3log.latke.plugin.PluginManager;
import org.b3log.latke.plugin.ViewLoadEventHandler; import org.b3log.latke.plugin.ViewLoadEventHandler;
import org.b3log.latke.repository.Transaction; import org.b3log.latke.repository.Transaction;
import org.b3log.latke.repository.jdbc.JdbcRepository;
import org.b3log.latke.servlet.AbstractServletListener; import org.b3log.latke.servlet.AbstractServletListener;
import org.b3log.latke.util.Requests; import org.b3log.latke.util.Requests;
import org.b3log.latke.util.Stopwatchs; import org.b3log.latke.util.Stopwatchs;
...@@ -52,7 +51,7 @@ import javax.servlet.http.HttpSessionEvent; ...@@ -52,7 +51,7 @@ import javax.servlet.http.HttpSessionEvent;
* Solo Servlet listener. * Solo Servlet listener.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.10.0.1, Oct 24, 2018 * @version 1.10.0.2, Oct 31, 2018
* @since 0.3.1 * @since 0.3.1
*/ */
public final class SoloServletListener extends AbstractServletListener { public final class SoloServletListener extends AbstractServletListener {
...@@ -91,8 +90,6 @@ public final class SoloServletListener extends AbstractServletListener { ...@@ -91,8 +90,6 @@ public final class SoloServletListener extends AbstractServletListener {
final ImportService importService = beanManager.getReference(ImportService.class); final ImportService importService = beanManager.getReference(ImportService.class);
importService.importMarkdowns(); importService.importMarkdowns();
JdbcRepository.dispose();
final OptionRepository optionRepository = beanManager.getReference(OptionRepository.class); final OptionRepository optionRepository = beanManager.getReference(OptionRepository.class);
final Transaction transaction = optionRepository.beginTransaction(); final Transaction transaction = optionRepository.beginTransaction();
try { try {
......
...@@ -28,7 +28,6 @@ import org.b3log.latke.logging.Level; ...@@ -28,7 +28,6 @@ import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger; import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.Role; import org.b3log.latke.model.Role;
import org.b3log.latke.model.User; import org.b3log.latke.model.User;
import org.b3log.latke.repository.jdbc.JdbcRepository;
import org.b3log.latke.servlet.HTTPRequestMethod; import org.b3log.latke.servlet.HTTPRequestMethod;
import org.b3log.latke.servlet.annotation.RequestProcessing; import org.b3log.latke.servlet.annotation.RequestProcessing;
import org.b3log.latke.servlet.annotation.RequestProcessor; import org.b3log.latke.servlet.annotation.RequestProcessor;
...@@ -56,7 +55,7 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -56,7 +55,7 @@ import java.util.concurrent.ConcurrentHashMap;
* </ul> * </ul>
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Sep 21, 2018 * @version 1.0.0.1, Oct 31, 2018
* @since 2.9.5 * @since 2.9.5
*/ */
@RequestProcessor @RequestProcessor
...@@ -204,7 +203,6 @@ public class OAuthGitHubProcessor { ...@@ -204,7 +203,6 @@ public class OAuthGitHubProcessor {
addUserReq.put(UserExt.USER_AVATAR, userAvatar); addUserReq.put(UserExt.USER_AVATAR, userAvatar);
addUserReq.put(User.USER_ROLE, Role.VISITOR_ROLE); addUserReq.put(User.USER_ROLE, Role.VISITOR_ROLE);
userMgmtService.addUser(addUserReq); userMgmtService.addUser(addUserReq);
JdbcRepository.dispose();
} }
} }
......
...@@ -24,7 +24,6 @@ import org.b3log.latke.Keys; ...@@ -24,7 +24,6 @@ import org.b3log.latke.Keys;
import org.b3log.latke.ioc.Inject; import org.b3log.latke.ioc.Inject;
import org.b3log.latke.logging.Level; import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger; import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.jdbc.JdbcRepository;
import org.b3log.latke.service.annotation.Service; import org.b3log.latke.service.annotation.Service;
import org.b3log.latke.util.Strings; import org.b3log.latke.util.Strings;
import org.b3log.solo.SoloServletListener; import org.b3log.solo.SoloServletListener;
...@@ -40,7 +39,7 @@ import java.util.*; ...@@ -40,7 +39,7 @@ import java.util.*;
* Import service. * Import service.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.1.2, Sep 16, 2018 * @version 1.0.1.3, Oct 31, 2018
* @since 2.2.0 * @since 2.2.0
*/ */
@Service @Service
...@@ -74,76 +73,72 @@ public class ImportService { ...@@ -74,76 +73,72 @@ public class ImportService {
*/ */
public void importMarkdowns() { public void importMarkdowns() {
new Thread(() -> { new Thread(() -> {
final ServletContext servletContext = SoloServletListener.getServletContext();
final String markdownsPath = servletContext.getRealPath("markdowns");
LOGGER.debug("Import directory [" + markdownsPath + "]");
JSONObject admin;
try { try {
final ServletContext servletContext = SoloServletListener.getServletContext(); admin = userQueryService.getAdmin();
final String markdownsPath = servletContext.getRealPath("markdowns"); } catch (final Exception e) {
LOGGER.debug("Import directory [" + markdownsPath + "]"); return;
}
JSONObject admin; if (null == admin) { // Not init yet
try { return;
admin = userQueryService.getAdmin(); }
} catch (final Exception e) {
return;
}
if (null == admin) { // Not init yet final String adminId = admin.optString(Keys.OBJECT_ID);
return;
}
final String adminId = admin.optString(Keys.OBJECT_ID); int succCnt = 0, failCnt = 0;
final Set<String> failSet = new TreeSet<>();
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true);
if (null == mds || mds.isEmpty()) {
return;
}
int succCnt = 0, failCnt = 0; for (final File md : mds) {
final Set<String> failSet = new TreeSet<>(); final String fileName = md.getName();
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true); if (StringUtils.equalsIgnoreCase(fileName, "README.md")) {
if (null == mds || mds.isEmpty()) { continue;
return;
} }
for (final File md : mds) { try {
final String fileName = md.getName(); final String fileContent = FileUtils.readFileToString(md, "UTF-8");
if (StringUtils.equalsIgnoreCase(fileName, "README.md")) { final JSONObject article = parseArticle(fileName, fileContent);
continue; article.put(Article.ARTICLE_AUTHOR_ID, adminId);
}
final JSONObject request = new JSONObject();
try { request.put(Article.ARTICLE, article);
final String fileContent = FileUtils.readFileToString(md, "UTF-8");
final JSONObject article = parseArticle(fileName, fileContent); final String id = articleMgmtService.addArticle(request);
article.put(Article.ARTICLE_AUTHOR_ID, adminId); FileUtils.moveFile(md, new File(md.getPath() + "." + id));
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
final JSONObject request = new JSONObject(); succCnt++;
request.put(Article.ARTICLE, article); } catch (final Exception e) {
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
final String id = articleMgmtService.addArticle(request);
FileUtils.moveFile(md, new File(md.getPath() + "." + id));
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
succCnt++;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
failCnt++;
failSet.add(fileName);
}
}
if (0 == succCnt && 0 == failCnt) { failCnt++;
return; failSet.add(fileName);
} }
}
if (0 == succCnt && 0 == failCnt) {
return;
}
final StringBuilder logBuilder = new StringBuilder(); final StringBuilder logBuilder = new StringBuilder();
logBuilder.append("[").append(succCnt).append("] imported, [").append(failCnt).append("] failed"); logBuilder.append("[").append(succCnt).append("] imported, [").append(failCnt).append("] failed");
if (failCnt > 0) { if (failCnt > 0) {
logBuilder.append(": ").append(Strings.LINE_SEPARATOR); logBuilder.append(": ").append(Strings.LINE_SEPARATOR);
for (final String fail : failSet) { for (final String fail : failSet) {
logBuilder.append(" ").append(fail).append(Strings.LINE_SEPARATOR); logBuilder.append(" ").append(fail).append(Strings.LINE_SEPARATOR);
}
} else {
logBuilder.append(" :p");
} }
LOGGER.info(logBuilder.toString()); } else {
} finally { logBuilder.append(" :p");
JdbcRepository.dispose();
} }
LOGGER.info(logBuilder.toString());
}).start(); }).start();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment