悬赏园豆:30
[已解决问题]
解决于 2008-08-20 13:57
<P>不知道为什么。</P>
<P>用到了NHibernate,</P>
<P>一般是这么用</P>
<DIV class=cnblogs_code><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><SPAN style="COLOR: #008080"> 1</SPAN> <SPAN style="COLOR: #000000">session </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> NHibernateHelper.GetCurrentSession();<BR></SPAN><SPAN style="COLOR: #008080"> 2</SPAN> <SPAN style="COLOR: #000000"> ITransaction tx </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> session.BeginTransaction();<BR></SPAN><SPAN style="COLOR: #008080"> 3</SPAN> <SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080"> 4</SPAN> <SPAN style="COLOR: #000000"> {<BR></SPAN><SPAN style="COLOR: #008080"> 5</SPAN> <SPAN style="COLOR: #000000"> artcileid </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">)session.Save(obj);<BR></SPAN><SPAN style="COLOR: #008080"> 6</SPAN> <SPAN style="COLOR: #000000"> tx.Commit();</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080"> 9</SPAN> <SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> artcileid;<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN> <SPAN style="COLOR: #000000"> }<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN> <SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000"> (HibernateException ex)<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN> <SPAN style="COLOR: #000000"> {<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN> <SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000"> tx) tx.Rollback();<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN> <SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">throw</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> ApplicationException(ex.Message);<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN> <SPAN style="COLOR: #000000"> }<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN> <SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">finally</SPAN><SPAN style="COLOR: #000000"> { NHibernateHelper.CloseSession(); }</SPAN></DIV>
<DIV class=cnblogs_code><SPAN style="COLOR: #000000"> </SPAN></DIV>
<DIV class=cnblogs_code><SPAN style="COLOR: #000000">也有的代码这样:</SPAN></DIV><SPAN style="COLOR: #000000">
<DIV class=cnblogs_code><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> InsertArticle(Article obj)<BR><IMG id=Codehighlighter1_54_719_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_54_719_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_54_719_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_54_719_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_54_719_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_54_719_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_54_719_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_54_719_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top> </SPAN><SPAN id=Codehighlighter1_54_719_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_54_719_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> artcileid </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> session </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> NHibernateHelper.GetCurrentSession();<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> ITransaction tx </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> session.BeginTransaction();<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_233_475_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_233_475_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_233_475_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_233_475_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_233_475_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_233_475_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_233_475_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_233_475_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top> </SPAN><SPAN id=Codehighlighter1_233_475_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_233_475_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> artcileid </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">)session.Save(obj);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> tx.Commit();<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 将tags加入Entity_Tag表</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"> TagDAL.SaveTags(obj.ID, TagEntityType.article, obj.Tags);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> artcileid;<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000"> (HibernateException ex)<BR><IMG id=Codehighlighter1_530_652_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_530_652_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_530_652_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_530_652_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_530_652_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_530_652_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_530_652_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_530_652_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top> </SPAN><SPAN id=Codehighlighter1_530_652_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_530_652_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000"> tx) tx.Rollback();<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">throw</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> ApplicationException(ex.Message);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_673_709_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_673_709_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_673_709_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_673_709_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_673_709_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_673_709_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_673_709_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_673_709_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">finally</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN id=Codehighlighter1_673_709_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_673_709_Open_Text><SPAN style="COLOR: #000000">{ NHibernateHelper.CloseSession(); }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top> }</SPAN></SPAN></DIV>
<P>SaveTags的定义:用到了SqlHelper类</P>
<DIV class=cnblogs_code><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><SPAN style="COLOR: #0000ff">internal</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> SaveTags(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> entityId, TagEntityType tet, </SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000"> tags)<BR>{<BR> SqlHelper.ExecuteNonQuery(SqlHelper.KyZhiSqlConn, CommandType.StoredProcedure, </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">ky_Tag_SaveTags</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">,<BR> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> SqlParameter(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">@EntityId</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">, entityId),<BR> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> SqlParameter(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">@EntityType</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">, (</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">)tet),<BR> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> SqlParameter(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">@Tags</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">, tags));<BR>}</SPAN></DIV>
<DIV class=cnblogs_code><SPAN style="COLOR: #000000"> </SPAN></DIV>
<DIV class=cnblogs_code><SPAN style="COLOR: #000000">我怀疑是这样的地方引发了问题。</SPAN></DIV>
<DIV class=cnblogs_code><SPAN style="COLOR: #000000"> </SPAN></DIV>
<DIV class=cnblogs_code><SPAN style="COLOR: #000000">还有很多地方只用SqlHelper。</SPAN></DIV>
<DIV class=cnblogs_code><SPAN style="COLOR: #000000"> </SPAN></DIV>
<DIV class=cnblogs_code><SPAN style="COLOR: #000000">这样同时访问的人多了,就会出现事务错误</SPAN></DIV></SPAN>