ormlite保存大量数据性能优化

时间:2021-12-24 06:09:00


原来是这样写的:

public void addList(List<AreaBean> areas) {
    try {
        for (AreaBean area : areas){
 areaDao.createOrUpdate(area);
} } catch (Exception e) { e.printStackTrace() ; }} 3000多条数据,耗时大概30秒

后来改成

try {
    DatabaseConnection conn = areaDao.startThreadConnection();
    Savepoint savePoint = conn.setSavePoint(null);
    for (AreaBean area : areas){
        areaDao.createOrUpdate(area);
    }
    conn.commit(savePoint);
    areaDao.endThreadConnection(conn);
} catch (Exception e) {
    e.printStackTrace();
}
大概1秒之内就写入了