Skip to content

Transactional and batch operations

xcesco edited this page May 3, 2018 · 1 revision

Every generated data-sources implementation contains code to manage transactions and batch operations. A developer does not need to open and close connection manually.

Given a data source definition:

@BindDataSource(daoSet=Bean96Dao.class, fileName = "dummy" , version=1, asyncTask=true, cursorWrapper=true)
public interface Bean96DataSource {

}

You can execute a transaction using execute method of data source:

BindBean96DataSource dataSource = BindBean96DataSource.instance();
dataSource.execute(new BindBean96DataSource.Transaction() {
			
    @Override
    public TransactionResult onExecute(BindBean96DaoFactory daoFactory) {
      Bean96DaoImpl dao = daoFactory.getBean96Dao();				
      dao.insert(bean);				
      ...
 				
      return TransactionResult.COMMIT;
    }
});

With Java source level 8 it become:

BindBean96DataSource dataSource = BindBean96DataSource.instance();
dataSource.execute((BindBean96DaoFactory daoFactory) -> {
      Bean96DaoImpl dao = daoFactory.getBean96Dao();				
      dao.insert(bean);				
      ...
 				
      return TransactionResult.COMMIT;
    }
});

The DaoFactory exposes all DAO defined in the data source. When a transaction is terminated, it is possible to commit operations returning TransactionResult.COMMIT. It is also possible to rollback operations with TransactionResult.ROLLBACK value. In generated execute method, there is all code needed to open connection, commit/rollback connection and close/release database connection.

Batch operation is a sequence of SQL commands that share the same connection. It is not a transaction. Every SQL operation is applied after its executions. The syntax to work with batch operations is similar to transation operations:

dataSource.executeBatch(new Batch<Integer>() {
  @Override
  public Integer onExecute(BindBean96DaoFactory daoFactory) {
    Bean96DaoImpl dao = daoFactory.getBean96Dao();
    dao.insert(bean);
    return (int) bean.id;
  }
});

And with lambda expressions:

dataSource.executeBatch((BindBean96DaoFactory daoFactory) -> {
    Bean96DaoImpl dao = daoFactory.getBean96Dao();
    dao.insert(bean);
    return (int) bean.id;
  }
});

Table of Contents

Query definition

Features

Relations

Multithread supports

Modularization

Annotations for data convertion

Annotations for SQLite ORM

Annotations for shared preferences

Clone this wiki locally