SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Exception Stacktrace:
at System.Data.SqlTypes.SqlDateTime.FromTimeSpan(TimeSpan value)
at System.Data.SqlTypes.SqlDateTime.FromDateTime(DateTime value)
at System.Data.SqlClient.MetaType.FromDateTime(DateTime dateTime, Byte cb)
at System.Data.SqlClient.TdsParser.WriteValue(Object value, MetaType type, Byte scale, Int32 actualLength, Int32 encodingByteSize, Int32 offset, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc)
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.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
Friday, March 16, 2012
SqlDateTime overflow Exception with NHibernate
Thursday, July 16, 2009
Don't flush the Session after an exception occurs
So code would look like
try
{
//Use Session like never used before
}
finally
{
NhibernateSessionManager.CloseCurrentSession();
//closes session that was assigned to the thread
}
CloseCurrentSession in NhibernateSessionManager would look like this,
CloseCurrentSession()
{
ISession session = GetCurrentContextSession();
if(session != null && session.IsOpen)
{
session.Flush(); //evil is here
session.Close();
RemoveCurrentContextSession();
}
}
I was proud of this solution until I got into a database exception in my main code (which happens rarely after I write millions of lines of code).
If any such exception occurred and you try to flush, you will be delighted with another exception when you use session.Flush() It says "don't flush the Session after an exception occurs". For undisclosed reason, I couldn't use autoflush (reserved for next blog may be ;)
I believe there are alternate solutions but I as I told, I am lazy (like any other ORM), so I reverted back to my 'Flush manually' everytime when you make some entity dirty. Pretty shitty stuff huh???
Anyways, thanks for reading the blog even though the title "Don't flush the Session after an exception occurs" tells it all and inside matter is just ramblings.
Tuesday, May 12, 2009
About CProxyType*_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable2
CProxyType*ENTITY_FULL_NAME*_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable2
Are you worried? Are you banging your head? Don't worry now, I have a solution for you! Follow as described below (step by step):
Step 1. Stop Debugging. Sometime NHibernate doesn't load lazy loaded entity during debug (specially in 'watch' or 'quick watch'). Test your code at runtime like Message Pop-up box or Console.Write etc
Step 2. Take a cup of tea.
Tuesday, September 16, 2008
What NHibernate Lacks
This is expensive when I the object is not in cache at frist place. It makes 2 database hit.
What I need is something like this,
UnitOfWork.GetFresh
OR
if(UnitOfWork.IsCached
{
//Refresh
}
else
{
//Get
}
Saturday, April 12, 2008
Track down TransientObjectException
Problem:
TransientObjectException is hibernate (nhibernate in my little world) exception which occurs when you flush.
Identify the code which causes it:
It is simple. Put nhibernate session.IsDirty() underwatch. Execute your program stpe by step. You are 'ok' till IsDirty give you 'true' or 'false'. You can identify the culprit step when IsDirty raises any exception.
No one can you tell you exact solution as this may be caused by variety of mistakes. The root cause is that you are trying to save an object into database which has transient objects (not existing in database in our context) attached to it. Solution is to save the transient objects before flush or just remove the "cascade" attribute from the HBM.
Thursday, February 28, 2008
NHibernate flush
Note: This discussion is only limited to first level cache provided at ISession level in NHibernate. Flush(): Unlike its name is suggesting, purpose of Flush() is not to clear cache. It synchronizes the cached objects with the database.In short, you cache and database gets in sync with each other. All that you retrieve using Load(), Get() and List() is entitled to stay in cache. When you do 'save', 'update' or 'delete', operations of NHibernate, not all operations are immediately performed to database. It is when you use 'flush' of NHibernate session, all 'yet to be done' change of database are done. Note that 'flush' is transaction safe. Means, all the operation done in a transaction by 'flush' can be rolled back with other things of transaction.
When to use flush? When you are interested in dirty reading of data, Flush() refreshes your modified data into database (and obviously, does “undo” if the Nhibernate transaction is rolled back) .
What to do to clear cache? Use “Evict” to clear individual object or collection of objects, Isession.Clear() removes all objects.
Friday, January 11, 2008
“generator” element in .hbm file
Recently I was stuck in a peculiar problem where I couldn’t insert a new record in a table using NHibernate. It came out to be the .hbm file which was giving problem. Because our DO generators are written so well, we looked for the bug there at last.
NHibernate class generator always creates class thinking that the primary key will be ‘auto-generated’. But that table doesn’t have auto-generated primary key. And so the “generator” element was incorrectly set to ‘native’ (i.e. auto-generated) for that table.
About “generator” element:
If the value of this is set to ‘native’, the key is auto-generated. Set ‘specified’ to set the primary key yourself in code. See link below for description of all values that generator can have. Note that ‘specified’ is the default in case “generator” element is absent in hbm file.
Link: http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml