Announcement Announcement Module
No announcement yet.
Declarative transaction management in Spring Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:
    	<!-- ========================= GENERAL DEFINITIONS ========================= -->
    	<!-- Referencia al fichero de propiedades "" -->
    	<bean id="messageSource" class="">
    		<property name="basename"><value>messages</value></property>										
    	<!-- ========================= 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;@;1521&#58;oradesa</value></property>
    		<property name="username"><value>dqtfinf</value></property>
    		<property name="password"><value>dqtfinf</value></property>
    	<!-- 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">
    		<property name="hibernateProperties">
    				<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>-->
    	<!-- 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>
    	<!-- ========================= 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">
    				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    	<!-- 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>
    My HibernateManager is:
    public class HibernateProductoManager extends HibernateDaoSupport&#123;
        public HibernateProductoManager&#40;&#41;
         * 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;
            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;
    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.


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


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


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