Results 1 to 7 of 7

Thread: Tx not commiting records using @Transactional and <tx:annotation-driven/>

  1. #1
    Join Date
    Apr 2007
    Posts
    17

    Default Tx not commiting records using @Transactional and <tx:annotation-driven/>

    Hi There,

    I'm testing out my persistence layer in a small console application before moving it to the webapp. I am using Spring 3.x and Hibernate 3.x and using Java 6 with PostgreSQL 8.4.

    PROBLEM : I'm able to persist and recover the data BUT unable to commit the transaction using the declarative Tx support.

    @Transactional annotations and <tx:annotation-driven/>

    Here's my DAO.

    Code:
    @Transactional
    public class AddressesDAO extends HibernateDaoSupport {
    	@Transactional
    	public void save(Addresses transientInstance) {
    		log.debug("saving Addresses instance");
    		try {
    			getHibernateTemplate().save(transientInstance);
    			log.debug("save successful");
    		} catch (RuntimeException re) {
    			log.error("save failed", re);
    			throw re;
    		}
    	}
    
    	@Transactional(readOnly = true)
    	public Addresses findById(java.lang.Integer id) {
    		log.debug("getting Addresses instance with id: " + id);
    		try {
    			Addresses instance = (Addresses) getHibernateTemplate().get(
    					"com.vattikutiirf.hibernatespring.Addresses", id);
    			return instance;
    		} catch (RuntimeException re) {
    			log.error("get failed", re);
    			throw re;
    		}
    	}
    }
    I am actually able to save and retrieve the record back from the DB and I see that Hibernate is issuing the right SQL while persisting and retrieving it BUT the problem is that the DATA is NOT committed to the disk(I confirm that by going to psql and issuing a query to the same table). I am NOT seeing a commit statement from Hibernate. Just to be sure, I tried to insert the record myself directly using psql and it committed the record just fine. So, I'm sure that there are NO issues with PostgreSQL itself.

    How do I debug this problem ?

    Thank you,

    BR,
    ~A

    Here's my spring config file.


    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:aop="http://www.springframework.org/schema/aop"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    		http://www.springframework.org/schema/tx
    		http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="configLocation" value="file:src/hibernate.cfg.xml">
    		</property>
    	</bean>
    
    	<bean id="AddressesDAO" class="com.vattikutiirf.hibernatespring.AddressesDAO">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="persistenceLayer" class="com.vattikutiirf.hibernatespring.PersistenceLayer">
    		<property name="addressesDAO">
    			<ref bean="AddressesDAO" />
    		</property>
    	</bean>
    
    	<!--
    		enable the configuration of transactional behavior based on
    		annotations
    	-->
    	<tx:annotation-driven />
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    </beans>

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    1) Use an ApplicationContext instead of a BeanFactory
    2) You aren't programming to interfaces so you must use classproxies
    3) Make sure your hibernate configuration is correct, you didn't post it.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2007
    Posts
    17

    Default classproxies

    Hi Marten,

    Thank you very much.

    Hibernate config : It has info like password, so I didn't post. But as I said earlier, this code is able to create a new record and retrieve it : THE ONLY PROBLEM is that the record is NOT being persisted.

    1) Use an ApplicationContext instead of a BeanFactory
    Will that change the Transaction semantics ?
    2) You aren't programming to interfaces so you must use classproxies
    "classproxies" : how do I do that ? Kindly point me to some sample code.

    Thank you,

    BR,
    ~A

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    I suggest a read of the reference guide especially chapter3 (explaining the difference between BeanFactory and ApplicationContext). Chapter 9 covers transactions and how to configure tx:annotation-driven.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Apr 2007
    Posts
    17

    Default

    Hi Marten,

    Thank you.

    I just followed your advice and it works! Except that I introduced Interfaces.

    Thank you,

    BR,
    ~A
    Last edited by anjanb; Jul 17th, 2010 at 06:51 AM. Reason: clarification

  6. #6
    Join Date
    Oct 2009
    Location
    The Netherlands
    Posts
    18

    Default

    Quote Originally Posted by anjanb View Post
    Except that I introduced Interfaces.
    If you don't want to introduce interfaces, you can tell spring to create "class based proxies" for classes with transactional functionality. By default the interface based proxies are created. To create class based proxies you need to provide cglib library on your classpath and then (not required) enforce creating class based proxies for classes with transactional functionality by specifying the following parameter:
    Code:
    <tx:annotation-driven proxy-target-class="false"/>
    Regards,

    Wojciech

  7. #7
    Join Date
    Apr 2007
    Posts
    17

    Default

    Thank you.

    will remember this next time I need to work on classes only.

    reminds me that I need to spend a few days going through the spring reference manual to understand the details.

Tags for this Thread

Posting Permissions

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