Summary

This chapter has covered some of the most powerful features of NHibernate that we may not need to use on daily basis.

We started the chapter with concurrency control. Concurrency control is very critical from a business point of view as it helps with preventing corruption of data due to multiple writes to the same data at the same time. NHibernate mainly provides two ways of concurrency control.

Optimistic concurrency control is a database technology agnostic. This is built entirely within NHibernate and you can use it with any database. Pessimistic concurrency control, on the other hand, makes use of database locks to make sure that only one transaction can update a database record at any point of time. Event system of NHibernate is carefully designed to make sure that enough extension points are provided for the users of NHibernate to plug in their own code at multiple different times during the commit of a transaction. What you can do with these hooks is only limited by your creativity. The most common use of these hooks is to audit entities transparently and from one place. Next, we touched upon the different types of caching implementation that NHibernate offers. Session level cache is scoped to a session and cannot be disabled. Second level cache is shared across multiple sessions created from the same session factory. Query cache is shared across multiple sessions but is applied to specific queries and works in conjunction with the second level cache. Session level cache and dirty checking make the normal session a bit slow and consume more memory. These attributes do not have significant impact when working with small number of entities at a time. But if you are loading thousands of entities in memory then the session starts slowing down with its burgeoning memory footprint. At this point, a lightweight stateless session may come in handy. Stateless sessions perform faster and use less memory at the cost of disabling a lot of features we would normally use. In the end, user defined types provide you a tool that can be used when NHibernate fails to handle conversion of SQL column type to .NET type automatically for you. User defined types is an important feature that can come in handy when dealing with legacy database situations.

Though we do not use these features on a daily basis, these features are very important. Knowledge of these features may save you hours of efforts when you need them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.183.210