Commit a47838a4 authored by Eduardo L. Buratti's avatar Eduardo L. Buratti
Browse files

server: Make all DB connections use JDBC connection pool

parent 7509fb94
......@@ -32,6 +32,9 @@ import java.sql.Statement;
import java.sql.SQLException;
import java.sql.Timestamp;
import org.postgresql.Driver;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;
/* File libs */
import java.io.BufferedReader;
......@@ -62,58 +65,31 @@ public class Seed2 {
******************************************************************************/
/* Constants - set their values manually */
/* Used by verifyInep() and storeInventoryData() */
private static final String HOST = "";
private static final String PORT = "";
private static final String SCHEMA = "";
private static final String USER = "";
private static final String PASSWD = "";
/* Used by verifyInep() */
private static final String SCHTABLE = "";
private static final String SCHTABLE = "dim_school";
/* Used by getNetQueries() */
private static final String NETTABLE = "";
private static final String NETTABLE = "proinfo_net_usage";
/* Used by getInventoryQueries() */
private static final String INVTABLE = "";
private static final String INVTABLE = "proinfo_inventory";
/* Used by getVersionFromFile() */
private static final String LINUX_VERFILE = "";
private static final String WIN_VERFILE = "";
private static final String ANDROID_PROINFO_VERFILE = "";
private static final String ANDROID_EDUBAR_VERFILE = "";
private static final String LINUX_VERFILE = "/home/proinfodata/conf/agent-version-linux";
private static final String WIN_VERFILE = "/home/proinfodata/conf/agent-version-win";
private static final String ANDROID_PROINFO_VERFILE = "/home/proinfodata/conf/agent-version-android";
private static final String ANDROID_EDUBAR_VERFILE = "/home/proinfodata/conf/agent-version-android-edubar";
/* Used by getUpdateLink() */
private static final String BASEURL = "";
private static final String LINUX_PKGNAME = "";
private static final String WIN_PKGNAME = "";
private static final String BASEURL = "http://proinfodata.c3sl.ufpr.br/downloads/";
private static final String LINUX_PKGNAME = "Pacote_de_Atualizacao_SEED2_v";
private static final String WIN_PKGNAME = "proinfodata_windows_";
private static final String ANDROID_PROINFO_PKGNAME = "";
private static final String ANDROID_EDUBAR_PKGNAME = "";
private static final String LINUX_EXTENSION = "";
private static final String WIN_EXTENSION = "";
private static final String ANDROID_EXTENSION = "";
private static final String LINUX_EXTENSION = ".run";
private static final String WIN_EXTENSION = ".exe";
private static final String ANDROID_EXTENSION = ".apk";
/******************************************************************************
* #1 - Methods related to database
******************************************************************************/
/**
* Return a string with database url.
*
* @author Danilo Kiyoshi Simizu Yorinori
* @param host DB Host
* @param port DB Port
* @param schema DB Schema
* @return String
*/
private static String createDbConnectionString(String host, String port,
String schema) {
StringBuilder sb = new StringBuilder("jdbc:postgresql://");
sb.append(host);
sb.append(":");
sb.append(port);
sb.append("/");
sb.append(schema);
return sb.toString();
}
/**
* Return a string to be appended on SQL statement according to key type and
* value. The ending of string is different according to its appending location.
......@@ -942,45 +918,45 @@ throws Exception {
*/
private static boolean verifyInep(String inep)
throws Exception {
/* DB connection variable */
Connection conn = null;
Context ctx = new InitialContext();
Driver.class.newInstance();
if (ctx == null)
throw new Exception("No context found.");
/* Default value */
boolean find = false;
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/proinfodata");
/* Database connection */
try {
String dbConn = createDbConnectionString(HOST, PORT, SCHEMA);
conn = DriverManager.getConnection(dbConn, USER, PASSWD);
if (ds == null)
throw new Exception("Datasource jdbc/proinfodata not found.");
Connection conn = ds.getConnection();
if (conn == null)
throw new Exception("Failed to acquire connection from JDBC pool.");
boolean found = false;
try {
Statement stmt = conn.createStatement();
try {
/* Query for inep */
String query = createIdVerificationQuery(SCHTABLE, inep);
ResultSet rs = stmt.executeQuery(query);
/* Verify if query result is not empty */
if (rs.next() == true) {
/* Change return value */
find = true;
}
/* Query for inep */
String query = createIdVerificationQuery(SCHTABLE, inep);
ResultSet rs = stmt.executeQuery(query);
/* Close result set and statement */
rs.close();
stmt.close();
} catch (SQLException e) {
throw new Exception(e.getMessage());
/* Verify if query result is not empty */
if (rs.next() == true) {
/* Change return value */
found = true;
}
} catch (SQLException e) {
throw new Exception(e.getMessage());
} finally {
/* Close result set and statement */
rs.close();
stmt.close();
}
finally {
conn.close(); /* Close database connection */
}
return find;
return found;
}
/******************************************************************************
......@@ -1941,39 +1917,38 @@ throws Exception {
*/
private static boolean storeInventoryData(String queries)
throws Exception {
/* DB connection variable */
Connection conn = null;
Context ctx = new InitialContext();
/* Control variable */
int insert = -1;
if (ctx == null)
throw new Exception("No context found.");
String errorMsg = null;
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/proinfodata");
Driver.class.newInstance();
if (ds == null)
throw new Exception("Datasource jdbc/proinfodata not found.");
/* Database connection */
try {
/* Open connection */
String dbConn = createDbConnectionString(HOST, PORT, SCHEMA);
conn = DriverManager.getConnection(dbConn, USER, PASSWD);
Connection conn = ds.getConnection();
if (conn == null)
throw new Exception("Failed to acquire connection from JDBC pool.");
// query execution return value
int insert = -1;
try {
Statement stmt = conn.createStatement();
insert = stmt.executeUpdate(queries);
stmt.close(); /* Close statement */
} catch (SQLException e) {
throw new Exception(e.getMessage());
} finally {
conn.close(); /* Close database connection */
stmt.close();
}
if (insert > 0) {
return true;
} else {
errorMsg = "Error while inserting data into database";
throw new Exception(errorMsg);
finally {
conn.close(); /* Close database connection */
}
if (insert <= 0)
throw new Exception("Error while inserting data into database");
return true;
}
/******************************************************************************
......@@ -2064,18 +2039,20 @@ public static String getUpdateLink(int os)
{
try {
String version = getVersionFromFile(os);
String ret = null;
if (os == 0)
return BASEURL + LINUX_PKGNAME + version + LINUX_EXTENSION;
ret = BASEURL + LINUX_PKGNAME + version + LINUX_EXTENSION;
else if (os == 1)
return BASEURL + WIN_PKGNAME + version + WIN_EXTENSION;
ret = BASEURL + WIN_PKGNAME + version + WIN_EXTENSION;
else if (os == 2)
return BASEURL + ANDROID_PROINFO_PKGNAME + version + ANDROID_EXTENSION;
ret = BASEURL + ANDROID_PROINFO_PKGNAME + version + ANDROID_EXTENSION;
else if (os == 3)
return BASEURL + ANDROID_EDUBAR_PKGNAME + version + ANDROID_EXTENSION;
else
return null;
ret = BASEURL + ANDROID_EDUBAR_PKGNAME + version + ANDROID_EXTENSION;
writeSuccessToLog("getUpdateLink", "os: " + os + "; ret: " + ret);
return ret;
} catch (Exception e) {
writeErrorToLog(e.getMessage(), "getUpdateLink");
return null;
......
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