简体   繁体   English

Hibernate 4 + Spring 3.2 +事务管理器+ Mysql中不回滚

[英]Hibernate 4 + Spring 3.2 + Transaction Manager + No Roll Back in Mysql

I need some help. 我需要协助。 I'm have a problem with a transaction with hibernate and spring. 我在使用休眠和春季事务时遇到问题。 I'm trying to fill up a File table in mysql. 我正在尝试填充mysql中的文件表。 My first insert works great, the second doesn't work (but it's normal...). 我的第一个插入效果很好,第二个不起作用(但这很正常...)。 But data from the frist insert are still present in the table. 但是表格中仍然存在来自第一个插入的数据。 This doesn't fit the concept of transaction. 这不适合交易的概念。 Am i alright on this ? 我可以吗?

I think a rollback should be executed the second time i try to insert corrupted data in database ( by corrupted data, i mean data that doesn't match field constraint) My first insert is ok but as the second insert is "corrupted" the first insert should be rollback and no data should be present in the table. 我认为应该在我第二次尝试在数据库中插入损坏的数据时执行回滚(通过损坏的数据,我的意思是与字段约束不匹配的数据),我的第一个插入是可以的,但是由于第二个插入被“损坏”,所以第一个插入insert应该是回滚的,并且表中应该没有数据。 Or it's not the case. 还是不是这样。 First data , from the first insert are still in the table. 表中仍保留着第一个插入的第一个数据。 It shouldn't be still present? 它不应该仍然存在吗?

I tried to look checked /uncheked exception stuff, misconfiguration of @transactionnal without success... 我试图查看已检查/未冻结的异常内容,@ transactionnal的配置错误而没有成功...

If you have any idea... 如果您有任何想法...

Thanks !! 谢谢 !!

Main.java : Main.java:

    public static void main( String[] args ) throws Exception{

            ApplicationContext appContext = new ClassPathXmlApplicationContext(    "classpath:webConfiguration/applicationContext.xml");

            FileBo aFileBo = (FileBo) appContext.getBean("fileBo");
// Data are ok, there is an insert.
File aFile = File (6778687,".bam");             
aFileBo.save(aFile);

// Error is produce here because the ".pdf" value is not tolerated in the enum field we want to fill up. No data is inserted. But a rollback should be done and data inserted before should be erased ?
File anAnotherFile = File (6567887,".pdf");             
aFileBo.save(anAnotherFile);

}

FileBoImpl.java FileBoImpl.java

public class FileBoImpl implements FileBo{
FileDao fileDao;


public FileDao getFileDao() {
    return fileDao;
}


public void setFileDao(FileDao fileDao) {
    this.fileDao = fileDao;
}

@Transactional// one transaction for multiple operations
public void save(com.clb.genomic.lyon.model.File aFile) {

 fileDao.save(aFile);
}

Hibernate.xml: Hibernate.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd " >

<!-- Hibernate session factory -->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource">
  <ref bean="dataSource"/>
</property>

<property name="hibernateProperties">
   <props>
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
     <prop key="hibernate.show_sql">true</prop>
   </props>
 </property>

 <property name="mappingResources">
   ...
  </property>

</bean>

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

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>

</bean> 

Beans.xml called in applicationContext.xml with also Hibernate.xml : 在applicationContext.xml中也与Hibernate.xml一起调用的Beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">


<!--  Data Access Object -->

<bean id="fileDao" class="com.clb.genomic.lyon.dao.FileDaoImpl" >
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean> 

<!--  Business Object  -->

<bean id="fileBo" class="com.clb.genomic.lyon.bo.FileBoImpl" >
    <property name="fileDao" ref="fileDao"></property>
</bean> 

</beans>

You're using two separate transactions here: one to save the first file, and a second one to save the second file. 您在此处使用两个独立的事务:一个保存第一个文件,第二个保存第二个文件。 So the first one is already committed when the second one rollbacks. 因此,当第二个回滚时,第一个已经提交。

If you want the two saves to be part of the same transaction, then you should call a single transaction method from main, and save the two files from this method: 如果要使两个保存成为同一事务的一部分,则应从main调用一个事务方法,并从该方法保存两个文件:

public static void main( String[] args ) throws Exception{
    ApplicationContext appContext = new ClassPathXmlApplicationContext(    "classpath:webConfiguration/applicationContext.xml");

    FileBo aFileBo = (FileBo) appContext.getBean("fileBo");
    // Data are ok, there is an insert.
    File aFile = File (6778687,".bam");    
    File anAnotherFile = File (6567887,".pdf");  

    aFileBo.save(aFile, anotherFile);
}

public class FileBoImpl implements FileBo {
    FileDao fileDao;
    // ...

    @Transactional
    public void save(com.clb.genomic.lyon.model.File aFile, 
                     com.clb.genomic.lyon.model.File bFile) {

        fileDao.save(aFile);
        fileDao.save(bFile);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM