-->
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: Problem with contraint violations
PostPosted: Sat Nov 05, 2005 3:58 pm 
Newbie

Joined: Sat Nov 05, 2005 3:38 pm
Posts: 7
Hi,

I'm having a problem when I violate a contraint. Suppose you have a DEPARTMENT table and also an EMPLOYEE table. EMPLOYEE has a foreign key referencing DEPARTAMENT (EMPLOYEE -> DEPARTMENT). You create only one department and also some employees which points to that department. Then you try to delete the department. What happens? And ADOException: could not delete etc. Nothing wrong here, I have to deal with this in my code. But even when I handle that exception and show some friendly message to the user, the same exception is raised again and again when I later try to add, alter or delete anything on the database! The same happens when I try to insert a record with an already existing primary key.

What am I missing here? :(

Here is some code that can help you figure out what's wrong. IMPORTANT NOTE: It's a WinForms app and the Session lives while the app is running (it never gets closed).


Code:
      protected void Save(object obj) {
         ITransaction transaction = PersistenceEngine.Session.BeginTransaction();
         
         try {
            Session.SaveOrUpdate(obj);
            transaction.Commit();
         } catch (Exception) {
            transaction.Rollback();
            throw;
         }
      }

      protected void Delete(object obj) {
         ITransaction transaction = PersistenceEngine.Session.BeginTransaction();
         
         try {
            Session.Delete(obj);
            transaction.Commit();
         } catch (Exception) {
            transaction.Rollback();
            throw;
         }
      }


Code:
internal class PersistenceEngine {
      private static ISessionFactory sessionFactory;
      private static ISession session;

      private PersistenceEngine() { }

      private static ISessionFactory SessionFactory {
         get {
            if (sessionFactory == null) {
               Configuration configuration = new Configuration();
         
               configuration.AddClass(typeof(Profissao));
               configuration.AddClass(typeof(Estado));
               configuration.AddClass(typeof(Municipio));
               configuration.AddClass(typeof(Bairro));
               configuration.AddClass(typeof(Paciente));
         
               sessionFactory = configuration.BuildSessionFactory();
            }
            
            return sessionFactory;
         }
      }
      
      public static ISession Session {
         get {
            if (session == null)
               session = SessionFactory.OpenSession();
            
            return session;
         }
      }
      
      public static void Close() {
         if (session != null)
            session.Close();
      }
   }



May someone help me?

Thank you,

Celio


Top
 Profile  
 
 Post subject: Re: Problem with contraint violations
PostPosted: Sat Nov 05, 2005 4:56 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
ccidral wrote:
the Session lives while the app is running (it never gets closed).


You answered your own question here. Don't do that. Sessions are meant to be short and used transactionally - if anything in a session fails, you roll back the current transaction and throw the session away. The session does not recover from exceptions.


Top
 Profile  
 
 Post subject: Re: Problem with contraint violations
PostPosted: Sun Nov 06, 2005 10:41 am 
Newbie

Joined: Sat Nov 05, 2005 3:38 pm
Posts: 7
sergey wrote:
You answered your own question here. Don't do that. Sessions are meant to be short and used transactionally - if anything in a session fails, you roll back the current transaction and throw the session away. The session does not recover from exceptions.


Thank you, I did not know it. I have read the documentation but don't remember of have read about such detail.


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.