Enable things in Android ormlite

Source: Internet
Author: User
Tags savepoint

I used ormlite for ORM mapping. It seems very useful and I have been using it in the project. It is good to encapsulate the omnipotent Dao interface when the data volume is small. It's a pleasure for me to use it .. Good days are always short, and the server-side database suddenly adds 800 pieces of data !! The Dao. createorupdate method is a tragedy. It takes 40 s !! It's really intolerable !! Then it is natural to think of things to enable ormlite, but find one on the Internet, where it is difficult to use it, and the words should look at the source code better.

Found:

public Savepoint setSavePoint(String name) throws SQLException {try {db.beginTransaction();logger.trace("{}: save-point set with name {}", this, name);return new OurSavePoint(name);} catch (android.database.SQLException e) {throw SqlExceptionUtil.create("problems beginning transaction " + name, e);}}

See:

db.beginTransaction();

Is what I want.

Next we will see:

public void commit(Savepoint savepoint) throws SQLException {try {db.setTransactionSuccessful();db.endTransaction();if (savepoint == null) {logger.trace("{}: transaction is successfuly ended", this);} else {logger.trace("{}: transaction {} is successfuly ended", this, savepoint.getSavepointName());}} catch (android.database.SQLException e) {throw SqlExceptionUtil.create("problems commiting transaction " + savepoint.getSavepointName(), e);}}

public void rollback(Savepoint savepoint) throws SQLException {try {// no setTransactionSuccessful() means it is a rollbackdb.endTransaction();if (savepoint == null) {logger.trace("{}: transaction is ended, unsuccessfuly", this);} else {logger.trace("{}: transaction {} is ended, unsuccessfuly", this, savepoint.getSavepointName());}} catch (android.database.SQLException e) {throw SqlExceptionUtil.create("problems rolling back transaction " + savepoint.getSavepointName(), e);}

When we see this write method, the problem is basically solved.

AndroidDatabaseConnection connection = null;String pointName = "pointName";Savepoint savepoint = null;try{DebugTool.info ( "@@@@@@@@@@@@@@@@@@@@updateFooodTable= " + CommonUtil.getCurrentTime () );// // beging transaction connection = new AndroidDatabaseConnection ( BaseApplication.SQLiteAccess ().getHelper ( BaseApplication.mAppContext ).getWritableDatabase () , true );//connection.setAutoCommit ( false );dao.setAutoCommit ( connection , false );//dao.startThreadConnection (); savepoint = connection.setSavePoint ( pointName );for ( Food food : foodList ){try{dao.createOrUpdate ( food );}catch ( SQLException e ){DebugTool.info ( "<!=============updateFooodTable===========SQLException============>" );}catch ( Exception e ){DebugTool.info ( "<!=============updateFooodTable===========Exception================>" );}} ///foodcookmethods//connection.commit ( null );//dao.commit ( connection );connection.commit ( savepoint );DebugTool.info ( "@@@@@@@@@@@@@@@@@@@@updateFooodTable= " + CommonUtil.getCurrentTime () );}catch ( SQLException e ){DebugTool.error ( "<!===updateFooodTable====>" ,e);try{connection.rollback ( savepoint );}catch ( SQLException e1 ){DebugTool.error ( "<!===updateFooodTable++rollback=SQLException===>" ,e1);}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.