Example usage for java.sql Statement setPoolable

List of usage examples for java.sql Statement setPoolable

Introduction

In this page you can find the example usage for java.sql Statement setPoolable.

Prototype

void setPoolable(boolean poolable) throws SQLException;

Source Link

Document

Requests that a Statement be pooled or not pooled.

Usage

From source file:edumsg.core.commands.list.GetListCommand.java

@Override
public void execute() {

    try {//w w  w  .j a  v  a  2 s .  c  o  m
        dbConn = PostgresConnection.getDataSource().getConnection();
        dbConn.setAutoCommit(true);
        Statement query = dbConn.createStatement();
        query.setPoolable(true);
        set = query.executeQuery(String.format("SELECT * FROM get_list(%s)", map.get("list_id")));
        List l = new List();
        while (set.next()) {
            l.setName(set.getString("name"));
            l.setDescription(set.getString("description"));

        }

        ValueNode list = nf.pojoNode(l);
        set.close();

        root.put("app", map.get("app"));
        root.put("method", map.get("method"));
        root.put("status", "ok");
        root.put("code", "200");
        root.set("list", list);
        try {
            CommandsHelp.submit(map.get("app"), mapper.writeValueAsString(root), map.get("correlation_id"),
                    LOGGER);
        } catch (JsonGenerationException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        } catch (JsonMappingException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        }
    } catch (PSQLException e) {
        if (e.getMessage().contains("unique constraint")) {
            if (e.getMessage().contains("(name)")) {
                CommandsHelp.handleError(map.get("app"), map.get("method"), "List name already exists",
                        map.get("correlation_id"), LOGGER);
            }
        }
        if (e.getMessage().contains("value too long")) {
            CommandsHelp.handleError(map.get("app"), map.get("method"), "Too long input",
                    map.get("correlation_id"), LOGGER);
        }
        CommandsHelp.handleError(map.get("app"), map.get("method"), "List name already exists",
                map.get("correlation_id"), LOGGER);
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } catch (SQLException e) {
        CommandsHelp.handleError(map.get("app"), map.get("method"), "List name already exists",
                map.get("correlation_id"), LOGGER);
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        PostgresConnection.disconnect(null, proc, dbConn);
    }
}

From source file:edumsg.core.commands.list.CreateListCommand.java

@Override
public void execute() {

    try {/*from  ww  w.  jav  a  2  s  .co m*/
        dbConn = PostgresConnection.getDataSource().getConnection();
        dbConn.setAutoCommit(true);
        Statement query = dbConn.createStatement();
        query.setPoolable(true);

        set = query
                .executeQuery(String.format("SELECT * FROM create_list('%s','%s','%s', %s, now()::timestamp)",
                        map.get("name"), map.get("description"), map.get("session_id"), map.get("private")));

        while (set.next()) {
            details.put("list_id", set.getInt("id") + "");
            details.put("name", set.getString("name"));
            details.put("description", set.getString("description"));
            details.put("private", set.getBoolean("private") + "");
            details.put("creator_id", set.getString("creator_id"));
            details.put("created_at", set.getTimestamp("created_at") + "");
            //Cache.createList(set.getInt("id") + "", details);
        }

        set.close();

        root.put("app", map.get("app"));
        root.put("method", map.get("method"));
        root.put("status", "ok");
        root.put("code", "200");
        try {
            CommandsHelp.submit(map.get("app"), mapper.writeValueAsString(root), map.get("correlation_id"),
                    LOGGER);
        } catch (JsonGenerationException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        } catch (JsonMappingException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        }
    } catch (PSQLException e) {
        if (e.getMessage().contains("unique constraint")) {
            if (e.getMessage().contains("(name)")) {
                CommandsHelp.handleError(map.get("app"), map.get("method"), "List name already exists",
                        map.get("correlation_id"), LOGGER);
            }
        }
        if (e.getMessage().contains("value too long")) {
            CommandsHelp.handleError(map.get("app"), map.get("method"), "Too long input",
                    map.get("correlation_id"), LOGGER);
        }
        CommandsHelp.handleError(map.get("app"), map.get("method"), "List name already exists",
                map.get("correlation_id"), LOGGER);
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } catch (SQLException e) {
        CommandsHelp.handleError(map.get("app"), map.get("method"), "List name already exists",
                map.get("correlation_id"), LOGGER);
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        PostgresConnection.disconnect(null, proc, dbConn);
    }
}

From source file:edumsg.core.commands.tweet.NewTweetCommand.java

@Override
public void execute() {
    try {/*from w w  w .  ja v  a 2 s. co  m*/
        dbConn = PostgresConnection.getDataSource().getConnection();
        dbConn.setAutoCommit(true);
        Statement query = dbConn.createStatement();
        query.setPoolable(true);
        if (map.containsKey("image_url")) {

            set = query.executeQuery(String.format("SELECT * FROM create_tweet('%s','%s','%s')",
                    map.get("tweet_text"), map.get("session_id"), map.get("image_url")));
        } else {
            set = query.executeQuery(String.format("SELECT * FROM create_tweet('%s','%s')",
                    map.get("tweet_text"), map.get("session_id")));

        }
        String id = null;
        while (set.next()) {
            id = set.getInt("id") + "";
            details.put("id", id);
            details.put("tweet_text", set.getString("tweet_text"));
            details.put("creator_id", set.getInt("creator_id") + "");
            details.put("image_url", set.getString("image_url"));
            details.put("created_at", set.getTimestamp("created_at") + "");
            //Cache.cacheTweet(set.getInt("id")+"", details);
            //Cache.cacheUserTweet(map.get("creator_id"),set.getInt("id")+"");
            root.put("id", set.getInt("id"));
        }

        root.put("app", map.get("app"));
        root.put("method", map.get("method"));
        root.put("status", "ok");
        root.put("code", "200");
        set.close();

        try {
            CommandsHelp.submit(map.get("app"), mapper.writeValueAsString(root), map.get("correlation_id"),
                    LOGGER);
            String cacheEntry = UserCache.userCache.get("user_tweets:" + map.get("session_id"));
            if (cacheEntry != null) {
                JSONObject cacheEntryJson = new JSONObject(cacheEntry);
                cacheEntryJson.put("cacheStatus", "invalid");
                //                    System.out.println("invalidated");
                UserCache.userCache.set("user_tweets:" + map.get("session_id"), cacheEntryJson.toString());
            }
            String cacheEntry1 = UserCache.userCache.get("timeline:" + map.get("session_id"));
            if (cacheEntry1 != null) {
                JSONObject cacheEntryJson = new JSONObject(cacheEntry1);
                cacheEntryJson.put("cacheStatus", "invalid");
                //                    System.out.println("invalidated");
                UserCache.userCache.set("timeline:" + map.get("session_id"), cacheEntryJson.toString());
            }
            String cacheEntry2 = TweetsCache.tweetCache.get("get_earliest_replies:" + map.get("session_id"));
            if (cacheEntry2 != null) {
                JSONObject cacheEntryJson = new JSONObject(cacheEntry2);
                cacheEntryJson.put("cacheStatus", "invalid");
                //                    System.out.println("invalidated");
                TweetsCache.tweetCache.set("get_earliest_replies:" + map.get("session_id"),
                        cacheEntryJson.toString());
            }
            String cacheEntry3 = TweetsCache.tweetCache.get("get_replies:" + map.get("session_id"));
            if (cacheEntry3 != null) {
                JSONObject cacheEntryJson = new JSONObject(cacheEntry3);
                cacheEntryJson.put("cacheStatus", "invalid");
                //                    System.out.println("invalidated");
                TweetsCache.tweetCache.set("get_replies:" + map.get("session_id"), cacheEntryJson.toString());
            }
            String cacheEntry4 = ListCache.listCache.get("get_list_feeds:" + map.get("session_id"));
            if (cacheEntry4 != null) {
                JSONObject cacheEntryJson = new JSONObject(cacheEntry4);
                cacheEntryJson.put("cacheStatus", "invalid");
                //                    System.out.println("invalidated");
                ListCache.listCache.set("get_list_feeds:" + map.get("session_id"), cacheEntryJson.toString());
            }
        } catch (JsonGenerationException e) {
            //Logger.log(Level.SEVERE, e.getMessage(), e);
        } catch (JsonMappingException e) {
            //Logger.log(Level.SEVERE, e.getMessage(), e);
        } catch (IOException e) {
            //Logger.log(Level.SEVERE, e.getMessage(), e);
        }
        //            catch (JSONException e) {
        //                // TODO Auto-generated catch block
        //                e.printStackTrace();
        //            }

    } catch (PSQLException e) {
        if (e.getMessage().contains("value too long")) {
            CommandsHelp.handleError(map.get("app"), map.get("method"), "Tweet exceeds 140 characters",
                    map.get("correlation_id"), LOGGER);
        } else {
            CommandsHelp.handleError(map.get("app"), map.get("method"), e.getMessage(),
                    map.get("correlation_id"), LOGGER);
        }
        //Logger.log(Level.SEVERE, e.getMessage(), e);
    } catch (SQLException e) {
        CommandsHelp.handleError(map.get("app"), map.get("method"), e.getMessage(), map.get("correlation_id"),
                LOGGER);
        //Logger.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        PostgresConnection.disconnect(null, proc, dbConn);
    }
}