2

在事务中,如果密钥重复出现异常,则发生在第二个持久化中,第一个持久化并且不执行回滚,并且这个仍然记录在数据库中。(使用更多代码更新问题以更好地检测错误)

      public interface TemperaturesDao {
        @Transactional (rollbackFor=Throwable.class)
    // @Transactional (rollbackFor=Exception.class) // changed
        void save(JavaTemperatures jT);
    }

    @Repository
    public class TemperaturesDaoImp implements TemperaturesDao{
        @Autowired
        private SessionFactory sf;
        @Transactional (rollbackFor=Throwable.class)
        // @Transactional (rollbackFor=Exception.class) // changed
        public void save(JavaTemperatures jT) {
            Session session = sf.getCurrentSession();
            session.save(jT);
        }
    }


    public interface TemperaturesService {
        @Transactional (rollbackFor=Throwable.class)
    // @Transactional (rollbackFor=Exception.class) // changed
        void saveService();
    }

    @Service("temperaturesServiceImp")
    public class TemperaturesServiceImp implements TemperaturesService{
        @Autowired
        TemperaturesDao tempeDao;

        @Transactional (rollbackFor=Throwable.class)
    // @Transactional (rollbackFor=Exception.class) // changed
        public void saveService() {
            JavaTemperatures jT = new JavaTemperatures();
            jT.setKey("key2");
            tempeDao.save(jT); // This is registered in the database at the end of the veService

            jT = new JavaTemperatures();
            jT.setKey("key1"); // This key exists in the database
            tempeDao.save(jT); // This record gives exception at the end of the saveService
        }
    }


    public class BaseBeanImp implements Serializable{
        ...
    }

    @Controller
    @Scope("session")
    public class ComparaBean extends BaseBeanImp implements Serializable {
        private static final long serialVersionUID = 1L;
        @Autowired
        private TemperaturesService tempeService;

        public String doSave() {
            tempeService.saveService() ;
            return null;
        }
    }

    **index.xhtml**
    <h:commandButton value="Save" action="#{comparaBean.doSave}" />

应用程序上下文.xml

<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" > </property>
<property name="url" value="jdbc:mysql://redhada.org:3306/db" > </property>
<property name="username" value="username" > </property>
<property name="password" value="password" ></property>
<property name="testOnBorrow" value="true" > </property>
<property name="validationQuery" value="SELECT 1" ></property>
<property name="timeBetweenEvictionRunsMillis" value="1200000" ></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name="dataSource" ref="dataSource" > </property>
<property name="annotatedClasses" >
    <list>
    <value>com.redhada.model.JavaTemperatures</value>
    </list>
</property>
<property name="hibernateProperties">
<props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop></property>
</bean>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.redhada"></context:component-scan>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
 class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" > </property>

安慰:

263194 [http-8080-Processor23] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000
263194 [http-8080-Processor23] ERROR org.hibernate.util.JDBCExceptionReporter - Duplicate entry 'clave31' for key 1
263195 [http-8080-Processor23] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
...
Caused by: java.sql.BatchUpdateException: Duplicate entry 'key1' for key 1
...
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry 'key1' for key 1
...

但它保存了记录

如果第二个 tempeDao.save 给出异常,为什么会持续存在?

为什么不做回滚?

4

3 回答 3

0

改变了你的@Transactional (rollbackFor=Exception.class)

@Transactional (rollbackFor=Throwable.class)

并检查。

于 2012-12-11T18:04:35.657 回答
0

您没有显示实际运行的调用saveService(). 调用很可能来自已经位于事务代理后面的位置,因此 Spring 无法“看到”注释以在该级别启动事务。如果您添加代码,将能够判断。

于 2012-12-11T19:07:18.527 回答
0

显然 tempeDao.save 每次调用都会启动一个新事务。所以第一次调用 save 方法成功提交,但第二次调用失败,出现重复条目​​异常和回滚。

尝试将propagation= Propagation.MANDATORY 添加到DAO 事务方法中。

@Transactional (propagation= Propagation.MANDATORY, rollbackFor=Throwable.class)

这迫使 dao 使用服务的相同事务。然后回滚将应用于所有。

我认为最新版本的spring不会有这个问题,

于 2015-10-26T17:18:59.717 回答