When mapping a property of an enumeration type you should always set the type attribute of that property in the mapping file to that enumaration type.
Often you don't set the type at all or you set it to Int32 since your enum is represented as an int in the database. It's easy to assume that setting the attribute type to Int32 will have the same effect as using the enum type. And it will work!
When using an In32 your code will execute without exception. The field will be selected and updated as usual.
However, during the session flush nhibernate will recognize that the current value of the property differs from the originall value causing an update to the database. Another disadvantage is that the query cache for this class won't work either.
onsdag 30 januari 2008
tisdag 22 januari 2008
Using Fiddler on localhost
Fiddler is a great tool to analyse http traffic to the server. But it's sometimes difficult to make it work on localhost. But if you enter a simple dot (.) after "localhost" in the url it works.
So intead of http://localhost:8081/...,
use http://localhost.:8081/...
Reference:
http://weblogs.asp.net/lorenh/archive/2008/01/10/tip-for-using-fiddler-on-localhost.aspx
So intead of http://localhost:8081/...,
use http://localhost.:8081/...
Reference:
http://weblogs.asp.net/lorenh/archive/2008/01/10/tip-for-using-fiddler-on-localhost.aspx
NHibernate: Enable second level cache using MemCached
The second level cache is a powerful feature used by NHibernate to cache objects and queries to limit the number of database queries.
NHibernate uses a provider based architecture where cache providers can be plugged in using the config file.
NHibernate is shipped with several cache providers, example:
- SysCache (uses ASP.NET Cache Objects to cache data)
- MemCached (distributed in-memory Cache running as a separate Windows Service)
We will use MemCached in this example.
There are several win32 ports of MemCached available. We will use one available at:
http://jehiah.cz/projects/memcached-win32/
Start by downloading MemCached from the URL above and from a Command prompt type:
memcached -d install <enter>
memcached -d start <enter>
This will start the MemCached service on your computer.
Next, we will configure MemCached in your application. Add a new configuration settings in your config file under <configSettings>:
<section name="memcache" type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" />
Add the MemCached settings using:
<memcache>
<memcached host="192.168.1.0" port="11211" weight="1"/>
</memcache>
The next step is to enable caching in your NHiberate project. Under the <NHibernate> node, add the following application settings:
<add key="hibernate.cache.provider_class" value="NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache" />
<add key="hibernate.cache.use_query_cache" value="true"/>
<add key="expiration" value="120" />
At last we need to enable caching for the persistant classes in your application. This is done in the hbm.xml file using the <cache> element:
<class name="NameSpace.Class, NameSpace" table="tblInDb">
<cache usage="read-write"/>
...
</class>
To enable caching for relations, add the <cache> element for each relation:
<bag name="Childs" lazy="true">
<cache usage="read-write"/>
<key column="ParentId"/>
<one-to-many class="NameSpace.ChildClass, NameSpace"/>
</bag>
The second level cache is used by NHibernate in the following scenarios:
- Single objects retrieved using ISession.Load
- Lists of objects retrieved using IQuery.List (if hibernate.cache.use_query_cache is set to true)
- Relational objects (when the bag is marked with <cache>)
Note that NHibernate does not use the second level cache when using ICriteria.
NHibernate uses a provider based architecture where cache providers can be plugged in using the config file.
NHibernate is shipped with several cache providers, example:
- SysCache (uses ASP.NET Cache Objects to cache data)
- MemCached (distributed in-memory Cache running as a separate Windows Service)
We will use MemCached in this example.
There are several win32 ports of MemCached available. We will use one available at:
http://jehiah.cz/projects/memcached-win32/
Start by downloading MemCached from the URL above and from a Command prompt type:
memcached -d install <enter>
memcached -d start <enter>
This will start the MemCached service on your computer.
Next, we will configure MemCached in your application. Add a new configuration settings in your config file under <configSettings>:
<section name="memcache" type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" />
Add the MemCached settings using:
<memcache>
<memcached host="192.168.1.0" port="11211" weight="1"/>
</memcache>
The next step is to enable caching in your NHiberate project. Under the <NHibernate> node, add the following application settings:
<add key="hibernate.cache.provider_class" value="NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache" />
<add key="hibernate.cache.use_query_cache" value="true"/>
<add key="expiration" value="120" />
At last we need to enable caching for the persistant classes in your application. This is done in the hbm.xml file using the <cache> element:
<class name="NameSpace.Class, NameSpace" table="tblInDb">
<cache usage="read-write"/>
...
</class>
To enable caching for relations, add the <cache> element for each relation:
<bag name="Childs" lazy="true">
<cache usage="read-write"/>
<key column="ParentId"/>
<one-to-many class="NameSpace.ChildClass, NameSpace"/>
</bag>
The second level cache is used by NHibernate in the following scenarios:
- Single objects retrieved using ISession.Load
- Lists of objects retrieved using IQuery.List (if hibernate.cache.use_query_cache is set to true)
- Relational objects (when the bag is marked with <cache>)
Note that NHibernate does not use the second level cache when using ICriteria.
torsdag 29 november 2007
DataContractSerializer requires alphabetic element order
When using the DataContractSerializer to deserialize objects either from pure XML or implicit using WCF it requires the XML elements to appear in alphatic order.
Let says we have the following class:
[DataContract]public class auth
{
[DataMember]
public string username;
[DataMember]
public string password;
}
...and the following XML input:
<auth>
<username>ABC</username>
<password>DEF</password>
</auth>
Using this XML, the DataContractSerializer would deserialize the "Auth" class and Username property correctly - but the Password property would be null.
The reason is that the DataContractSerializer requires the element order in the XML to be in alphabetic order, ie:
<auth>
<password>DEF</password>
<username>ABC</username>
</auth>
The sort order can be set explicitly using the "Order" property on the "DataMember" attribute - however: it would still require the XML input to follow a specific order.
Example using the "Order" property and allowing the username element to appear before the password element:
[DataContract]
public class auth
{
[DataMember(Order=1)]
public string username;
[DataMember(Order=2)]
public string password;
}
Let says we have the following class:
[DataContract]public class auth
{
[DataMember]
public string username;
[DataMember]
public string password;
}
...and the following XML input:
<auth>
<username>ABC</username>
<password>DEF</password>
</auth>
Using this XML, the DataContractSerializer would deserialize the "Auth" class and Username property correctly - but the Password property would be null.
The reason is that the DataContractSerializer requires the element order in the XML to be in alphabetic order, ie:
<auth>
<password>DEF</password>
<username>ABC</username>
</auth>
The sort order can be set explicitly using the "Order" property on the "DataMember" attribute - however: it would still require the XML input to follow a specific order.
Example using the "Order" property and allowing the username element to appear before the password element:
[DataContract]
public class auth
{
[DataMember(Order=1)]
public string username;
[DataMember(Order=2)]
public string password;
}
WCF: Adding a behavior extension requires careful notation of the assembly path
When registering a behavior extension in the .config file the inialization might fail with the following error message:
An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element 'myService' cannot be added to this element. Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.Parameter name: element
This might be the case even if the syntax for the assembly/class is correct and even if the service host has access to the assembly.
The reason is that for this particular configuration, .NET requires you to:
1. Add the fully qualified name for the assembly
Example:
"MyAssembly.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
instead of:
"MyAssembly.MyClass, MyAssembly" - which normally does the job
2. Use the exact syntax for the assembly
You are not allowed to use any line break of white spaces at the wrong places.
Example:
"MyAssembly.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
instead of:
"MyAssembly.MyClass,MyAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" - note the missing white spaces between each attribute.
Microsoft has acknowledged this; yet not classified it as a bug:
https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=216431
An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element 'myService' cannot be added to this element. Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.Parameter name: element
This might be the case even if the syntax for the assembly/class is correct and even if the service host has access to the assembly.
The reason is that for this particular configuration, .NET requires you to:
1. Add the fully qualified name for the assembly
Example:
"MyAssembly.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
instead of:
"MyAssembly.MyClass, MyAssembly" - which normally does the job
2. Use the exact syntax for the assembly
You are not allowed to use any line break of white spaces at the wrong places.
Example:
"MyAssembly.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
instead of:
"MyAssembly.MyClass,MyAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" - note the missing white spaces between each attribute.
Microsoft has acknowledged this; yet not classified it as a bug:
https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=216431
torsdag 18 oktober 2007
ASP.NET Cache gets flushed, cleared or randomly disappear
Have you been having problems with the ASP.NET cache gets cleared, flushed or simply seems to disappear when running your ASP.NET 2.0 web application? Restarting the IIS seems to help for a while but eventually the cache gets flushed again.
This problem seem to appear especially computers with 2GB of ram or less.
The solution is quite simple, and should be good enough on a developing machine. Simply add the following lines in web.config in the .
1: <caching>
2: <cache disableMemoryCollection = "true"
3: disableExpiration = "false"
4: privateBytesLimit = "0"
5: percentagePhysicalMemoryUsedLimit = "90"
6: privateBytesPollTime = "00:02:00"/>
7: </caching>
There are a few tools that might be helpful when debugging cache.
- Visual Studio 2005 Cache Visualizer (note the changes you need to do the code in the comments)
- WebDevHelper (you need to add the httpModule to web.config and the nstuff.WebDevHelper.Server.dll as reference.)
Reference:
Thanks to Henrik for good google skills.
tisdag 16 oktober 2007
SQLQueryStress
I want to recommend a good tool when you are optimizing your queries.
Often after a trace you will find that only a couple of your queries ends up taking the majority of the cpu.
Sometimes it's as easy to solve the problems by just adding an index, and sometimes there will be other solutions.
Whatever solution you choose, it can be a good thing to test your query.
The SQLQueryStress tool written by Adam Machanic, can be downloaded here http://www.datamanipulation.net/SQLQueryStress/.
SQLQueryStress is very simple although very powerful. You can use parameter substitution so you can execute your queries with different parameters. You can set the number of iterations but also the number of threads (clients).
The output from this is "cpu seconds", "elapsed time", "client seconds", "logical reads", and more.
So when you compare two queries and booth queries takes the same time to execute, you can easily see in this program if one of them use less cpu or maybe less IO over several executions and with different parameters.
Often after a trace you will find that only a couple of your queries ends up taking the majority of the cpu.
Sometimes it's as easy to solve the problems by just adding an index, and sometimes there will be other solutions.
Whatever solution you choose, it can be a good thing to test your query.
The SQLQueryStress tool written by Adam Machanic, can be downloaded here http://www.datamanipulation.net/SQLQueryStress/.
SQLQueryStress is very simple although very powerful. You can use parameter substitution so you can execute your queries with different parameters. You can set the number of iterations but also the number of threads (clients).
The output from this is "cpu seconds", "elapsed time", "client seconds", "logical reads", and more.
So when you compare two queries and booth queries takes the same time to execute, you can easily see in this program if one of them use less cpu or maybe less IO over several executions and with different parameters.
Prenumerera på:
Inlägg (Atom)