-->
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.  [ 3 posts ] 
Author Message
 Post subject: Unexpected Behavior with Session management and Transactions
PostPosted: Fri Oct 05, 2007 12:36 pm 
Newbie

Joined: Fri Oct 05, 2007 12:29 pm
Posts: 1
I've written the test below and I'm very confussed by the results. It appears that if I make a change to a a hibernated class outside of a transaction and then create a transaction modify a different object and save this object both updates are sent a saved to the database. I would have expected the Address.Address1 property to not get persisted. Is there something wrong with this code or my configuration. This is an issue when we are using the OpenSessionInView model for our ASP.net application, just because an object has changed we don't want to persist it unless saveorUpdate is called explicitly in a transaction for that particular object.

[Test]
public void Test2()
{
Configuration cfg = new Configuration();
cfg.Configure("hibernate.cfg.xml");

ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
session.FlushMode = FlushMode.Commit;

Address address = session.Load<Address>(3);
address.Address1 = "" + DateTime.Now.ToLongTimeString();

Customer customer = session.Load<Customer>(2);
ITransaction transaction = session.Transaction;
transaction.Begin();
customer.IsActive = false;
session.SaveOrUpdate(customer);
transaction.Commit();
}


hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test">
<!-- properties -->
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=ssnwserver3;Database=X;User ID=XWebAdmin;Password=XWebAdmin;Min Pool Size=10;Max Pool Size=50;Pooling=true;Connection Lifetime=60;Trusted_Connection=False;</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.connection.isolation">Serializable</property>
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping assembly="HibernateTests" />
</session-factory>

</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 1:23 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Actually, transactions don't quite work that way.

Basically, if you have any object attached to a session and it's state changes then those changes will be persisted to the database when a flush occurs.

Normally flushing occurs on an as-needed basis; this could be when you commit a transaction or it could be when you execute a query to ensure that the query results match the data in memory.

You've said explicitly that you only want changes to be flushed on commit of a transaction, but that doesn't mean that changes made to persistent objects outside the transaction won't be tracked - they'll still be flushed when the next flush occurs unless you evict them from the session.

The transaction you've created ensures that all the SQL statements that are executed are all handled in a database transaction. This allows you to make sure that all actions against the database can be atomic for the NHibernate transaction, although the actual database transaction will not start until the first action against the database happens (ie when the first flush occurs).

Does that make sense?

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 1:23 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Your expectations are wrong. That is indeed how NHibernate works and kind of the whole point - you shouldn't need to tell NHibernate explicitly what to do, it figures it out on its own. Read the documentation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.