facebook

Hibernate Transaction

  1. MyEclipse IDE
  2.  > 
  3. Comments
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #215591 Reply

    I’m trying to get my DataServices layer written and I’d like to get a code review. Here’s my method for adding a record.

    
        /**
         * addDataStore() inserts new <code>DataStore</code> into the database through Hibernate.
         * 
         * @param DataStore A new <code>DataStore</code> to be added.
         */
        public void addDataStore(DataStore DataStore) throws Exception
        {
    
            //         Create a configuration based on the properties file we've put
            // in the standard place.
            Configuration config = new Configuration();
            
            // Tell it about the classes we want mapped, taking advantage of
            // the way we've named their mapping documents.
            config.addClass(DataStore.class);
            
            // Get the session factory we can use for persistence
            SessionFactory sessionFactory = config.buildSessionFactory();
            
            // Ask for a session using the JDBC information we've configured
            Session session = sessionFactory.openSession();        
            
            Transaction tx = null;
            
            try
            {
                // Begin transaction
                tx = session.beginTransaction();
                
                // Add new record
                session.save(DataStore);
                
                // Commit transaction
                tx.commit();
            }
            catch (HibernateException e)
            {
                if (tx != null) {
                        tx.rollback();
                }
                log.error("Hibernate Exception" + e.getMessage());
                throw new RuntimeException(e);
            }
            /*
             * Regardless of whether the above processing resulted in an Exception
             * or proceeded normally, we want to close the Hibernate session.  When
             * closing the session, we must allow for the possibility of a Hibernate
             * Exception.
             * 
             */
            finally
            {
                if (session != null)
                {
                    try
                    {
    
                        session.close();
                    }
                    catch (HibernateException e)
                    {
                        log.error("Hibernate Exception" + e.getMessage());
                        throw new RuntimeException(e);
                    }
    
                }
            }
    
        }
    
    

    Thanks,

    Lee

    #215611 Reply

    Riyad Kalla
    Member

    Thank you for posting this in the correct forum 😉

    #215614 Reply

    Well….once my brain unfroze and I realized it wasn’t GENERATED code 🙂

    Lee

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Hibernate Transaction

You must be logged in to post in the forum log in