From d80ab1d056dd09708e0b79def811a1d5ac72c279 Mon Sep 17 00:00:00 2001 From: Ippolitov A. Igor Date: Mon, 26 Oct 2015 13:15:03 +0300 Subject: sql search connection reopened every time --- src/main/java/com/juick/http/www/Main.java | 37 +++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'src/main/java/com/juick/http') diff --git a/src/main/java/com/juick/http/www/Main.java b/src/main/java/com/juick/http/www/Main.java index 56aa439f..faf334c9 100644 --- a/src/main/java/com/juick/http/www/Main.java +++ b/src/main/java/com/juick/http/www/Main.java @@ -47,6 +47,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { Connection sql; Connection sqlSearch; + String sqlSearchConnStr = "jdbc:mysql://127.0.0.1:9306?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000"; Stream xmpp; Home home = new Home(); Discover discover = new Discover(); @@ -64,6 +65,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { @Override public void init() throws ServletException { + super.init(); try { Properties conf = new Properties(); @@ -71,7 +73,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { Class.forName("com.mysql.jdbc.Driver"); sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); - sqlSearch = DriverManager.getConnection("jdbc:mysql://127.0.0.1:9306?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000", "", ""); + sqlSearch = getSqlSearch(); setupXmppComponent(conf.getProperty("xmpp_password")); @@ -80,7 +82,27 @@ public class Main extends HttpServlet implements Stream.StreamListener { log(null, e); } } - + public void closeSqlSearch() { + if (sqlSearch != null) { + try { + sqlSearch.close(); + sqlSearch = null; + } catch (SQLException e) { + log("An error on closing sql search connection", e); + } + } + }; + public Connection getSqlSearch() { + if (sqlSearch == null) { + try { + sqlSearch = DriverManager.getConnection(sqlSearchConnStr, "", ""); + } + catch (Exception e) { + log("Couldn't open sqlSearch connection",e); + } + } + return sqlSearch; + } public void setupXmppComponent(final String password) { Thread thr = new Thread(new Runnable() { @@ -130,7 +152,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { } } - /** + /** * Handles the HTTP GET method. * @param request servlet request * @param response servlet response @@ -150,7 +172,8 @@ public class Main extends HttpServlet implements Stream.StreamListener { Utils.sendPermanentRedirect(response, "/tag/" + URLEncoder.encode(tag, "UTF-8")); } else { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - home.doGet(sql, sqlSearch, request, response, visitor); + home.doGet(sql, getSqlSearch(), request, response, visitor); + closeSqlSearch(); } } else if (uri.equals("/post")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); @@ -201,7 +224,8 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else if (uri.startsWith("/help/")) { help.doGetHelp(sql, request, response); } else if (uri.startsWith("/tag/")) { - discover.doGet(sql, sqlSearch, request, response); + discover.doGet(sql, getSqlSearch(), request, response); + closeSqlSearch(); } else if (uri.matches("^/\\d+$")) { String strID = request.getRequestURI().substring(1); int mid = 0; @@ -229,7 +253,8 @@ public class Main extends HttpServlet implements Stream.StreamListener { com.juick.User user = com.juick.server.UserQueries.getUserByName(sql, uriparts[1]); if (user != null && user.UName.equals(uriparts[1]) && user.Banned == false) { if (uriparts.length == 2) { // http://juick.com/username/ - pagesUser.doGetBlog(sql, sqlSearch, request, response, user); + pagesUser.doGetBlog(sql, getSqlSearch(), request, response, user); + closeSqlSearch(); } else if (uriparts[2].equals("tags")) { pagesUser.doGetTags(sql, request, response, user); } else if (uriparts[2].equals("friends")) { -- cgit v1.2.3