-->
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.  [ 2 posts ] 
Author Message
 Post subject: using-statements in NHibernate with sessions and transaction
PostPosted: Sun Nov 12, 2006 7:13 am 
Beginner
Beginner

Joined: Thu Oct 19, 2006 1:03 pm
Posts: 29
Can you use a using-statement with sessions and transactions that will close the session and roll back the transaction if exception is thrown?

Also, isn't this a better way of writing the code?

Like so:
Code:
using (ISession session= _SessionFactory.OpenSession())
{
   //here are some checks, some other code and some stuff as well...
   //loading persistent classes
   using (ITransaction transaction = session.BeginTransaction())
   {
      try
      {
         //Create some new objects/change properties of the objects
         session.Update(myobject);
         transaction.Commit();
      }
      catch
      {
         transaction.Rollback();
      }
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 1:02 pm 
Beginner
Beginner

Joined: Thu Oct 19, 2006 1:03 pm
Posts: 29
Another design question:
This code:
Code:
AspNetApplication app = null;
try
{
   //Because I can't directely check for tables with NHibernate - this will throw an error if the table doesn't exist.
   app = GetApplication(_SessionFactory.OpenSession(), this.ApplicationName, false);
}
catch
{
   try
   {
      DatabaseUtility du = new DatabaseUtility();
      du.GenerateSchema(cfg);
   }
   catch (Exception e) //what type of exception is it really??
   {
      throw new ProviderException("Unable to generate database.", e);
   }
}
finally
{
   if (app != null)
      app = null;
}


Isn't that really ugly? What should I do instead?? I'd like to check whether a table structure is already in place, and if not generate it - should I rather go with the native SQL here? Another way?

And why doesn't NHib. use these sort of source code comments?
http://msdn.microsoft.com/msdnmag/issues/02/06/XMLC/

They would really make it easier to know, for example, what type of exceptions a specific method throws, and would be rather easy to insert into already present source code. It would also help when being a beginner with the framework (as I am).

You'd also get a generated xml file from which you can then use to build a better documentation...

I don't wanna come off wrong, though, as I think you're doing a great job, with or without comments/table-checks.


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