views:

93

answers:

2

I need help on this.

I got this error while inserting/updating a number of records

could not load an entity: 
[Star.CNPL_BusinessObjects.Entities.CNPL.CNPL_AgencyProduct#48][SQL: SELECT cnpl_agenc0_.Id as Id48_0_, cnpl_agenc0_.AgencyID as AgencyID48_0_, cnpl_agenc0_.ProductID as ProductID48_0_, cnpl_agenc0_.CreatedDate as CreatedD4_48_0_, cnpl_agenc0_.CreatedBy as CreatedBy48_0_, cnpl_agenc0_.UpdatedDate as UpdatedD6_48_0_, cnpl_agenc0_.UpdatedBy as UpdatedBy48_0_ FROM CNPL_AgencyProduct cnpl_agenc0_ WHERE cnpl_agenc0_.Id=?]"

Then, I get this inner exception

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData() 
   at System.Data.SqlClient.SqlDataReader.get_MetaData() 
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) 
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) 
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() 
   at NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd) 
   at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, RowSelection selection, ISessionImplementor session) 
   at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) 
   at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) 
   at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, Type optionalEntityName, Object optionalIdentifier, IEntityPersister persister)

Need help on this guys. Ideas can help. :)

A: 

You might be experiencing a deadlock issue here. Are you doing a simple Get? or are you doing other operations in a transaction that might lock it?

Mark PM
it's just a simple update on the table. I checked the mapping tables and they are just the same as in the physical tables in SQL.
Musikero31
I suggest you run SQL Profiler (seems like you are using SQL server) and find the command that is causing the timeout.
Mark PM
There's was this one command that has 1.6M seconds (I guess) under the read column and it's the proc_MSS_Crawl. I don't know if this is the one and I don't know if this counts.
Musikero31
Which profiler template did you use? You should use TSQL
Mark PM
i didn't use any template. but i saw an sql statement that, when i ran it in sql server, it was fast. was it because of the nhibernate? that sql statement was the agencyproduct
Musikero31
It might be indexing issue as when you run it via SQL it will have different connection settings. Do you have an index on the table? Try updating the stats on the table.
Mark PM
It has the same connection string with that of SQL. Is it possible that it could be because there's a missing value for the ID? This only happens upon inserting.
Musikero31
Is your ID field an identity column? The connection will have different properties (eg nocount on etc...)
Mark PM
My mistake, it happens when it updates. Yes, it is an identity column. But what's weird is that it should automatically update in nhibernate if there's an existing ID. however, it seems that there's something fishy with this.
Musikero31
Another weird thing is that it points to the correct database with an existing table. It updates/adds correctly. However, when it is the 2nd to the last record to be inserted, then it gets a timeout. Is it possible that the table gets locked during the process? If so, how can I unlock it?
Musikero31
That should be easy to check, simply run sp_who2 in SQL Server when the statement runs and check if it is blocked and by which process.
Mark PM
I ran sp_who2 and it is not blocked. I wonder what would cause the "Could Not Load an Entity" and at the same time, throw an inner exception of "Timeout Expired"?
Musikero31
You are getting "Couldn't load entity" because of the timeout.
Mark PM
So what could be the solution here if it is not blocked?
Musikero31
The only other explanation is that the indexes are fragmented and need to rebuild.
Mark PM
How do I do rebuild that?
Musikero31
Here is one way:http://technet.microsoft.com/en-us/library/ms187874.aspx
Mark PM
A: 

We found the solution to the issue.

It seems that NHibernate is having a hard time when it changes from update to insert. The problem is that when there's an insert before an update in bulk transactions, this error happens. What we did was we separated the update and the insert transactions and it worked.

I'm wondering if this is because of the NHibernate dll. Any thoughts on this?

Musikero31