java sqlite 事务处理,澄清Java / SQLite批处理和自动提交

I copied the following example from an SQLite Java library website:

PreparedStatement prep =

conn.prepareStatement("insert into people values (?, ?);");

prep.setString(1, "Gandhi");

prep.setString(2, "politics");

prep.addBatch();

prep.setString(1, "Turing");

prep.setString(2, "computers");

prep.addBatch();

conn.setAutoCommit(false);

prep.executeBatch();

conn.setAutoCommit(true);

I'm struggling to understand the significance of toggling autoCommit() either side of executeBatch(). Does it merely prevent a commit being made for each individual batch operation? Thus a single 'bulk' commit is will be made by setAutoCommit(true).

解决方案

The auto commit is disabled before batch because enabling auto commit will commit (i.e. wait for sync to happen which means it will wait the data is actually written to persistent storage like hard disk) after every row that is inserted.

If auto commit is false, it will not wait for sync.

The difference in waiting for sync and not waiting is the guaranty that whether data is actually to hard disk or it is in the buffer (that could be buffered IO or buffer of hard disk).

In short, disabling auto commit gives you performance boost. And I think by default auto commit is enabled.

Another way of optimization

If you want to have auto commit ON and still need performance boost just try to start as transaction before the batch operation and commit the transaction after. This way sqlite wont auto commit after every insert and it will give good performance boost.

EDIT:

When you starting a transaction you are only disabling auto commit

for that transaction and it will be again 'on' once transaction is over. What auto commit helps is

when you are inserting/updating rows separately (not as batch), then you dont have to start a

transaction explicitly for every insert/update. And regarding setting auto-commit to true, after the

fact, does not do call for commit. If you make auto-commit

true and whatever you have already inserted/updated wont have any effect and won't have same

guaranties as auto-commit true prior to making those insert/update.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值