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

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

本文整理了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

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

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

  1. public void saveCards(final List<CardInfo> newCards, long newContentVersion) {
  2. try {
  3. cardDao.callBatchTasks(() -> {
  4. // add
  5. if (newCards != null && newCards.size() > 0) {
  6. logger.info("DB: need to add " + newCards.size() + " new cards");
  7. try {
  8. for (CardInfo card : newCards) {
  9. cardDao.create(card);
  10. if (classNames != null) {
  11. classNames.add(card.getClassName());
  12. }
  13. }
  14. } catch (SQLException ex) {
  15. Logger.getLogger(CardRepository.class).error("Error adding cards to DB - ", ex);
  16. }
  17. }
  18. // no card updates
  19. return null;
  20. });
  21. setContentVersion(newContentVersion);
  22. eventSource.fireRepositoryDbUpdated();
  23. } catch (Exception ex) {
  24. //
  25. }
  26. }

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

  1. @Override
  2. public Void call() throws Exception {
  3. dao.callBatchTasks(new Callable<Void>() {
  4. @Override
  5. public Void call() throws Exception {
  6. dao.delete(foo);
  7. return null;
  8. }
  9. });
  10. return null;
  11. }
  12. });

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

  1. /**
  2. * @see Dao#callBatchTasks(Callable)
  3. */
  4. @Override
  5. public <CT> CT callBatchTasks(Callable<CT> callable) {
  6. try {
  7. return dao.callBatchTasks(callable);
  8. } catch (Exception e) {
  9. logMessage(e, "callBatchTasks threw exception on: " + callable);
  10. throw new RuntimeException(e);
  11. }
  12. }

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

  1. /**
  2. * @see Dao#callBatchTasks(Callable)
  3. */
  4. @Override
  5. public <CT> CT callBatchTasks(Callable<CT> callable) {
  6. try {
  7. return dao.callBatchTasks(callable);
  8. } catch (Exception e) {
  9. logMessage(e, "callBatchTasks threw exception on: " + callable);
  10. throw new RuntimeException(e);
  11. }
  12. }

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

  1. @Override
  2. public void updateAll(final Collection objectsCollection) {
  3. try {
  4. dao.callBatchTasks(new Callable() {
  5. @Override
  6. public T call() throws Exception {
  7. for (Object object : objectsCollection) {
  8. update(object);
  9. }
  10. notifyObservers(OBSERVE_KEY);
  11. return null;
  12. }
  13. });
  14. } catch (Exception e) {
  15. ErrorUtils.logError(TAG, "updateAll(Collection) - " + e.getMessage());
  16. }
  17. }

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

  1. @Override
  2. public void createOrUpdateAll(final Collection objectsCollection) {
  3. try {
  4. dao.callBatchTasks(new Callable() {
  5. @Override
  6. public T call() throws Exception {
  7. for (Object object : objectsCollection) {
  8. createOrUpdate(object, false);
  9. }
  10. notifyObservers(null, CREATE_OR_UPDATE_ALL_ACTION);
  11. return null;
  12. }
  13. });
  14. } catch (Exception e) {
  15. ErrorUtils.logError(TAG, "createOrUpdateAll(Collection) - " + e.getMessage());
  16. }
  17. }

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

  1. @Override
  2. public void createOrUpdateAll(final Collection objectsCollection) {
  3. try {
  4. dao.callBatchTasks(new Callable() {
  5. @Override
  6. public T call() throws Exception {
  7. for (Object object : objectsCollection) {
  8. createOrUpdate(object);
  9. }
  10. notifyObservers(OBSERVE_KEY);
  11. return null;
  12. }
  13. });
  14. } catch (Exception e) {
  15. ErrorUtils.logError(TAG, "createOrUpdateAll(Collection) - " + e.getMessage());
  16. }
  17. }

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

  1. @Override
  2. public void updateAll(final Collection objectsCollection) {
  3. try {
  4. dao.callBatchTasks(new Callable() {
  5. @Override
  6. public T call() throws Exception {
  7. for (Object object : objectsCollection) {
  8. update(object, false);
  9. }
  10. notifyObservers(null, UPDATE_ALL_ACTION);
  11. return null;
  12. }
  13. });
  14. } catch (Exception e) {
  15. ErrorUtils.logError(TAG, "updateAll(Collection) - " + e.getMessage());
  16. }
  17. }

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

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

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

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

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

  1. @Test
  2. public void testDeleteIds() throws Exception {
  3. final Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
  4. final List<Integer> fooIdList = new ArrayList<Integer>();
  5. fooDao.callBatchTasks(new Callable<Void>() {
  6. @Override
  7. public Void call() throws Exception {
  8. for (int i = 0; i < 100; i++) {
  9. Foo foo = new Foo();
  10. assertEquals(1, fooDao.create(foo));
  11. fooIdList.add(foo.id);
  12. }
  13. return null;
  14. }
  15. });
  16. assertEquals(fooIdList.size(), fooDao.deleteIds(fooIdList));
  17. assertEquals(0, fooDao.queryForAll().size());
  18. }

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

  1. @Test
  2. public void testCallBatch() throws Exception {
  3. final Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. final Foo foo1 = new Foo();
  5. assertEquals(0, dao.queryForAll().size());
  6. // this should be none
  7. dao.callBatchTasks(new Callable<Void>() {
  8. @Override
  9. public Void call() throws Exception {
  10. assertEquals(1, dao.create(foo1));
  11. return null;
  12. }
  13. });
  14. assertEquals(1, dao.queryForAll().size());
  15. }

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

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

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

  1. @Test
  2. public void testIteratePageSize() throws Exception {
  3. final Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
  4. int numItems = 1000;
  5. fooDao.callBatchTasks(new InsertCallable(numItems, fooDao));
  6. // now delete them with the iterator to test page-size
  7. CloseableIterator<Foo> iterator = fooDao.iterator();
  8. try {
  9. while (iterator.hasNext()) {
  10. iterator.next();
  11. iterator.remove();
  12. }
  13. } finally {
  14. iterator.close();
  15. }
  16. }

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

  1. @Test
  2. public void testCallBatchTasksCommitted() throws Exception {
  3. final Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. final Foo foo1 = new Foo();
  5. DatabaseConnection conn = dao.startThreadConnection();
  6. try {
  7. dao.callBatchTasks(new Callable<Void>() {
  8. @Override
  9. public Void call() throws Exception {
  10. assertEquals(1, dao.create(foo1));
  11. assertNotNull(dao.queryForId(foo1.id));
  12. return null;
  13. }
  14. });
  15. dao.rollBack(conn);
  16. assertNotNull(dao.queryForId(foo1.id));
  17. } finally {
  18. dao.endThreadConnection(conn);
  19. }
  20. }

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

  1. @Test
  2. public void testIteratorPreparedQuery() throws Exception {
  3. final Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
  4. // do an insert of bunch of items
  5. final int numItems = 100;
  6. fooDao.callBatchTasks(new InsertCallable(numItems, fooDao));
  7. int lastX = 10;
  8. PreparedQuery<Foo> preparedQuery =
  9. fooDao.queryBuilder().where().ge(Foo.VAL_FIELD_NAME, numItems - lastX).prepare();
  10. // now delete them with the iterator to test page-size
  11. CloseableIterator<Foo> iterator = fooDao.iterator(preparedQuery);
  12. try {
  13. int itemC = 0;
  14. while (iterator.hasNext()) {
  15. iterator.next();
  16. itemC++;
  17. }
  18. assertEquals(lastX, itemC);
  19. } finally {
  20. iterator.close();
  21. }
  22. }

相关文章