如何使用Dao.setAutoCommit()? [英] How to use Dao.setAutoCommit()?

查看:488
本文介绍了如何使用Dao.setAutoCommit()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器解析JSON,使用ORMLite因为我读出来坚持对象到数据库中。这些文件可以得到非常大的,所以我的想法是因为他们正在阅读将对象提交一次,而不是读取上百个对象到内存中,并做了质量承诺(内存为智能手机稀少,毕竟)。但是,每个对象将包含多个值的集合(我试图使用 ForeignCollection 这些值,所以每个值需要被视为一个单独的对象),并认为它会更好提交的所有项目对象的集合在一次,而不是承诺,也就是说,每个字符串或整数。

我认为做一团提交的批处理,我会简单地调用 dao.setAutoCommit(假),电话 dao.create ()为每个项目,调用 dao.commit()之后,然后调用 dao.setAutoCommit(真)回去零碎的提交。

我有三个问题:


  1. 是不是犯,我走了,我还是要批应承诺 - 即使这意味着高达上千元的对象会立即得到承诺

  2. 是更好地犯之名可享集合的时间,或批处理?

  3. 我在哪里可以得到的的DatabaseConnection dao.setAutoCommit() dao.commit()要求?也许我错过了,但文档或例子,我找不到它。


解决方案

  

时的正确提交,因为我去,我还是要批应承诺 - 即使这意味着高达上千元的对象会立即得到承诺


如果您有大量的项目可能会更好一次,而不是1000犯下了几个100的对象。这在很大程度上取决于所以用各种不同批量运行它和计时他们是尝试好事对象的大小。至于你提到,你需要平衡内存和数据库的性能。我很好奇的结果,所以请你定时的数据发表评论。


  

是更好地犯之名可享集合的时间,或批处理?


确实的好做的事情在一个批处理。这只是我工作过的每个数据库也是如此。

如果你正在谈论外国然后收集真的是有对象的外国收集和要创建对象的大名单没有区别。双方将受益于批处理。

每个外部对象被创建后,我不会承诺。我宁愿批量达100(比方说)外的物体和任何相关的异物他们。然后提交了100的变化,做下100。然后改变100到200,然后尝试50,等等。你应该看到拐点。

我有没有回答这一个吧?


  

在哪里得到了价值的DatabaseConnection dao.setAutoCommit() dao.commit()要求?也许我错过了,但文档或例子,我找不到它。


编辑#2:

由于@Chad指出的,我错了采用Android下自动提交建议。通过启动一个事务,完成后提交它虽然sqlite的驱动程序通常支持自动提交。 Android的似乎并不不过来支持这一点。在Android数据库连接时,code是一个空操作。

在Android上,做批量任务,ORMLite的正确方法是使用 道。 callBatchTasks(可赎回)方法,而不是。是这样的:

  accountDao.callBatchTasks(connectionSource,
  新的可赎回<无效>(){
    公共无效()调用抛出的SQLException {
        //同时插入多个账户
        对于(账户账号:accountsToInsert){
           //更新我们的帐户对象
           accountDao.create(账户);
        }
        返回null;
    }
});

I'm parsing JSON from a server, using ORMLite to persist objects to the database as I read them out. The files could get very large, so my thought is to commit objects one at a time as they are read, instead of reading hundreds of objects into memory and doing a mass commit (memory being scarce on smartphones, after all). However, each object will contain a collection with multiple values (I am attempting to use ForeignCollection for those values, so each value needs to be treated as a separate object) and think it would be better to commit all the items in an object's collection at once, rather than committing, say, each individual String or Integer.

I believe that to make a group of commits in a batch, I would simply call dao.setAutoCommit(false), call dao.create() for each item, call dao.commit() afterwards and then call dao.setAutoCommit(true) to go back to piecemeal commits.

I have three questions:

  1. Is it right to commit as I go, or should I to a batch commit--even if it means up to a thousand objects would get committed at once?
  2. Is it better to commit the collection of items one at a time, or in a batch?
  3. Where do I get the databaseConnection value from that dao.setAutoCommit() and dao.commit() require? Maybe I missed it, but I can't find it in the documentation or examples.

解决方案

Is it right to commit as I go, or should I to a batch commit--even if it means up to a thousand objects would get committed at once?

If you have a large number of items it may be better commit a couple of 100 objects at once instead of 1000s. It depends a lot on the size of the objects so running it with various different batch sizes and timing them would be a good thing to try. As you mention, you need to balance memory and database performance. I'd be curious of the results so please comment with your timing data.

Is it better to commit the collection of items one at a time, or in a batch?

It is certainly better to do things in a batch. This is true of just about every database I've ever worked on.

If you are talking about a foreign collection then there is really no difference between a foreign collection of objects and a large list of objects that you want to create. Both will benefit from batching.

I would not commit after each outer object was created. I'd rather batch up 100 (let's say) outer objects and whatever associated foreign objects they have. Then commit the changes for the 100 and do the next 100. Then change the 100 to 200 and then try 50, etc.. You should see inflection points.

Did I answer this one right?

Where do I get the databaseConnection value from that dao.setAutoCommit() and dao.commit() require? Maybe I missed it, but I can't find it in the documentation or examples.

Edit #2:

As @Chad pointed out, I was wrong to recommend using auto-commit under Android. Although Sqlite drivers usually support auto-commit by starting a transaction and committing it when done. Android does not seem to support this however. In the Android database connection, the code is a no-op.

Under Android, the proper way to do batch tasks with ORMLite is to use the dao.callBatchTasks(Callable) method instead. Something like:

accountDao.callBatchTasks(connectionSource,
  new Callable<Void>() {
    public Void call() throws SQLException {
        // insert a number of accounts at once
        for (Account account : accountsToInsert) {
           // update our account object
           accountDao.create(account);
        }
        return null;
    }
});

这篇关于如何使用Dao.setAutoCommit()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆