Package org.jugile.util

Examples of org.jugile.util.DBConnection


    }     
  }

  public static List<Msg> getMessages(String node, int max) {
    DBPool pool = DBPool.getPool();
    DBConnection c = pool.getConnection();
    List<Msg> msgs = new ArrayList<Msg>();
    try {
      String sql ="";
      sql += "select msg_id, nodeid, status, ts from dbmq_queue_t ";
      sql += "where nodeid=? AND status=? ORDER BY ts LIMIT ?";
      c.prepare(sql);
      c.param(node);
      c.param(NEW);
      c.param(max);
      List<Long> ids = new ArrayList<Long>();
      for (List row : c.select()) {
        long msg_id = (Integer)row.get(0);
        ids.add(msg_id);
      }
      if (ids.size() == 0) return msgs;
     
      // get messages
      sql = "select msg, nodeid, id_f, ts from dbmq_messages_t ";
      sql += "where id_f IN ("+qmarks(ids.size())+")";
      c.prepare(sql);
      for (Long id : ids) { c.param(id); }
      for (List<Object> row : c.select()) {
        String msg = (String)row.get(0);
        String nodeid = (String)row.get(1);
        long id = (Integer)row.get(2);
        Time ts = new Time((java.util.Date)row.get(3));
        Msg m = new Msg(msg);
        m.id = id;
        m.ts = ts;
        m.nodeid = nodeid;
        msgs.add(m);
      }
      c.rollback();
     
      // mark all status in progress     
      sql = "update dbmq_queue_t set status=? ";
      sql += "where nodeid=? AND status=? AND msg_id IN ("+qmarks(ids.size())+")";
      c.prepare(sql);
      c.param(INPROGRESS);
      c.param(node);
      c.param(NEW);
      for (Long id : ids) { c.param(id); }
      int count = c.execute();
      if (count != ids.size()) fail("inconsistent status write: " + count + "/"+ ids.size());
      c.commit();
      log.debug("read messages from :" + node + " size: " + count);
      return msgs;
    } catch (java.sql.SQLNonTransientConnectionException se) {
      log.info("connection error in read. retrying in 30 sec: " + node);
      // sleep 30 sec and retry
      try {
        Thread.sleep(30000);
      } catch (InterruptedException ie) {
        fail(ie);
      }
      log.info("retrying read now: " + node);
      return getMessages(node,max);
    } catch (Exception e) {
      try {
        c.rollback();
      } catch (Exception e2) {
        fail(e2);
      }
      fail(e);
    } finally {
      try {
        c.free();
      } catch (Exception e3) {
        //fail(e3);
      }
    }
    return msgs;
View Full Code Here


    return msgs;
  }
 
  public static int flush(String name) {
    DBPool pool = DBPool.getPool();
    DBConnection c = pool.getConnection();
    try {
      String sql = "delete from dbmq_queue_t where nodeid=?";
      c.prepare(sql);
      c.param(name);
      int res = c.execute();
      c.commit();
      return res;
    } catch (java.sql.SQLNonTransientConnectionException se) {
      fail("connection error in flush: " + name);
    } catch (Exception e) {
      try { c.rollback()} catch (Exception e2) { fail(e2);}
      fail(e);
    } finally {
      try { c.free(); } catch (Exception e3) { }
    }
    return 0;
  }
View Full Code Here

    return 0;
  }

  public static int flushNotNew(String name) {
    DBPool pool = DBPool.getPool();
    DBConnection c = pool.getConnection();
    try {
      String sql = "delete from dbmq_queue_t where nodeid=? and status!=0";
      c.prepare(sql);
      c.param(name);
      int res = c.execute();
      c.commit();
      return res;
    } catch (java.sql.SQLNonTransientConnectionException se) {
      fail("connection error in flush: " + name);
    } catch (Exception e) {
      try { c.rollback()} catch (Exception e2) { fail(e2);}
      fail(e);
    } finally {
      try { c.free(); } catch (Exception e3) { }
    }
    return 0;
  }
View Full Code Here

public class DBTest extends JugileTestCase {

  public static void clearDatabase() throws Exception {
    DBPool db = DBPool.getPool();
    DBConnection c = db.getConnection();
    c.updateN("delete from person_t");
    c.updateN("delete from family_t");
    c.updateN("delete from map_person_2friend_family");
    c.updateN("delete from dbmq_queue_t");
    c.updateN("delete from dbmq_messages_t");
    c.updateN("update idpool set nextid=100 where obj=\"global\"");
    HiLo.setNextid(1);
    c.commit()
  }
View Full Code Here

TOP

Related Classes of org.jugile.util.DBConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.