开发者

Spring global transaction committed after getting an element

I am using Spring and Hibernate with Jta Transactions, I have 2 databases, and I have a problem in a transactional method.

In this method I insert a lot of objects but I throws an exception to rollback the insertions, here the code works as I expected because the objects dont appear into the database.

But if I add a line in the method that get the objects of the same table, the objects are committed into the database.

I think that when I make a SELECT the objects are auto-committed, because the exception its thrown again and the objects persists into the database.

My xml and code:

dao.xml

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:configuracion_dao.properties" />
</bean>

<bean name="productosDAO" class="practica1.hibernate.HibernateProductosDAOImpl"
    parent="abstractPracticaBean">
    <property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>

<bean name="tercerosDAO" class="${tercerosDAO.classname}" parent="abstractPracticaBean">
    <property name="dataSource" ref="dataSourceDatos" />
</bean>

<bean name="auditoriaDAO" class="practica1.hibernate.HibernateAuditoriaDAOImpl" parent="abstractPracticaBean">
    <property name="sessionFactory" ref="hibernateSessionFactory2" />
</bean>


<bean id="hibernateSessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceDatos" />
    <property name="mappingResources">
        <list>
            <value>hibernate-mappings.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
  </value>
    </property>
</bean>

<bean id="hibernateSessionFactory2"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceAuditoria" />
    <property name="mappingResources">
        <list>
            <value>hibernate-mappings-auditoria.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate开发者_StackOverflow社区.dialect=org.hibernate.dialect.HSQLDialect
  </value>
    </property>
</bean>


<bean name="dataSourceDatos" class="org.enhydra.jdbc.standard.StandardXADataSource">
    <property name="driverName" value="org.apache.derby.jdbc.EmbeddedDriver" />
    <property name="url" value="jdbc:derby:/tmp/datos.db;create=true" />
    <property name="transactionManager" value="#{txManager.transactionManager}" />
</bean>

<jdbc:initialize-database data-source="dataSourceDatos"
    ignore-failures="ALL">
    <jdbc:script location="classpath:practica1/sql/creacion_derby.sql" />
    <jdbc:script location="classpath:practica1/sql/datos.sql" />
</jdbc:initialize-database>

<bean name="dataSourceAuditoria" class="org.enhydra.jdbc.standard.StandardXADataSource">
    <property name="driverName" value="org.apache.derby.jdbc.EmbeddedDriver" />
    <property name="url" value="jdbc:derby:/tmp/auditoria.db;create=true" />
    <property name="transactionManager" value="#{txManager.transactionManager}" />
</bean>

<jdbc:initialize-database data-source="dataSourceAuditoria"
    ignore-failures="ALL">
    <jdbc:script location="classpath:practica1/sql/creacion_auditoria_derby.sql" />
</jdbc:initialize-database>

<bean id="txManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" value="#{jotm.transactionManager}" />
    <property name="userTransaction" value="#{jotm.userTransaction}" />
</bean>

<bean id="jotm" class="org.objectweb.jotm.Jotm" destroy-method="stop">
    <constructor-arg value="true" />
    <constructor-arg value="false" />
</bean>

<tx:annotation-driven transaction-manager="txManager" />

bo.xml

<bean name="tercerosBO" class="practica1.impl.TercerosBOImpl"
    parent="abstractPracticaBean" autowire="constructor">

</bean>
<bean name="productosBO" class="practica1.impl.ProductosBOImpl"
    parent="abstractPracticaBean">
    <property name="productosDAO" ref="productosDAO" />
    <property name="auditoriaDAO" ref="auditoriaDAO" />
</bean>

aplicacion.xml

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="mensajes" />
</bean>

<bean id="abstractPracticaBean" class="practica1.impl.AbstractPracticaBean" abstract="true">
    <property name="messageSource" ref="messageSource"></property>
</bean>

<import resource="bo.xml" />
<import resource="dao.xml" />

Transactional method:

@Transactional
@Override
public void actualizaPrecio(double porcentaje) {
    internalActualizaPrecio(porcentaje);
}
private void internalActualizaPrecio(double porcentaje) {
    auditoriaDAO.insertAuditoria(getMessageSource().getMessage(
            "mensaje.actualizar_productos", new Object[] { porcentaje },
            null));
    int i = 0;
    auditoriaDAO.getAuditorias(); // Without this line its works like I expected
    List<Producto> productos = productosDAO.getProductos();
    for (Producto producto : productos) {
        i++;
        if (i > 3)
            throw new UnsupportedOperationException(
                    "Error para que veamos las transacciones");
        producto.setPrecio(producto.getPrecio().multiply(
                new BigDecimal(porcentaje).divide(new BigDecimal(100))));
        productosDAO.updateProducto(producto);
    }
}

I realised that if I use auditoriaDAO.getAuditorias() the rollback only affects to Producto but if I use productoDAO.getProductos() the rollback only affects to Auditoria...


You may be mixing up flush and commit here: a SELECT statement usually flushes all previous SQL statements, in order to fetch up-to-date data (regarding the previous changes you made in the tx). It may be possible that before such a SELECT statement is done (the following DAO calls are made to the 2nd sessionFactory if I'm not mistaken), the exception exits the method without a flush. Hence no modification in database.

So the question is: are you sure you're rollbacking the tx effectively? I see you've annotated a private method: the proxy-based mechanism of Spring AOP don't handle that! You must annotate a public method and call it from outside the annotated method's class, due to this very proxy-based mechanism. See the "Method visibility and @Transactional" block in the documentation.

Another lead: you have 2 sessionFactories, so I assume you're using XA transactions/datasources: are you sure this part of the conf is OK?


Please check auditoriaDAO and productosDAO and search for other transactional annotation. I think a new transaction is created somewhere and the UnsupportedException rollbacks only the last transaction, and the parent transaction is committed. Hope I helped!

I have found two example. Please check it.

JOTM transactions in spring and hibernate

Access Multiple Database Using Spring 3, Hibernate 3 and Atomikos

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜