Java TransactionExt Example

说明

java transactionext示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Java

命名空间/包名称: org.apache.ojb.odmg

示例#1
文件: PerformanceODMGTest.java项目: cniesen/ojb-1.0.4

  /**
   * read in all the PerformanceArticles from the RDBMS that have been inserted by <code>
   * insertNewArticles(). The lookup is done with a cursor fetch, that is: a between
   * Statement is used to select all inserted PerformanceArticles and Objects are read in by
   * fetching from the cursor (JDBC ResultSet).
   */
  protected void readArticlesByCursor() throws Exception {
    TransactionExt tx = (TransactionExt) odmg.newTransaction();
    // we don't want implicite locks when compare performance
    tx.setImplicitLocking(false);
    tx.begin();
    // clear cache to read from DB
    tx.getBroker().clearCache();

    long start = System.currentTimeMillis();
    OQLQuery query = odmg.newOQLQuery();
    String sql =
        "select allArticles from "
            + PerformanceArticle.class.getName()
            + " where articleId between "
            + new Integer(offsetId)
            + " and "
            + new Integer(offsetId + articleCount);
    query.create(sql);
    ManageableCollection collection = (ManageableCollection) query.execute();
    Iterator iter = collection.ojbIterator();
    int fetchCount = 0;
    while (iter.hasNext()) {
      fetchCount++;
      iter.next();
    }
    long stop = System.currentTimeMillis();
    logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec");
  }

示例#2
文件: PerformanceODMGTest.java项目: cniesen/ojb-1.0.4

 /**
  * read in all the PerformanceArticles from the RDBMS that have been inserted by <code>
  * insertNewArticles(). The lookup is done one by one, that is: a primary key based lookup
  * is used.
  */
 protected void readArticles() throws Exception {
   TransactionExt tx = (TransactionExt) odmg.newTransaction();
   // we don't want implicite locks when compare performance
   tx.setImplicitLocking(false);
   String sql =
       "select allArticles from " + PerformanceArticle.class.getName() + " where articleId=$1";
   long start = System.currentTimeMillis();
   tx.begin();
   for (int i = 0; i < articleCount; i++) {
     OQLQuery query = odmg.newOQLQuery();
     query.create(sql);
     query.bind(arr[i].getArticleId());
     query.execute();
   }
   tx.commit();
   long stop = System.currentTimeMillis();
   logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
 }

展开阅读全文