I need to execute two tasks within the unit of work of NHibernate: a transactional one (saving one unit) and a non-transactal one.
Since non-transactional operation can not be rollbacked, if I save before the executing unit without a transaction operation (and finally transaction) I still get the transaction behavior: PROBLEM: With a code similar to the following, NHibernate will not execute the actual SQL Insert SQL for the call. Commit () (which internally calls session.Flush ()): With a code like this, if SQL fails to insert, then it is too late: Note: Theresignal operation is executed. To insert the actual SQL before executing NotTransactionalOperation, let me clarify the call session before the session. Fuchs () is. Save (): The code works, but ... it is considered the best practice to call the session before conducting this transaction. If not, then what are the better ways to achieve similar results? Flushing means that NHibernate will ensure that all changes are maintained for DB, this is to ensure that That all necessary SQL statements will be executed. When the transaction fails, and thus rollback is done, then all those changes will be returned. Therefore, I have no problem doing this (you should bear in mind that your organization is not in a valid state, because the transaction has failed). But ... I do not really see the problem: when the non-behavioral process fails, what's the problem? Since this process is non-pragmatic, I think there is no effect on the data that is with the database? Or, what does this code do? If this is unprofessional, why can not you do it outside your work-unit? When saving is unsuccessful, I think that you will ensure that not only the transaction is rolled out, the exception will be put on the stack, as long as it does not face the error handler. , Which means that your non-transaction code will not be executed well:
using
(var transaction = session.BeginTransaction ()) {session.Save (entity); NotTransactionalOperationBasedOn (unit); Transaction.Commit (); // True SQL insertion will be executed here}
using
(var transaction = session.BeginTransaction ()) {session.Save (unit); Session.Flush (); // The actual SQL insertion will be executed here Transaction.Commit (); }
using
(var transaction = session.BeginTransaction ()) {repository.Save (entity) ; Transaction.Commit (); // error, exception is thrown} Non-reproducible code (unit); // This line will not be executed, because the exception will be thrown the stack until the appropriate catch block is encountered.
Comments
Post a Comment