title: Hibernate and Spring DataSourceTransactionManager
body: <p>I'm trying to work with Hibernate and Spring DataSourceTransactionManager to handle commit and rollback functions, but probably I dind't get something.</p>
  
  <p>Before using Spring DataSourceTransactionManager, this was one of my DAO class</p>
  
  <pre><code>  package com.springgestioneerrori.DAO;
  
    public class UtenteDAO extends DAOBase{   
  
         public void salvaUtente(Utente utenteIn) throws DAOException{ 
  
              Session session = getHibernateSession(); //from this method  I get Hibernate SessionFactory
                try{   
                    session.beginTransaction();             
                    session.saveOrUpdate(Object); 
                    session.getTransaction().commit();    
                    }
                catch(Exception e){
                    session.getTransaction().rollback()
                }
          }
  }
  </code></pre>
  
  <p>This is the class that give me the sessionFactory</p>
  
  <pre><code> private static final SessionFactory sessionFactory = buildSessionFactory();
  
              private static SessionFactory buildSessionFactory() {
                  try {
                      // Create the SessionFactory from hibernate.cfg.xml
                      return new AnnotationConfiguration().configure().buildSessionFactory();  
                  }
                  catch (Throwable ex) {
                                System.err.println("Initial SessionFactory creation failed." + ex);
                      throw new ExceptionInInitializerError(ex);
                  }
              }
  
              public static SessionFactory getSessionFactory() {
                  return sessionFactory;
              }
  
              public static void shutdown() {             
                  getSessionFactory().close();
              }
  
  public Session getHibernateSession (){
      Session session = HibernateUtil.getSessionFactory().openSession();
      return session;
  }   
  </code></pre>
  
  <p>Now i'm trying to use DataSourceTransactionManager in a declarative way. Following some examples on the internet I wrote this:</p>
  
  <p>
           
           
                    </p>
  
  <pre><code> &lt;tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" /&gt;        
  
  &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;
      &lt;tx:attributes&gt;
          &lt;tx:method name="salvaUtente"/&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  
  &lt;aop:config&gt;
      &lt;aop:pointcut id="userDaoTxPointcut" expression="execution(* com.springgestioneerrori.DAO.UtenteDAO.salvaUtente(..))" /&gt;
      &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="userDaoTxPointcut" /&gt;
  &lt;/aop:config&gt;       
  </code></pre>
  
  <p>Now, what should I write inside the method salvaUtente() to perform many inserts, for example like these</p>
  
  <pre><code>  session.saveOrUpdate(User); 
    session.saveOrUpdate(UserCredentials); 
    session.saveOrUpdate(UserOtherDetails); 
  </code></pre>
  
  <p>and making Spring handle commint and rollback? </p>