トランザクションでキーの重複の例外が発生した場合、それは 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}" />
applicationContext.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
...
しかし、それは記録を保存します
2 番目の tempeDao.save で例外が発生した場合に持続するのはなぜですか?
ロールバックを行わないのはなぜですか?