-->
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.  [ 1 post ] 
Author Message
 Post subject: [Spring / Hibernate] doInHibernate uses another transaction?
PostPosted: Sat Jul 09, 2011 1:32 pm 
Newbie

Joined: Wed Sep 08, 2010 9:58 am
Posts: 6
Location: Sao Paulo | Brazil
Hi Guys,
I'm about two days trying to know what is happening with my transaction on JUnit Test using doInHibernate.

This is my code:

applicationContext.xml (just the data config)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans ... >

   <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${hibernateConnectionUrl}" />
        <property name="username" value="${hibernateConnectionUsername}" />
        <property name="password" value="${hibernateConnectionPassword}" />
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
   <property name="dataSource" ref="dataSource" />
    </bean>



    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
       
         <property name="dataSource" ref="dataSource" />
       
         <property name="annotatedClasses">
        <list>
          <value>com.model.hibernate.entidade.Loja</value>
        </list>   
         </property>

        <property name="hibernateProperties">
            <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>   
            <prop key="hibernate.show_sql">true</prop>   
            <prop key="hibernate.c3p0.minPoolSize">5</prop>
            <prop key="hibernate.c3p0.maxPoolSize">20</prop>
            <prop key="hibernate.c3p0.min_size">5</prop>
            <prop key="hibernate.c3p0.max_size">20</prop>
            <prop key="hibernate.c3p0.timeout">1800</prop>
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
            <prop key="hibernate.connection.providerClass">org.hibernate.connection.C3P0ConnectionProvider</prop>
            <prop key="hibernate.connection.charset">UTF-8</prop>
            </props>
        </property>
    </bean>   

</bean>



LojaHibernateDao and GenericoDaoHibernateImpl
Code:
@Repository("lojaDao")
public class LojaHibernateDao extends GenericoDaoHibernateImpl<Loja, Long> implements LojaDao {

   @Autowired
   public LojaHibernateDao(SessionFactory sessionFactory) {
      super(sessionFactory);
   }

   //...
}


abstract class GenericoDaoHibernateImpl<T, ID extends Serializable> extends HibernateDaoSupport implements GenericoDao<T, ID> {


   // ...

   @SuppressWarnings("unchecked")
   protected Class<T> classePersistente = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

   @Override
   public void salva(T objeto) {
      getHibernateTemplate().saveOrUpdate(objeto);
   }

   @Override
   public T get(ID id) {
      return getHibernateTemplate().get(classePersistente, id);
   }

   @Override
   public Integer executaAtualizacao(final String sQuery, final Map<String,Object> params) {
      
      return getHibernateTemplate().execute(new HibernateCallback<Integer>() {

         @Override
         public Integer doInHibernate(Session sessao) throws HibernateException,SQLException {
            
            Query query = sessao.createQuery(sQuery);
            
            if (params!=null) {
               Iterator<String> iterator = params.keySet().iterator();
               while(iterator.hasNext()) {
                  String param = iterator.next();
                  query.setParameter(param, params.get(param));
               }
            }
            
            return query.executeUpdate();
         }
      });
   }

   // ...

}



LojaDaoTest
Code:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:**beans/applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class LojaDaoTest {

   @Autowired
   LojaDao lojaDao;


   @Test
   public void pegaLojasASeremIntegradas() throws Exception {

      //IT WORKS!!
      Loja loja = new Loja("valid value1",...);
      loja.setUrl("http://hibernate.com");
      lojaDao.salva(loja);
      Loja loja2 = lojaDao.get(loja.getId());
      assertEquals("http://hibernate.com",loja2.getUrl());

      //IT DOESNT WORK!!
      String query = "update Loja set url = :url where id = :id";
      Map<String, Object> params = new HashMap<String, Object>();
      params.put("url", "http://cnn.com");
      params.put("id", loja2.getId());
      lojaDao.executaAtualizacao(query,params); //Returns the value 1
      Loja loja3 = lojaDao.get(loja2.getId());
      assertEquals("http://cnn.com",loja3.getUrl()); //THE OLD VALUE IS RETURNED
      
      }
}



Debugging the code I could notice that the session ID is the same in doInHibernate scope and in the get/salva scope.

Do you know what could have being?

Thank you.

Rondon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.