From f0834c91bfcd8b5d6f658c946334ad6b89ad36a3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 16 Jan 2016 18:33:36 +0300 Subject: almost working with spring-jdbc --- src/main/java/com/juick/api/Main.java | 39 +++++----------------------------- src/main/java/com/juick/api/Utils.java | 2 +- 2 files changed, 6 insertions(+), 35 deletions(-) (limited to 'src/main/java/com/juick/api') diff --git a/src/main/java/com/juick/api/Main.java b/src/main/java/com/juick/api/Main.java index 4233c45d..cdced593 100644 --- a/src/main/java/com/juick/api/Main.java +++ b/src/main/java/com/juick/api/Main.java @@ -33,9 +33,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; import java.util.Properties; /** @@ -46,8 +43,6 @@ import java.util.Properties; @MultipartConfig public class Main extends HttpServlet implements Stream.StreamListener { - Connection sql; - Connection sqlSearch; JdbcTemplate jdbc; Stream xmpp; Messages messages; @@ -62,11 +57,8 @@ public class Main extends HttpServlet implements Stream.StreamListener { Properties conf = new Properties(); conf.load(new FileInputStream("/etc/juick/api.conf")); final String driverClassName = "com.mysql.jdbc.Driver"; - Class.forName(driverClassName); - 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/juick?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000&relaxAutoCommit=true&user=root&password="); DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setUrl(sql.getMetaData().getURL()); + dataSource.setUrl("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); dataSource.setDriverClassName(driverClassName); jdbc = new JdbcTemplate(dataSource); messages = new Messages(jdbc); @@ -76,8 +68,8 @@ public class Main extends HttpServlet implements Stream.StreamListener { setupXmppComponent(conf.getProperty("xmpp_password")); - } catch (IOException | ClassNotFoundException | SQLException e) { - log(null, e); + } catch (IOException e) { + log("API initialization error", e); } } @@ -105,27 +97,6 @@ public class Main extends HttpServlet implements Stream.StreamListener { System.err.println("XMPP STREAM READY"); } - @Override - public void destroy() { - super.destroy(); - if (sql != null) { - try { - sql.close(); - sql = null; - } catch (SQLException e) { - log(null, e); - } - } - if (sqlSearch != null) { - try { - sqlSearch.close(); - sqlSearch = null; - } catch (SQLException e) { - log(null, e); - } - } - } - /** * Handles the HTTP GET method. * @param request servlet request @@ -139,7 +110,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { request.setCharacterEncoding("UTF-8"); } - int vuid = Utils.getHttpAuthUID(sql, request); + int vuid = Utils.getHttpAuthUID(jdbc, request); if (vuid == 0) { vuid = Utils.getVisitorQueryStringUID(jdbc, request); } @@ -197,7 +168,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { request.setCharacterEncoding("UTF-8"); } - int vuid = Utils.getHttpAuthUID(sql, request); + int vuid = Utils.getHttpAuthUID(jdbc, request); if (vuid == 0) { vuid = Utils.getVisitorQueryStringUID(jdbc, request); } diff --git a/src/main/java/com/juick/api/Utils.java b/src/main/java/com/juick/api/Utils.java index db73fdaa..ccb81f8e 100644 --- a/src/main/java/com/juick/api/Utils.java +++ b/src/main/java/com/juick/api/Utils.java @@ -70,7 +70,7 @@ public class Utils { return 0; } - public static int getHttpAuthUID(Connection sql, HttpServletRequest request) { + public static int getHttpAuthUID(JdbcTemplate sql, HttpServletRequest request) { String auth = request.getHeader("Authorization"); if (auth != null && auth.length() > 8 && auth.startsWith("Basic ")) { try { -- cgit v1.2.3