NHibernate sql queries not actually taking effect on the database
I'm having some weird issue here that I'm having trouble debugging. This app was previously working fine, but since changing the nhibernate configuration/sessoin management I've somehow broken it, and can't workout where.
The web pages and their post backs all seem to work ok, changes are saved to the database and what not. The transaction/sessions are handled by a HTTPModule I have made:
public class NHibernateSessionModule: IHttpModule
{
private INHibernateRequest _nhibRequest;
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginTransaction);
context.EndRequest += new EventHandler(CommitAndCloseSession);
Console.WriteLine("Init Nhibernate module");
}
/// <summary>
/// Opens a session within a transaction at the beginning of the HTTP request.
/// This doesn't actually open a connection to the database until needed.
/// </summary>
private void BeginTransaction(object sender, EventArgs e)
{
_nhibRequest = new NHibernateRequest(NHibernateSessionManager.Instance);
_nhibRequest.Start();
}
/// <summary>
/// Commits and closes
/// </summary>
private void CommitAndCloseSession(object sender, EventArgs e)
{
_nhibRequest.Commit();
}
public void Dispose() { }
}
This INhibernateRequest is a custom DLl of mine, all it does is:
public class NHibernateRequest : INHibernateRequest
{
private ITransaction _currentTransaction;
private SessionFunctions _sessionFunctions;
public NHibernateRequest(SessionFunctions sessionFunctions)
{
_sessionFunctions = sessionFunctions;
}
/// <summary>
/// Starts a session and transaction
/// </summary>
public void Start()
{
_sessionFunctions.OpenSession();
_currentTransaction = _sessionFunctions.CurrentSession.Transaction;
_currentTransaction.Begin();
}
/// <summary>
/// Closes a session and transaction
/// </summary>
public void Commit()
{
try
{
if (_currentTransaction.IsActive)
{
_currentTransaction.Commit();
}
}
finally
{
_sessionFunctions.CloseSession();
}
}
}
Basic session management. This all seems to work fine for .aspx files (web forms). But I've got a web service that is called, which creates a new entity. My domain model is focused around 'forms' and 'questions', this web service creates a new form with some questions.
I thought perhaps this web service wasn't firing the handler so I put in the session stuff manually (yes this is a bit dodgey but its only temporary to test)
[WebMethod]
public bool CreateFormForProject(string jobNumber)
{
INHibernateRequest _nhibRequest = new NHibernateRequest(NHibernateSessionManager.Instance);
_nhibRequest.Start();
try
{
ServiceLayer.FormsService.CreatePmqccFormForJob(jobNumber);
_nhibRequest.Commit();
return true;
} catch
{
_nhibRequest.Commit();
return false;
}
}
The web service always returns true so there is no exceptions being thrown.
So then I tried to make a button on a web page which makes this call then shows the results. This seemed to create it, as it showed the result, but when I refresh the page the entity is no longer there, and it was never in the database, so I guess it must have just cached it for that request.
So I turned on SQL output, and configured log4net to spit it out to the debugging output window. This is what happens:
NHibernate.SQL: 17:45:59,466 DEBUG SQL:0 - SELECT project0_.ProjectNo as ProjectNo0_0_, project0_.Name1 as Name2_0_0_, project0_.Name2 as Name3_0_0_, project0_.Project_State as Project4_0_0_, project0_.ProjectLeader as ProjectL5_0_0_, project0_1_.AdminArchived as AdminArc2_2_0_ FROM infobase.dbo.Projects project0_ left outer join infobase.dbo.Project_Archiving project0_1_ on project0_.ProjectNo=project0_1_.ProjectNo WHERE project0_.ProjectNo=@p0;@p0 = '11904' [Type: String (4000)]
NHibernate.SQL: 17:45:59,692 DEBUG SQL:0 - UPDATE infobase.dbo.Projects SET Name1 = @p0, Name2 = @p1, Project_State = @p2, ProjectLeader = @p3 WHERE ProjectNo = @p4;@p0 = 'REVIT TO ACAD PREPARE AND EXPORT TOOL' [Type: String (4000)], @p1 = 'BIM SOLUTIONS API ADDIN' [Type: String (4000)], @p2 = Active [Type: Int32 (0)], @p3 = 187 [Type: Int32 (0)], @p4 = '11904' [Type: String (4000)]
NHibernate.SQL: 17:46:02,652 DEBUG SQL:0 - INSERT INTO Forms (LastSaved, Note, Complete, MeetingId, SuggestedDeferralMonth, NextReviewDate, LastReviewDate, SuggestedProjectState, SuggestedProjectLeader, ProjectId) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9); select SCOPE_IDENTITY();@p0 = 30/06/2011 5:46:02 PM [Type: DateTime (0)], @p1 = NULL [Type: String (4000)], @p2 = False [Type: Boolean (0)], @p3 = 0 [Type: Int32 (0)], @p4 = NULL [Type: Int32 (0)], @p5 = 30/06/2011 5:45:59 PM [Type: DateTime (0)], @p6 = NULL [Type: DateTime (0)], @p7 = NULL [Type: Int32 (0)], @p8 = NULL [Type: Int32 (0)], @p9 = '11904' [Type: String (4000)]
NHibernate.SQL: 17:46:02,735 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, 'PmqccDomain.DomainObjects.PiAlertQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = Unanswered [Type: Int32 (0)]
NHibernate.SQL: 17:46:02,826 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, 'PmqccDomain.DomainObjects.ClientHappyQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = Unanswered [Type: Int32 (0)]
NHibernate.SQL: 17:46:02,928 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, 'PmqccDomain.DomainObjects.ReworkQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = Unanswered [Type: Int32 (0)]
NHibernate.SQL: 17:46:03,028 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, 'PmqccDomain.DomainObjects.ScopeOfWorksQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = Unanswered [Type: Int32 (0)]
NHibernate.SQL: 17:46:03,130 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, 'PmqccDomain.DomainObjects.TeamMeetingQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = Unanswered [Type: Int32 (0)]
NHibernate.SQL: 17:46:03,227 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, 'PmqccDomain.DomainObjects.DeadlinesQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = Unanswered [Type: Int32 (0)]
NHibernate.SQL: 17:46:03,347 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, QuestionType) VALUES (@p0, @p1, 'PmqccDomain开发者_如何学JAVA.DomainObjects.JobVelocityQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)]
NHibernate.SQL: 17:46:03,433 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InvoiceAmount, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, @p3, 'PmqccDomain.DomainObjects.InvoiceAmountQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = NULL [Type: Double (0)], @p3 = Unanswered [Type: Int32 (0)]
NHibernate.SQL: 17:46:03,534 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, 'PmqccDomain.DomainObjects.InvoiceForecastQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = Unanswered [Type: Int32 (0)]
NHibernate.SQL: 17:46:03,630 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, 'PmqccDomain.DomainObjects.ContactedClientQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = NotRequired [Type: Int32 (0)]
NHibernate.SQL: 17:46:03,734 DEBUG SQL:0 - INSERT INTO Questions (Explanation, PmqccFormId, InputtedAnswer, QuestionType) VALUES (@p0, @p1, @p2, 'PmqccDomain.DomainObjects.AsConsQuestion'); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = 8141 [Type: Int32 (0)], @p2 = NotRequired [Type: Int32 (0)]
This is exactly what is expected, but the database doesn't change!!! The SQL must get rolled back or something. I tried setting a breakpoint after the _nhibRequest.Commit() call, and it said the transaction was committed.
So now I'm stumped, how can I debug this? or can anyone see something fundamental I'm doing wrong here?
EDIT: Here is a paste of a full log4net debug output from when I'm making the web service request. http://pastebin.com/mEYWiCsF
I've finally worked it out. It was actually to do with this SO question: NHIbernate affecting non-nhibernate queries?
My hack to get around that was to commit and reopen the transaction, that was causing problems here. I've instead changed it to do a Flush, and it seems to work fine, though I'd love to find a proper solution for that issue.
精彩评论