-
Notifications
You must be signed in to change notification settings - Fork 16
Transactional and batch operations
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;
}
});- Introduction
- Goals & Features
- Kotlin
- Immutable or Mutable Pojo
- Annotation Processor Args
- Credits
- Articles
- Benchmarks
- Setup
- Tutorial
- Usage
- Dependencies and inspirations
- Stackoverflow
- Documentation
- SQL logging
- Data source options
- Indices
- SQL Type adapter
- Global SQL Type adapter
- Constraints
- Live data: welcome Architectural components!!
- Paged Live data
- Dynamic parts
- Transactional and batch operations
- Async Transactional and batch operations
- Global transaction
- Support for immutable POJO
- Generate Content provider
- Generate Database schema generation
- Database migration
- BindSqlColumn
- BindContentProvider
- BindContentProviderEntry
- BindContentProviderPath
- BindDao
- BindDaoMany2Many
- BindDataSource
- BindDataSourceOptions
- BindDataSourceUpdateTask
- BindIndex
- BindSqlRelation
- BindSqlAdapter
- BindSqlChildSelect
- BindSqlDelete
- BindSqlDynamicOrderBy
- BindSqlDynamicWhere
- BindSqlDynamicWhereParams
- BindSqlInsert
- BindSqlPageSize
- BindSqlParam
- BindSqlSelect
- BindSqlUpdate
- BindSqlType
- BindSqlTransaction