Package org.jugile.util

Examples of org.jugile.util.DBConnection


    p1.setName("foo");
    d.commit();
   
    // check db directly
    DBPool db = DBPool.getPool();
    DBConnection c = db.getConnection();
    List<List> rows = c.select("select name_f,id_f,vers from person_t where id_f="+p1.id());
    c.free();
    assertEquals(1,rows.size());
    List row = rows.get(0);
    assertEquals("foo",row.get(0));
    assertEquals(""+p1.id(),""+row.get(1));
    assertEquals("4",""+row.get(2));
View Full Code Here


  /**
   * Read current version from db immediately.
   */
  public void refresh() {
    DBPool db = DBPool.getPool();
    DBConnection c = db.getConnection();
    DomainData dd = DomainCore.cd();
    try {
      c.prepare("select "+_getSelectFlds()+" from "+table() +" where id_f=?");
      c.param(id());
      List<List> rows = c.select();
      if (rows.size() == 1) {
        List row = rows.get(0);
        // set state
        Bo o = origin;
        o._modified = row.get(1)==null?null:new Time((java.util.Date)row.get(1));
        //String host = (String)row.get(2);
        o.version = (Integer)row.get(3);
        o._setFlds(dd,bi(),row,countSelectHeaderFlds());
        // copy fields to copy
        o.copy(bi(),this);
      }
      c.commit();
    } catch (Exception e) {
      try { c.rollback(); fail(e); } catch (Exception e2) { fail(e2); }
    } finally {
      try { c.free(); } catch (Exception e2) { fail(e2); }
    }
  }
View Full Code Here

  /**
   * Write to db immediately.
   */
  public void flush() {
    DBPool db = DBPool.getPool();
    DBConnection c = db.getConnection();   
    try {
      this._dbWriteFlds(c, bi());
      c.commit();
      modified = false; // not modified anymore
      // _modified Time still remains
      incrVersion();
    } catch (Exception e) {
      try { c.rollback(); fail(e); } catch (Exception e2) { fail(e2); }
    } finally {
      try { c.free(); } catch (Exception e2) { fail(e2); }
    }   
  }
View Full Code Here

    isArchived = v;
    String a = "0";
    if (v) a = "1";
    String sql = "update " + table() + " set archived="+a+" where id_f="+getId();
    DBPool db = DBPool.getPool();
    DBConnection c = db.getConnection();
    try {
      c.update(sql);
      c.commit();
    } catch (Exception e) {
      log.error("dbread failed",e);
      try { c.rollback(); } catch (Exception e2) { fail(e2);}
    } finally {
      try { c.free(); } catch (Exception e) {}
    }   
  }
View Full Code Here

  }
 
  public static long getNextIdHiFromDb(String obj) {   
    long nid = 0;
    DBPool pool = DBPool.getPool();
    DBConnection c = pool.getConnection();
    try {
      c.writeTx();
      c.prepare("select nextid from idpool where obj=?");
      c.param(obj);
      List<List> res = c.select();
      nid = (Integer)res.get(0).get(0);
      c.prepare("update idpool set nextid=? where obj=?");
      c.param(nid+idIncrement);
      c.param(obj);
      c.execute();
      c.commit();
    } catch (Exception e) {
      try { c.rollback(); } catch (Exception e2) { } fail(e);
    } finally {
      try { c.free(); } catch (Exception e) { log.fatal("could not free connection",e); }     
    }
    return nid;
  }
View Full Code Here

 
  protected int saveAllToDB() {
    uow(); // starts readTx
    try {
      DBPool db = DBPool.getPool();
      DBConnection c = db.getConnection();
      try {
        int res = cd().saveToDB(c); // whole core domain cd()
        c.commit();
        return res;
      } catch (Exception e) {
        try { c.rollback(); fail(e); } catch (Exception e2) { fail(e2); }
      } finally {
        try { c.free(); } catch (Exception e2) { fail(e2); }
      }
    } catch (Exception e) {
      log.fatal("could not write domain to db", e);
      fail(e);
    } finally {
View Full Code Here

 
  protected abstract Class<Bo>[] classes();
 
  private int doLoadFromDB() throws Exception {
    DBPool db = DBPool.getPool();
    DBConnection c = db.getConnection();
    int count = 0;
    try {
      //reset();
      count = cd().loadFromDB(c,classes());
    } catch (Exception e) {
      log.error("dbread failed",e);
      try { c.rollback(); } catch (Exception e2) { fail(e2);}
    } finally {
      try { c.free(); } catch (Exception e) {}
    }
    return count;   
  }
View Full Code Here

    return cd().readDeltasFromQueue(node(),max);
  }

  private int modifyDbAndSendMsg(String delta) {
    DBPool db = DBPool.getPool();
    DBConnection c = db.getConnection();
    try {
      int res = uow().writeToDB(c, classes());
      DBQueue.writeMessage(delta, node(), nodes(), c);
      c.commit();
      return res;
    } catch (Exception e) {
      try { c.rollback(); } catch (Exception e2) { fail(e2); }
      log.error("could not write db changes",e);
      fail(e);
    } finally {
      try { c.free(); } catch (Exception e2) { fail(e2); }
    }
    return 0;
  }
View Full Code Here

  public final static long ERROR = 3L;

  public static void writeMessage(String msg, String node, String nodes[]) {
    if (nodes == null) return;
    DBPool pool = DBPool.getPool();
    DBConnection c = pool.getConnection();
    try {
      writeMessage(msg,node,nodes,c);
      c.commit();
    } catch (java.sql.SQLNonTransientConnectionException se) {
      log.info("connection error in write. retrying in 30 sec: " + node);
      // sleep 30 sec and retry
      try {
        Thread.sleep(30000);
      } catch (InterruptedException ie) {
        fail(ie);
      }
      log.info("retrying write now: " + node);
      writeMessage(msg,node,nodes);
    } catch (Exception e) {
      try {
        c.rollback();
      } catch (Exception e2) {
        fail(e2);
      }
      fail(e);
    } finally {
      try { c.free();
      } catch (Exception e3) {
        //fail(e3);
      }
    }
  }
View Full Code Here

    if (sql == null) sql = "where " + q2.toSql();
    sql = "select " + bo._getSelectFlds() +" from " + bo.table() + " " + sql;

    List<E> res = new ArrayList<E>();
    DBPool db = DBPool.getPool();
    DBConnection c = db.getConnection();
    try {
      c.prepare(sql);
      for (List<Object> row : c.select()) {
        long id = (Integer)row.get(0);
        E o = (E)bo.createOld(bo.getClass(), id);
        o._setState(null,bo.bi(),row);
        res.add(o);
      }
    } catch (Exception e) {
      log.error("dbread failed",e);
      try { c.rollback(); } catch (Exception e2) { fail(e2);}
    } finally {
      try { c.free(); } catch (Exception e) {}
    }
    return res;
  }
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.