Results 1 to 6 of 6

Thread: my DAOs don't commit !

  1. #1
    Join Date
    Dec 2005
    Location
    paris
    Posts
    13

    Default my DAOs don't commit !

    I use spring 1.2.6 with hsqldb 1.7.3 and hibernate 3.
    i have a simple DAO that extends HibernateDaoSupport and uses HibernateTemplate. When i perfom a simple insert of a row in the database, the operation is not commited.
    if i do it trough a main that uses simple hibernate tx.begin and tx.commit everything goes right.

    Here is my DAO code:
    Code:
    public class UniteDao extends HibernateDaoSupport implements UniteDAOIF {
    	
    	protected static ResourceBundle bundle = ResourceBundle.getBundle("coach.dao.impl.hibernate.message");
    
    	public Long create(Object unite) throws CoachDaoException {
    		if (!(unite instanceof UniteIF)) throw new CoachDaoException(bundle.getString("dao.badtype"));
    		try {
    			Long res;
    			res = (Long)getHibernateTemplate().save(unite);
    			return res;
    		} catch (Exception e) {
    			throw new CoachDaoException(bundle.getString("dao.hibernate.error"), e);
    		}
    	}
    }
    }
    i have the following spring configuration file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="gestionnaireTache"
    		class="coach.service.impl.basic.GestionnaireTache">
    	</bean>
    	<bean id="gestionnaireRessource"
    		class="coach.service.impl.basic.GestionnaireRessource">
    	</bean>
    	<bean id="coachFactory"
    		class="coach.model.impl.basic.ModelBasicFactory">
    		<property name="gestionnaireRessource">
    			<ref bean="gestionnaireRessource" />
    		</property>
    		<property name="gestionnaireTache">
    			<ref bean="gestionnaireTache" />
    		</property>
    	</bean>
    
    	<!-- Session factory pour Hibernate -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="mappingResources">
    			<list>
    				<value>coach/model/impl/basic/Unite.hbm.xml</value>
    				<value>
    					coach/model/impl/basic/Description.hbm.xml
    				</value>
    			</list>
    		</property>
    	</bean>
    
    	<!-- Gestionnaire de transaction pour Hibernate -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<!-- Interceptor AOP pour hibernate -->
    	<bean id="hibernateInterceptor"
    		class="org.springframework.orm.hibernate3.HibernateInterceptor">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<!-- DAO pour la gestion des Unites -->
    	<bean id="uniteDaoTarget"
    		class="coach.dao.impl.hibernate.UniteDao">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<!-- Proxy genere par Spring pour la gestion des transactions -->
    	<bean id="uniteDaoProxy"
    		class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="proxyInterfaces">
    			<value>coach.dao.UniteDAOIF</value>
    		</property>
    		<property name="interceptorNames">
    			<list>
    				<value>hibernateInterceptor</value>
    				<value>uniteDaoTarget</value>
    			</list>
    		</property>
    	</bean>
    
    </beans>
    i have a configuration file for hibernate in a propertie file
    Code:
    hibernate.show_sql=true
    hibernate.max_fetch_depth=1
    hibernate.hbm2ddl.auto=update
    hibernate.connection.driver_class=org.hsqldb.jdbcDriver
    hibernate.connection.url=jdbc:hsqldb:hsql://localhost/coach
    hibernate.connection.username=sa
    hibernate.connection.password=
    hibernate.dialect=org.hibernate.dialect.HSQLDialect

    i have a unit test that checks the creation of an object unite in the database:
    Code:
    public class UniteDaoTest extends TestCase {
    
    	HibernateTemplate hibernateTemplate;
    	ModelBasicFactory mvf;
    	XmlBeanFactory xbf;
    	UniteDAOIF uniteDao;
    
    	protected void setUp() throws Exception {
    		super.setUp();
    		ClassPathResource res = new ClassPathResource("coach/applicationContext.xml");
    		xbf = new XmlBeanFactory(res);
    		mvf = (ModelBasicFactory)xbf.getBean("coachFactory");
    		uniteDao = (UniteDAOIF)xbf.getBean("uniteDaoTarget");
    	}
    	
    	protected void tearDown() throws Exception {
    		super.tearDown();
    		xbf.destroySingletons();
    	}	
    
    	public void testUniteCreate() throws Throwable {
    		//UniteDao uniteDao = new UniteDao();
    		//uniteDao.setHibernateTemplate(hibernateTemplate);
    		UniteIF unJourHomme = mvf.getJourHomme();
    		uniteDao.create(unJourHomme);
    	}
    }
    I'm sure i'm missing something trivial, but i don't know what. Should i perform tx commit in my DAO ?

  2. #2
    Join Date
    Dec 2005
    Location
    Philadelphia, PA, USA
    Posts
    228

    Default

    Take a look in Spring documentation for 6.7. Concise proxy definitions section.

    It looks like your configuration is little off the mark.

  3. #3
    Join Date
    Dec 2005
    Location
    paris
    Posts
    13

    Default Thanks for the quick reply, still KO

    I updated my configuration file:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="gestionnaireTache"
    		class="coach.service.impl.basic.GestionnaireTache">
    	</bean>
    	<bean id="gestionnaireRessource"
    		class="coach.service.impl.basic.GestionnaireRessource">
    	</bean>
    	<bean id="coachFactory"
    		class="coach.model.impl.basic.ModelBasicFactory">
    		<property name="gestionnaireRessource">
    			<ref bean="gestionnaireRessource" />
    		</property>
    		<property name="gestionnaireTache">
    			<ref bean="gestionnaireTache" />
    		</property>
    	</bean>
    
    	<!-- Session factory pour Hibernate -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="mappingResources">
    			<list>
    				<value>coach/model/impl/basic/Unite.hbm.xml</value>
    				<value>
    					coach/model/impl/basic/Description.hbm.xml
    				</value>
    			</list>
    		</property>
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>coach/model/impl/basic</value>
    			</list>
    		</property>
    	</bean>
    
    	<!-- Gestionnaire de transaction pour Hibernate -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<!-- DAO pour la gestion des Unites -->
    	<bean id="uniteDaoTarget"
    		class="coach.dao.impl.hibernate.UniteDao">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="txProxyTemplate" abstract="true"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="transactionManager" />
    		<property name="transactionAttributes">
    			<props>
    				<prop key="*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="uniteDaoProxy" parent="txProxyTemplate">
    		<property name="target">
    			<bean class="coach.dao.impl.hibernate.UniteDao"></bean>
    		</property>
    	</bean>
    </beans>

    Exactly the same thing happen: the operation do not commit. In debug mode, after the call of the save method of the DAO, i can clearly see the new row in my database, but after exit of the method, the row disapear.
    auto commit is not set. If i change my hibernate.properties and i set hibernate.connection.autocommit=true, then the line is commited and everything is OK !! why , i don't want to set autocommit=true ...
    Last edited by dsissoko; Dec 12th, 2005 at 02:36 PM.

  4. #4
    Join Date
    Dec 2005
    Location
    Philadelphia, PA, USA
    Posts
    228

    Default

    Two things - you do not have a data source defined for your HibernateSessionFactory to work with.

    Second:
    Code:
    <bean id="uniteDaoProxy" parent="txProxyTemplate">
    		<property name="target">
    			<bean class="coach.dao.impl.hibernate.UniteDao"></bean>
    		</property>
    	</bean>
    should be:
    Code:
    <bean id="uniteDaoProxy" parent="txProxyTemplate">
    		<property name="target">
    			<ref bean="uniteDaoTarget"></bean>
    		</property>
    	</bean>
    I think this should work.

  5. #5
    Join Date
    Dec 2005
    Location
    paris
    Posts
    13

    Smile

    Quote Originally Posted by dsklyut
    Two things - you do not have a data source defined for your HibernateSessionFactory to work with.

    Second:
    Code:
    <bean id="uniteDaoProxy" parent="txProxyTemplate">
    		<property name="target">
    			<bean class="coach.dao.impl.hibernate.UniteDao"></bean>
    		</property>
    	</bean>
    should be:
    Code:
    <bean id="uniteDaoProxy" parent="txProxyTemplate">
    		<property name="target">
    			<ref bean="uniteDaoTarget"></bean>
    		</property>
    	</bean>
    I think this should work.

    Thank u very much, it's OK

    I works great.
    The configuration is great now. I'm surprised because i read somewhere that datasource were not necessary if hibernate.properties is correctly set in the classpath !! This is wrong
    I give here my configuration file, in case it helps someone: DO NOT forget datasource declaration

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="gestionnaireTache"
    		class="coach.service.impl.basic.GestionnaireTache">
    	</bean>
    	<bean id="gestionnaireRessource"
    		class="coach.service.impl.basic.GestionnaireRessource">
    	</bean>
    	<bean id="coachFactory"
    		class="coach.model.impl.basic.ModelBasicFactory">
    		<property name="gestionnaireRessource">
    			<ref bean="gestionnaireRessource" />
    		</property>
    		<property name="gestionnaireTache">
    			<ref bean="gestionnaireTache" />
    		</property>
    	</bean>
    
    	<!-- DataSource defintion -->
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName">
    			<value>org.hsqldb.jdbcDriver</value>
    		</property>
    		<property name="url">
    			<value>jdbc:hsqldb:hsql://localhost/coach</value>
    		</property>
    		<property name="username">
    			<value>sa</value>
    		</property>
    		<property name="password">
    			<value></value>
    		</property>
    	</bean>
    
    
    	<!-- Session factory pour Hibernate -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="mappingResources">
    			<list>
    				<value>coach/model/impl/basic/Unite.hbm.xml</value>
    				<value>
    					coach/model/impl/basic/Description.hbm.xml
    				</value>
    			</list>
    		</property>
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>coach/model/impl/basic</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.HSQLDialect
    				</prop>
    				<prop key="hibernate.show_sql">
    					true
    				</prop>
    				<prop key="hibernate.max_fetch_depth">
    					1
    				</prop>
    				<prop key="hibernate.hbm2ddl.auto">
    					update
    				</prop>
    			</props>
    		</property>
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>
    
    	<!-- Gestionnaire de transaction pour Hibernate -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<!-- DAO pour la gestion des Unites -->
    	<bean id="uniteDaoTarget"
    		class="coach.dao.impl.hibernate.UniteDao">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="txProxyTemplate" abstract="true"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="transactionManager" />
    		<property name="transactionAttributes">
    			<props>
    				<prop key="*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="uniteDaoProxy" parent="txProxyTemplate">
    		<property name="target">
    			<ref bean="uniteDaoTarget" />
    		</property>
    	</bean>
    
    </beans>
    How can i rate you !!?

  6. #6

    Default

    greate job,hehe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •