AUTO COMMIT TRANASACTION SEMANTIC //
DaoManager daoManager = DaoManagerBuilder.buildDaoManager(reader); PersonDao personDao = daoManager.getDao(PersonDao.class); // A transaction will be automatically started committed and ended // by calling any method on the DAO. The following insert and update // are TWO separate transactions. personDao.insertPerson (person); // Starts transaction person.setLastName("Begin"); personDao.updatePerson (person); // Starts a new transaction
// // PROGRAMMATIC DEMARCATION OF TRANSACTION SCOPE //
DaoManager daoManager = DaoManagerBuilder.buildDaoManager(reader); PersonDao personDao = daoManager.getDao(PersonDao.class); try { // Calling startTransaction() tells the DAO Manager that you // are going to be managing the transactions manually. daoManager.startTransaction(); personDao.insertPerson (person); person.setLastName("Begin"); personDao.updatePerson (person); // Commit all active transactions in all contexts daoManager.commitTransaction(); } finally { // End all active transactions in all contexts and rollback if necessary. daoManager.endTransaction(); }
Important: In order to achieve global transaction behaviour (i.e. two phase commit), you'll need to configure all of your contexts using JTA, JNDI and XA compliant DataSources.