diff options
-rw-r--r-- | src/main/java/com/juick/http/www/Main.java | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/main/java/com/juick/http/www/Main.java b/src/main/java/com/juick/http/www/Main.java index 56aa439f..e4c6b8ee 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() { @@ -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")) { |