Results 1 to 5 of 5

Thread: Declarative transaction management in Spring

  1. #1
    Join Date
    Apr 2005
    Posts
    20

    Default Declarative transaction management in Spring

    Hi friends!!
    I'm trying to use the declarative transaction management with Hibernate in Spring. But i can't obtain a rollback at my wrong actions on BBDD.

    This is my applicationContext-Hibernate.xml:
    Code:
    <beans>
    	<!-- ========================= GENERAL DEFINITIONS ========================= -->
    	<!-- Referencia al fichero de propiedades "messages.properties" -->
    	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename"><value>messages</value></property>										
    	</bean>
    
    	<!-- ========================= RESOURCE DEFINITIONS ========================= -->
      
    	<!-- DataSource a utilizar en la aplicacion -->
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
    		<property name="url"><value>jdbc&#58;oracle&#58;thin&#58;@192.168.24.13&#58;1521&#58;oradesa</value></property>
    		<property name="username"><value>dqtfinf</value></property>
    		<property name="password"><value>dqtfinf</value></property>
    	</bean>
    	
    	<!-- SessionFactory de Hibernate-->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="dataSource"/></property>
    		<!-- Fichero donde se encuentra el mapeo a los objetos relaciones en BBDD -->
    		<property name="mappingResources">
    			<value>com/acotelsa/transactions/hibernate/spring_hibernate.xml</value>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop>
                    <!-- <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
                    <prop key="hibernate.show_sql">false</prop>-->
    			</props>
    		</property>
    	</bean>
    	
    	<!-- Manager de transacciones para el SessionFactory de Hibernate &#40;alternativo a JTA&#41; -->
    	<!-- probar también con "org.springframework.transaction.jta.JtaTransactionManager"-->
    	<bean id="txManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
    	
    	<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
    	<!-- Proxy transaccional para el objeto de negocio principal de la aplicacion -->
    	<bean id="blankProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager"><ref local="txManager"/></property>
    		<property name="target"><ref local="blankTarget"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<!-- Objeto de negocio principal de SpringHibernateBlank. Implementación con Hibernate -->
    	<bean id="blankTarget" class="com.acotelsa.transactions.hibernate.HibernateProductoManager">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
    </beans>
    My HibernateManager is:
    Code:
    public class HibernateProductoManager extends HibernateDaoSupport&#123;
    
        public HibernateProductoManager&#40;&#41;
        &#123;
            
        &#125;
        
        /**
         * Crea el producto especificado
         * @param producto El producto a crear en BBDD
         * @return El identificador del producto creado
         */
        public Integer createProducto&#40;Producto producto&#41;
        &#123;
            Integer id= &#40;Integer&#41; getHibernateTemplate&#40;&#41;.save&#40;producto&#41;;
            id= &#40;Integer&#41; getHibernateTemplate&#40;&#41;.save&#40;producto&#41;; //to provoke rollback
            return id;
        &#125;
    &#125;
    When i call the "createProducto" method, is doesn't provoke the rollback, what is wrong??

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    I don't understand this:

    id= (Integer) getHibernateTemplate().save(producto); //to provoke rollback
    It looks like you expect that saving the product twice will cause a rollback (due to a PK constraint violation). It won't. Hibernate will simply ignore the second call. Essentially you are not causing Hibernate to output raw SQL inserts here: you are telling it which objects it should start dirty checking. In either case, Hibernate will generate valid inserts when the session flushes--at commit time.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Apr 2005
    Posts
    20

    Default

    ok,and how i can prove that rollback runs??

  4. #4
    Join Date
    Aug 2005
    Posts
    2

    Default

    jarva (coleguilla) , the most elegant form to do it is to throw any exception in your method (createProducto)

  5. #5
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Or you can simply use TransactionTemplate and set rollbackOnly on the transaction status which will force the rollback w/o the need of throwing an exception.
    If you want to double check that the tx was rolled back, you could use jdbc (through the jdbc template) to query the database to see if an insert/update occurred or not.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Similar Threads

  1. Replies: 2
    Last Post: Oct 17th, 2011, 11:32 AM
  2. Replies: 15
    Last Post: Sep 1st, 2005, 07:41 AM
  3. Replies: 0
    Last Post: Jun 6th, 2005, 06:22 AM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Transaction pb Hibernate/MySQL
    By syluser in forum Data
    Replies: 2
    Last Post: Aug 28th, 2004, 02:40 PM

Posting Permissions

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