NHibernate and TooManyRowsAffectedException(转载)
2010-05-18 11:55
295 查看
Today I received a log of an error in a windows service that runs some scheduled actions, the error was TooManyRowsAffectedException and it is thrown by nhibernate when a session is flushed. The exact message said Unexpected row count: 2; expected: 1 , after a little moment of confusion I immediately realize that the problem is due to a trigger.
I modified a table to keep a log of all modifications of every row, and I did this with a simple trigger that, for each update, saves the last value of each modified row in an history table. Since the trigger runs in the same context of the update operation, when you update a row, the trigger fired, insert a row in history table and the whole update operation returns 2 as the number of affected rows. NHibernate check the affected row count to verify that the row was really updated by the UPDATE statement, when he saw a rowcount of 2 he throws exception because something was wrong since it issue an update of a single row.
The obvious solution is to issue a SET NOCOUNT ON at the beginning of the trigger and a SET NOCOUNT OFF at the end. This is a good practice, because in this way the trigger does not affect the number of affected rows. After all the trigger should be completely transparent to the application, and this is the best way to achieve this result.
From:http://www.codewrecks.com/blog/index.php/2009/03/25/nhibernate-and-toomanyrowsaffectedexception/
I modified a table to keep a log of all modifications of every row, and I did this with a simple trigger that, for each update, saves the last value of each modified row in an history table. Since the trigger runs in the same context of the update operation, when you update a row, the trigger fired, insert a row in history table and the whole update operation returns 2 as the number of affected rows. NHibernate check the affected row count to verify that the row was really updated by the UPDATE statement, when he saw a rowcount of 2 he throws exception because something was wrong since it issue an update of a single row.
The obvious solution is to issue a SET NOCOUNT ON at the beginning of the trigger and a SET NOCOUNT OFF at the end. This is a good practice, because in this way the trigger does not affect the number of affected rows. After all the trigger should be completely transparent to the application, and this is the best way to achieve this result.
From:http://www.codewrecks.com/blog/index.php/2009/03/25/nhibernate-and-toomanyrowsaffectedexception/
相关文章推荐
- BatchedTooManyRowsAffectedException Batch update returned unexpected row count异常问题
- org.hibernate.jdbc.BatchedTooManyRowsAffectedException
- JedisClusterMaxRedirectionsException: Too many Cluster redirections
- pl/sql异常之 too_many_rows小例子
- java.net.SocketException: Too many open files问题分析及解决方案
- java.net.SocketException: Too many open files解决方法
- org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned b
- JedisClusterMaxRedirectionsException: Too many Cluster redirections?
- lucene: IO/FileNotFoundException:(Too many open files) 查询异常解决
- java.net.SocketException: Too many open files
- Treating HTML like XML using HtmlAgilityPack, and doing it inside of an XSLT too [转载]
- java.io.FileNotFoundException: open failed: EMFILE (Too many open files)
- ValueError: output parameter for reduction operation logical_and has too many dimensions ?
- 解决lucene范围搜索中的TooManyClauses exception
- MongoDB numa系列问题一:[initandlisten] connection refused because too many open connections:
- Caused by: org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be
- java.lang.RuntimeException: java.sql.SQLException: Too many parameters: expected 0, was given 1 Quer
- 求助!! android 上使用mina 出现java.net.SocketException: Too many open files
- Mybatis TooManyResultsException
- java.net.SocketException: Too many open files解决方法