These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: nhiberante without transaction.
PostPosted: Sun Apr 27, 2008 9:34 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Hello,

I use an odbc driver with NHibernate 1.2 to load my objects from .dbf files. That works perfectly, but I've a problem to save my objects to the database as the odbc driver doesn't support transactions...

So I would like to save an object (which will result in an INSERT or UPDATE) outside of a transaction.
I've read the article http://www.hibernate.org/403.html and tried to write this:

session.Save(item);
session.Close();

and in the config file:
<property name="connection.autocommit">true</property>

Unfortunately this doesn't work. Although I get no error, no row is inserted in the database...

Is there an other way to update/insert without transaction ?

Thank you in advance for any advices.

Best regards,

mathmax


Top
 Profile  
 
 Post subject: nhiberante without transaction.
PostPosted: Mon Apr 28, 2008 2:33 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi mathmax,

Try using session.Flush().

Code:
session.Save(item);
session.Flush();
session.Close();


Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 28, 2008 9:17 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Thank you, that works.

I've heard that with nhibernate 2 we will be constrained to to use a transaction each time we load or save data. Is it true ?


Top
 Profile  
 
 Post subject: nhiberante without transaction.
PostPosted: Mon Apr 28, 2008 10:24 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I am still trying to get up to speed on NH 2.0 myself.

My understanding is that there is "no auto-flush outside of a transaction" - so without a transaction NHibernate will not send your updates to the database automatically.

From playing with the trunk code it appears that you can still manually Flush() the session outside of a transaction. I'm not 100% sure it will stay that way.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 28, 2008 10:30 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Hi,

I still have a problem when trying to update a collection of object that way:

foreach (var item in list)
session.SaveOrUpdate(item);
session.Flush();
session.Close();

I get the following error message:

Unexpected row count: 0; expected: 1
" at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)\r\n at NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)\r\n at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)\r\n at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, ISessionImplementor session)\r\n at NHibernate.Impl.ScheduledUpdate.Execute()\r\n at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)\r\n at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)\r\n at NHibernate.Impl.SessionImpl.Execute()\r\n at NHibernate.Impl.SessionImpl.Flush()\r\n at NHibernateLibrary.NHibernateSessionManager.SaveAndClose[T](IEnumerable`1 en) in D:\\Mes documents\\Visual Studio 2008\\Projects\\NHibernate\\NHibernateSessionManager.cs:line 429\r\n at ConsoleApplication2.Program.Main(String[] args) in C:\\Documents and Settings\\MAA\\Bureau\\ConsoleApplication2\\ConsoleApplication2\\Program.cs:line 25\r\n at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ThreadHelper.ThreadStart()"

Do you have an idea of the problem?


Top
 Profile  
 
 Post subject: nhiberante without transaction.
PostPosted: Mon Apr 28, 2008 10:54 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

It's hard to tell, but it might be related to how NHibernate is identifying an unsaved object (e.g., when the Id property is '0'). If NHibernate thinks the Id is already saved it will issue an update instead of an insert, and fail if no record was found.

It might help to post your mapping/config/etc.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 28, 2008 11:12 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<nhibernate>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.GenericDialect"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.OdbcDriver"
/>
<add
key="hibernate.connection.connection_string"
value="filedsn=Q:\Osoft.dsn;"
/>
<add
key="hibernate.show_sql"
value="true"
/>
<add
key="connection.autocommit"
value="true"
/>
</nhibernate>
</configuration>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="ConsoleApplication2" assembly="ConsoleApplication2">
<class name="Circuit" lazy="false">
<id name="CIRCIRC">
<generator class="assigned" />
</id>
<property name="CIRJODE" />
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 28, 2008 11:15 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
What is the type of 'CIRCIRC'?

Are you calling SaveOrUpdate on an instance with CIRCIRC set to a value that doesn't correspond to a row in the database?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.