Android ormlite 开启事物

8 篇文章 0 订阅
2 篇文章 0 订阅


用了 ormlite来做orm mapping,感觉挺好用的,就一直在项目中用了,在数据量小的时候挺好的万能的Dao接口的封装。让我用起来那是一个爽呀。。好日子总是的短暂的,服务器端的数据库一下子增加了800条数据!!用dao.createOrUpdate方法就比较的悲剧了,要执行40s!!实在是不能忍受了!!然后就很自然的想到要开启ormlite的事物了,可是上网上以找, 找到一个,那里用发现不好使,话说还是要看源码比较的好。

找到了:

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);
		}
	}

看到:

db.beginTransaction();
是我想要的东西。

接下来还有:

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 rollback
			db.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);
		}

看到这个写个方法,问题就基本上解决了。


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);
					}
				}



  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值