Announcement Announcement Module
Collapse
No announcement yet.
Declarative transaction management in Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      ok,and how i can prove that rollback runs??

      Comment


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

        Comment


        • #5
          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.

          Comment

          Working...
          X