com.j256.ormlite.dao.Dao.callBatchTasks()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(171)

本文整理了Java中com.j256.ormlite.dao.Dao.callBatchTasks()方法的一些代码示例,展示了Dao.callBatchTasks()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Dao.callBatchTasks()方法的具体详情如下:
包路径:com.j256.ormlite.dao.Dao
类名称:Dao
方法名:callBatchTasks

Dao.callBatchTasks介绍

[英]Call the call-able that will perform a number of batch tasks. This is for performance when you want to run a number of database operations at once -- maybe loading data from a file. This will turn off what databases call "auto-commit" mode, run the call-able, and then re-enable "auto-commit". If auto-commit is not supported then a transaction will be used instead.

NOTE: If neither auto-commit nor transactions are supported by the database type then this may just call the callable. Also, "commit()" is not called on the connection at all. If "auto-commit" is disabled then this will leave it off and nothing will have been persisted.

NOTE: Depending on your underlying database implementation and whether or not you are working with a single database connection, this may synchronize internally to ensure that there are not race-conditions around the transactions on the single connection. Android (for example) will synchronize. Also, you may also need to synchronize calls to here and calls to #setAutoCommit(DatabaseConnection,boolean).
[中]调用将执行许多批处理任务的Call-able。当您希望同时运行多个数据库操作时(可能是从文件加载数据),这是为了提高性能。这将关闭数据库所称的“自动提交”模式,运行可调用,然后重新启用“自动提交”。如果不支持自动提交,则将使用事务。
注意:如果数据库类型既不支持自动提交也不支持事务,那么这可能只是调用可调用的。此外,根本不会对连接调用“commit()”。如果“自动提交”被禁用,那么这将使其处于禁用状态,并且不会保留任何内容。
注意:根据您的基础数据库实现以及您是否使用单个数据库连接,这可能会在内部进行同步,以确保单个连接上的事务不存在争用条件。Android(例如)将同步。此外,您可能还需要同步对here的调用和对#setAutoCommit(DatabaseConnection,boolean)的调用。

代码示例

代码示例来源:origin: magefree/mage

public void saveSets(final List<ExpansionInfo> newSets, final List<ExpansionInfo> updatedSets, long newContentVersion) {
  try {
    expansionDao.callBatchTasks(() -> {

代码示例来源:origin: magefree/mage

public void saveCards(final List<CardInfo> newCards, long newContentVersion) {
  try {
    cardDao.callBatchTasks(() -> {
      // add
      if (newCards != null && newCards.size() > 0) {
        logger.info("DB: need to add " + newCards.size() + " new cards");
        try {
          for (CardInfo card : newCards) {
            cardDao.create(card);
            if (classNames != null) {
              classNames.add(card.getClassName());
            }
          }
        } catch (SQLException ex) {
          Logger.getLogger(CardRepository.class).error("Error adding cards to DB - ", ex);
        }
      }
      // no card updates
      return null;
    });
    setContentVersion(newContentVersion);
    eventSource.fireRepositoryDbUpdated();
  } catch (Exception ex) {
    //
  }
}

代码示例来源:origin: j256/ormlite-core

@Override
  public Void call() throws Exception {
    dao.callBatchTasks(new Callable<Void>() {
      @Override
      public Void call() throws Exception {
        dao.delete(foo);
        return null;
      }
    });
    return null;
  }
});

代码示例来源:origin: j256/ormlite-core

/**
 * @see Dao#callBatchTasks(Callable)
 */
@Override
public <CT> CT callBatchTasks(Callable<CT> callable) {
  try {
    return dao.callBatchTasks(callable);
  } catch (Exception e) {
    logMessage(e, "callBatchTasks threw exception on: " + callable);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: com.j256.ormlite/ormlite-core

/**
 * @see Dao#callBatchTasks(Callable)
 */
@Override
public <CT> CT callBatchTasks(Callable<CT> callable) {
  try {
    return dao.callBatchTasks(callable);
  } catch (Exception e) {
    logMessage(e, "callBatchTasks threw exception on: " + callable);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: QuickBlox/q-municate-android

@Override
public void updateAll(final Collection objectsCollection) {
  try {
    dao.callBatchTasks(new Callable() {
      @Override
      public T call() throws Exception {
        for (Object object : objectsCollection) {
          update(object);
        }
        notifyObservers(OBSERVE_KEY);
        return null;
      }
    });
  } catch (Exception e) {
    ErrorUtils.logError(TAG, "updateAll(Collection) - " + e.getMessage());
  }
}

代码示例来源:origin: QuickBlox/q-municate-android

@Override
public void createOrUpdateAll(final Collection objectsCollection) {
  try {
    dao.callBatchTasks(new Callable() {
      @Override
      public T call() throws Exception {
        for (Object object : objectsCollection) {
          createOrUpdate(object, false);
        }
        notifyObservers(null, CREATE_OR_UPDATE_ALL_ACTION);
        return null;
      }
    });
  } catch (Exception e) {
    ErrorUtils.logError(TAG, "createOrUpdateAll(Collection) - " + e.getMessage());
  }
}

代码示例来源:origin: QuickBlox/q-municate-android

@Override
public void createOrUpdateAll(final Collection objectsCollection) {
  try {
    dao.callBatchTasks(new Callable() {
      @Override
      public T call() throws Exception {
        for (Object object : objectsCollection) {
          createOrUpdate(object);
        }
        notifyObservers(OBSERVE_KEY);
        return null;
      }
    });
  } catch (Exception e) {
    ErrorUtils.logError(TAG, "createOrUpdateAll(Collection) - " + e.getMessage());
  }
}

代码示例来源:origin: QuickBlox/q-municate-android

@Override
public void updateAll(final Collection objectsCollection) {
  try {
    dao.callBatchTasks(new Callable() {
      @Override
      public T call() throws Exception {
        for (Object object : objectsCollection) {
          update(object, false);
        }
        notifyObservers(null, UPDATE_ALL_ACTION);
        return null;
      }
    });
  } catch (Exception e) {
    ErrorUtils.logError(TAG, "updateAll(Collection) - " + e.getMessage());
  }
}

代码示例来源:origin: j256/ormlite-core

@Test(expected = RuntimeException.class)
public void testCallBatchTasksThrow() throws Exception {
  @SuppressWarnings("unchecked")
  Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
  RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
  expect(dao.callBatchTasks(null)).andThrow(new SQLException("Testing catch"));
  replay(dao);
  rtDao.callBatchTasks(null);
  verify(dao);
}

代码示例来源:origin: j256/ormlite-core

@Test(expected = Exception.class)
public void testCallBatchThrow() throws Exception {
  final Dao<Foo, Integer> dao = createDao(Foo.class, true);
  assertEquals(0, dao.queryForAll().size());
  // this should be none
  dao.callBatchTasks(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      throw new Exception("for the hell of it");
    }
  });
}

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

@Test
public void testDeleteIds() throws Exception {
  final Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
  final List<Integer> fooIdList = new ArrayList<Integer>();
  fooDao.callBatchTasks(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      for (int i = 0; i < 100; i++) {
        Foo foo = new Foo();
        assertEquals(1, fooDao.create(foo));
        fooIdList.add(foo.id);
      }
      return null;
    }
  });
  assertEquals(fooIdList.size(), fooDao.deleteIds(fooIdList));
  assertEquals(0, fooDao.queryForAll().size());
}

代码示例来源:origin: j256/ormlite-core

@Test
public void testCallBatch() throws Exception {
  final Dao<Foo, Integer> dao = createDao(Foo.class, true);
  final Foo foo1 = new Foo();
  assertEquals(0, dao.queryForAll().size());
  // this should be none
  dao.callBatchTasks(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      assertEquals(1, dao.create(foo1));
      return null;
    }
  });
  assertEquals(1, dao.queryForAll().size());
}

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

final int numPerPage = 10;
final List<Foo> foos = new ArrayList<Foo>();
dao.callBatchTasks(new Callable<Void>() {
  @Override
  public Void call() throws Exception {

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

@Test
public void testIteratePageSize() throws Exception {
  final Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
  int numItems = 1000;
  fooDao.callBatchTasks(new InsertCallable(numItems, fooDao));
  // now delete them with the iterator to test page-size
  CloseableIterator<Foo> iterator = fooDao.iterator();
  try {
    while (iterator.hasNext()) {
      iterator.next();
      iterator.remove();
    }
  } finally {
    iterator.close();
  }
}

代码示例来源:origin: j256/ormlite-core

@Test
public void testCallBatchTasksCommitted() throws Exception {
  final Dao<Foo, Integer> dao = createDao(Foo.class, true);
  final Foo foo1 = new Foo();
  DatabaseConnection conn = dao.startThreadConnection();
  try {
    dao.callBatchTasks(new Callable<Void>() {
      @Override
      public Void call() throws Exception {
        assertEquals(1, dao.create(foo1));
        assertNotNull(dao.queryForId(foo1.id));
        return null;
      }
    });
    dao.rollBack(conn);
    assertNotNull(dao.queryForId(foo1.id));
  } finally {
    dao.endThreadConnection(conn);
  }
}

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

@Test
public void testIteratorPreparedQuery() throws Exception {
  final Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
  // do an insert of bunch of items
  final int numItems = 100;
  fooDao.callBatchTasks(new InsertCallable(numItems, fooDao));
  int lastX = 10;
  PreparedQuery<Foo> preparedQuery =
      fooDao.queryBuilder().where().ge(Foo.VAL_FIELD_NAME, numItems - lastX).prepare();
  // now delete them with the iterator to test page-size
  CloseableIterator<Foo> iterator = fooDao.iterator(preparedQuery);
  try {
    int itemC = 0;
    while (iterator.hasNext()) {
      iterator.next();
      itemC++;
    }
    assertEquals(lastX, itemC);
  } finally {
    iterator.close();
  }
}

相关文章