I was wondering if we can some how extend NHiberate to throw an exception if a piece of code attempted to save an object to a database without beginning a transaction? Since beginning a transaction is a requirement for calling Save() to work properly, I can’t see a programmer calling Save() without beginning a transaction in the first place.
Share
Solution is not in the exception throwing. It is about keeping us (developers on the project) aware of what and where we are doing
1) Project shared approach. Firstly, explain to the team how the Architecture of application works. All team members should now, that Unit of Work, or Session per Request patterns are in place.
2) The FlushMode. Secondly, explain the concept of the Session object.
ISession. Save()orUpdate()is far away from SQL executeINSERTorUPDATE. What count is the ISession.Flush(). And (based on the first step) we can decide when that Flush() happens.I would suggest setting the FlushMode to on
CommitorNone(with explict call). And then, if a team member would like to Execute any write command – one standard place, one common way (used on the project) will guide hin/her.The call
Session.Flush()should/will/must be wrapped in atransaction.